Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science.

Similar presentations


Presentation on theme: "Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science."— Presentation transcript:

1 Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Place photo here

2 The Objectives and the outcomes The Objectives: 1.To understand the basic concept of C programming. 2.To be familiar with the programming structure. 3.To be able to write a simple program, compile it and run it. The Outcomes: 1.Students should have brief knowledge about computers programming. 2.Students should know how programs are structured. 3.Students should be able to write and run a simple program.

3 Introduction to Programming and Programming language The original C was still too limiting, and not standardized, and so in 1983 an ANSI committee was established to formalize the language definition. It has taken until now (ten years later) for the ANSI (American National Standard Institute) standard to become well accepted and almost universally supported by Compilers. The first language developed was (BCPL) and the next upgraded version was (B). The language (B) was upgraded to (C) by Dennis Ritche of AT & T laboratories, USA. C was first used to write UNIX OS. (C) is a general purpose language. Bjarne Strousstrup added object orientation to (C) and developed (C++).

4 Structure of a program Every C program consists of one or more modules called functions. One of these functions is called main. The program begins by executing main function and accesses other functions, if any. Functions are written after or before main function separately. A function has: (1) heading consists of name with list of arguments (optional) enclosed in parenthesis, (2) argument declaration (if any) and (3) compound statement enclosed in two braces { } such that each statement ends with a semicolon. Comments, which are not executable statement, of necessary can be placed in between /* and */.

5 Structure of a program In other words, at this stage the programs considered will fit into the following general format: // Introductory comments // file name, programmer, when written or modified // what program does #include int main () { constant declarations variable declarations executable statements }

6 Displaying a message Example Let us begin with a simple C program that displays the message “Hello world”. //C hello world example #include int main() { printf("Hello world\n"); return 0; } Output Hello world

7 Computing with C // Sample program // IEA September 2013 // Reads values for the length and width of a rectangle // and returns the area of the rectangle. #include int main() { int length, breadth, area; printf("\nEnter the Length of Rectangle : "); scanf("%d", &length); printf("\nEnter the Breadth of Rectangle : "); scanf("%d", &breadth); area = length * breadth; printf("\nArea of Rectangle : %d \n", area); return (0); } Output : Enter the Length of Rectangle : 5 Enter the Breadth of Rectangle : 4 Area of Rectangle : 20

8 Problem Solving Tools 1.Edit: Program is created in the editor and stored on the disk 2.Compile: Compiler create Object code and stores on disk. 3.Link: Linker links the Object code with Libraries, creates result and stores it on disk. 4.Load: Loader prints program in memory. 5.Execute: CPU takes each instruction and executes it. Possibly storing new data Values as program execute.

9 Creating, Compiling, and Running Programs 1.Study the requirement specification for the application. 2.Analyze the problem and decide how to solve it. 3.Translate the algorithm produced at the previous step into a suitable high-level language. 4.Compile the program into machine language. 5.The object code produced by the compiler will then be linked with various function libraries that are provided by the system. 6.Run the compiled, linked and loaded program with test data. 7.The program can now be put into general use - though unless the testing was very comprehensive it is possible that at some future date more logical errors may become apparent.

10 Creating, Compiling, and Running Programs

11 The character set: Character set includes the basic building blocks of a language. C character set consists of: 1.Numeric Character Set: Alphabetic Character Set: A-Z, a-z 3.Special Character Set: #, ;, :,(comma) etc 4.Escape Character Set: they are used for formatting the output. It always starts with a back slash ( \ ). Some Examples are \n, \t, \b, \a

12 Tokens The smallest individual unit in a program is known as token. C has the tokens, like: Keywords, Identifiers, Literals, Punctuators, and Operators. Keywords Keywords or Reserved words are those words, which are reserved for a specific purpose and part of the language. Examples: auto, break, case, char, const, continue, default, do, double, else enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while.

13 Tokens Punctuators The following characters are used as punctuators (also known as separators) in C: * + ( ), -, ; : * … = # Identifiers Words that are not reserved or keywords are called user defined words or identifiers. Identifiers must follow the following rules: 1.Can be composed of alphabets (A-Z or a-z), numbers (0 to 9) and underscore “_” 2.The first character must be an alphabet or an underscore “_”. 3.Identifier must be unique. 4.Blank Spaces are not allowed. 5.They are case sensitive i.e. Val and val are different Examples: Roll_number, r_no, Z, A1234_BC etc. Use the name of the identifier that is self-explanatory e.g. employee_name, student_name etc.

14 Data type: Data types are means to identify the type of data and associated operations of handling it. C data types are of two types; namely fundamental data types and derived data type. Fundamental data types Derived data type

15 Fundamental data types Data type:

16 Fundamental data types

17 Data type: Derived data type From the fundamental types, other types can be derived, using the declaration operators, e.g. Arrays.

18 Constants and Variables Constants (Literals): Constants are identifiers, which do not change its stored value during executing the program. There are 4 basic types of constants: 1.Integer constants: It is an integer valued number written in three different number system types, namely; decimal (base 10), octal(base8), and hexadecimal(base 16). 2.Floating point constants: It is a decimal number (i.e.; base 10) with a decimal point or an exponent or both. Ex; 32.65,0.654, 0.2E-3, 2.65E10 etc. These numbers have greater range than integer constants. 3.Character constants: It is a single character enclosed in single quotes like ′a′. ′3′, ′?′, ′A′, etc. each character has an ASCII to identify. For example ′A′ has the ASCII code 65, ′3′ has the code 51 and so on. 4.String constants: it consists of any number of consecutive characters enclosed in double quotes. Ex: const char *HELLO2 = "Howdy";

19 Constants and Variables Variables A variable is an identifier that has a changeable value during the execution of a program. 1.Variable Declaration: Declare a variable means to allocate a memory location to hold the value of this variable. 2.Defining a variable: each variable should have an Identifier, which is the variable name that you will use during the program, and in fact it is translated to an address of the memory location. 3.Data Type: it shows the type of the value that will be stored in this variable, such as Data_Type Identifier; int number1;

20 There are different types of variables such as: 1.Character variable (To store characters) 2.Integer variable (To store whole numbers) 3.Float variable (To store floating point numbers) 4.Boolean variable (To store two values true/false) e.g.: int a,b,c,d=10; (you can also initialize at the time of declaration). Constants and Variables Variables


Download ppt "Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills-2 4800153-3 1435/1436 Department of Computer Science."

Similar presentations


Ads by Google