Summer 2009/10 ITEC 113 LECTURE NOTES 5 : A SIMPLE C PROGRAM STRUCTURE: The sample run of above program : First Line : #include <stdio.h> Preprocessor directive : Starts with # character and it is used to call in action the header file <stdio.h> which is enclosed in angle brackets. Note that this header file contains definitions of some functions which we intend to use in the program. Note that the preprocessor is not terminated with semicolon ( ; ) 2 Second Line: main() Syntax: Should be written in lower case letter and terminated by open and closing paranthesis() It should NOT be terminated by open and close braces ( {} ). C is a well structured general-purpose program that is supporting subroutine use. As such it allocates modular construction of the programs. This means big programs can be divided into small modules and they can be build by putting together these modules which are called functions. main() is considered as the master function. Program execution starts in main() and also terminates in main() . The main() will call for execution any function linked to the main(). The set of statements enclosed in open brace { and closing brace } following the main() is called the main() block of statements. Open brace { is also be called “begin block” and the closing brace } is called “end block” third line : { ( open brace ) “begin block” Fourth and fifth lines : int num; int sq; int is a reserved ( special ) word indicating that the symbols num , and sq symbols following it will process integer type variable. The 2 statements above are called “declaration statements” Syntax : Declaration statements has to be terminated with semicolon ( ; ). sixth line : printf("\nEnter an integer number --> "); This is an “output statement”. It is used to send a message to the output Unit, the monitor. Syntax : Output statement has to be terminated with semicolon ( ; ). seventh line : scanf("%d",&num); This is an “input statement”. It is used to enter data into symbol ‘num’ using the keyboard. Syntax : Input statement has to be terminated with semicolon ( ; ). eighth line : 3 sq=num*num; This is an “assignment statement”.It evaluates num x num and assigns ( writes ) the resut to the symbol ‘sq’. Syntax : Assignment statement has to be terminated with semicolon ( ; ). nineth line : printf("\nThe square is = %d",sq); This is an “output statement”. It is used to send a message to the output Unit, the monitor. Syntax : Output statement has to be terminated with semicolon ( ; ). eighth line : } ( closing brace ) “end block” C Summary : » As C is a structured programming language it is a collection of functions (subroutines or sub-programs), which they are working together in order to solve a problem. » C is case sensitive. » Uppercase letters in C are considered to be different then lowercase letters. Example : income_tax INCOME_TAX » The first function in C has to be the main(). » When you execute a program written in C, the main() function gets the control of flow to share it with the other functions. » Step of instructions are named as Statements. » Each statement in C has to be ended with a semi-colon (;) character. » In C, related statements are blocked with {, and } characters. 4 » Every action in C has to be used in blocks. » Each function has its own execution block. The Preprocessor Directives » Begins with # character and occurs before main(). » Does not end with a semicolon. » Has a header file inside angle brackets < and >. » A header file contains information about frequently used constants, and predefined functions. » Header files are used as code Libraries. e.g: stdio.h is a header file, containing information about the family of standard input-output functions, and constants. » The definition of the function printf() is saved in stdio.h. » In order to use a pre-defined function, we have to include the header file that contains the definition of the function. e.g. Without including stdio.h header file, you can not use the function printf(). The Function main() » The main() executes C statements and other functions. » A function can be: » User-defined function. » Standard-library (predefined) function. » Function main() is followed by a left brace {, and ends with right brace }. » Left brace {, is some times called as “Begin Block”, and Right brace is called as “End Block”. The Function printf() » The printf() is a standard output function declared in stdio.h library. » It is used to display anything placed between parentheses to the monitor. 5 » It has an argument list enclosed in parentheses. Example: (A typical main function) main() { printf(“ Hello World ! ”); } » In this example, the function main() got the control of execution, and then gave it to the function printf() in order to display the text “ Hello World !”. There is a character string enclosed in double quotes as an argument of the function printf() above. The Use of Comments : Example : The output of above program will be: 6 » » » » » » The previous example consists of : » Comments ( /* Some Comments here*/ ) » Preprocessor Directive ( # include <stdio.h> ) » The function main() and its braces { } » The function printf() » An Escape Sequence » Semicolons. A Comment begins with /* and ends with */ They are not executable statements. C compiler ignores everything between /* and */ Comments can appear everywhere in the program They may be several lines. 7 // Double slashes can also be used to identify comment lines. Using /* ……*/ we can comment out multiple lines. Using // we can only comment out all characters on the single line to the right of the //. VARIABLES : » » » » » Variables are basic data objects manipulated in a program. Each variable has to be declared before use. Each variable has a name and a data type. You can give initial value (variable initialization) on variable declaration. A variable name is the symbolic representation of the memory location that is allocated on variable declaration. Rules on Variable Names: DO NOT use reserved words as variable names (e.g. if, else, int, float, case, for, …). The first character has to be a letter or underscore. It can not be a numeric digit. The second and the other characters of the name can be any letter, any number, or an underscore “_”. 8 Some valid names: my_name, m113_1, salary, bluemoon , _at Some invalid names: my name, my-name , 1stmonth , salary! , guns&roses , Tradition on Variable Names: ( Note that, these are NOT Rules but you can increase the quality of your program with using them! ) 1. Select related and meaningful names to your variables indicating their tasks. 2. Do not exceed 8 characters on your variable names. 3. Use small case letters on your variable names. (Upper case letters are mostly used in the names of symbolic constants). Variable Declaration: » Variable declaration is used to introduce the system to the variables that the programmer decides to use on the rest of the program. » On variable declaration, 1. variable name, 2. data type are declared. » Also you can give the initial value of the variable on its declaration. Example : int k ; int m=15; float fnumber= 1.75; char ch=’w’ ; Data Types of the Variables : » A variable data type specifies: » The kind of value a variable can store » The set of operations that can be applied to the variable » There are 3 main different data types and their derivations for declaration in ANSI–C. Main Data types Derived Data Types integer short, long float Double char Data Types and Sizes : 9 Integers (int) : » Integers are all numeric values that have no fractional or decimal components. » Integer numbers may be positive or negative. Examples : 13 7 –6 208 1024 » C is allocating 4 bytes (32 bits) to an integer (int) variable. » An integer variable can store values in the range –32,768 through 32,767 Derived Integers : » short, long and unsigned are data types derived from int, and used to keep integer values. » The sizes of long and short is differentiated from int. » The data type unsigned is used only with positive integers. Data Types Bytes Used int 4 Bytes short 2 Bytes double 8 Bytes unsigned 4 Bytes Real Numbers : » C is using float and double data types for real numbers. » The float data type requires 4 bytes and has a precession of seven digits (seven digits after decimal point). » The double data type requires 8 bytes and has a precession of fifteen digits (fifteen digits after decimal point). Example : float: 3.14159 534.322344 0.3333333 double: -3738.787878787878787 3.141592653589790 Using Scientific Notation to represent real numbers : We can use Scientific Notation to represent real numbers that are very large or very small in value. The letters e or E is used to represent times 10 to the power. e.g.: 1.23 x 10 5 is represented in C as 1.23e5 or 1.23e+5 or 1.23E5 1 x 10 -9 is represented in C as 1e-9 Character : ( char ) Character is a value in ( ASCII ) e.g : A is represented by decimal 65 or 8-bit binary 0 1 0 0 0 0 0 1 10 Categories of characters : 1. Alphabetic Letters ( Upper case : ‘A’ , ‘B’, …….. ‘Z’ ) 2. Alphabetic Letters ( Lower case : ‘a’ , ‘b’, …….. ‘z’ ) 3. Numeric digits ( ‘1’,’2’,’3’,…………,’9’,’0’ ) 4. Special Characters ( blank, ‘+’,’#’,’-‘,’_’,……..) 5. Control Characters ( ‘\n’ , ‘\t’ , ……. ) Characters constants are usually used enclosed single quotes ‘A’ , ‘7’, Only one byte of memory location is used by a character variable In ASCII code is used to represent uniquely any one of the available 255 characters VARIABLES : 11 » » » » » Variables are basic data objects manipulated in a program. Each variable has to be declared before use. Each variable has a name and a data type. You can give initial value (variable initialization) on variable declaration. A variable name is the symbolic representation of the memory location that is allocated on variable declaration. Rules on Variable Names: DO NOT use reserved words as variable names (e.g. if, else, int, float, case, for, …). The first character has to be a letter or underscore. It can not be a numeric digit. The second and the other characters of the name can be any letter, any number, or an underscore “_”. Some valid names: my_name, m113_1, salary, bluemoon , _at Some invalid names: my name, my-name , 1stmonth , salary! , guns&roses , Tradition on Variable Names: ( Note that, these are NOT Rules but you can increase the quality of your program with using them! ) 4. Select related and meaningful names to your variables indicating their tasks. 5. Do not exceed 8 characters on your variable names. 6. Use small case letters on your variable names. (Upper case letters are mostly used in the names of symbolic constants). Variable Declaration: » Variable declaration is used to introduce the system to the variables that the programmer decides to use on the rest of the program. » On variable declaration, 1. variable name, 2. data type are declared. » Also you can give the initial value of the variable on its declaration. Example : int k ; 12 int m=15; float fnumber= 1.75; char ch=’w’ ; Data Types of the Variables : » A variable data type specifies: » The kind of value a variable can store » The set of operations that can be applied to the variable » There are 3 main different data types and their derivations for declaration in ANSI–C. Main Data types Derived Data Types integer short, long float Double char Data Types and Sizes : Integers (int) : » Integers are all numeric values that have no fractional or decimal components. » Integer numbers may be positive or negative. Examples : 13 7 –6 208 1024 » C is allocating 4 bytes (32 bits) to an integer (int) variable. » An integer variable can store values in the range –32,768 through 32,767 Derived Integers : » short, long and unsigned are data types derived from int, and used to keep integer values. » The sizes of long and short is differentiated from int. » The data type unsigned is used only with positive integers. Data Types Bytes Used int 4 Bytes short 2 Bytes double 8 Bytes unsigned 4 Bytes Real Numbers : » C is using float and double data types for real numbers. 13 The float data type requires 4 bytes and has a precession of seven digits (seven digits after decimal point). » The double data type requires 8 bytes and has a precession of fifteen digits (fifteen digits after decimal point). Example : float: 3.14159 534.322344 0.3333333 double: -3738.787878787878787 3.141592653589790 » Using Scientific Notation to represent real numbers : We can use Scientific Notation to represent real numbers that are very large or very small in value. The letters e or E is used to represent times 10 to the power. e.g.: 1.23 x 10 5 is represented in C as 1.23e5 or 1.23e+5 or 1.23E5 1 x 10 -9 is represented in C as 1e-9 Character : ( char ) Character is a value in ( ASCII ) e.g : A is represented by decimal 65 or 8-bit binary 0 1 0 0 0 0 0 1 Categories of characters : 6. Alphabetic Letters ( Upper case : ‘A’ , ‘B’, …….. ‘Z’ ) 7. Alphabetic Letters ( Lower case : ‘a’ , ‘b’, …….. ‘z’ ) 8. Numeric digits ( ‘1’,’2’,’3’,…………,’9’,’0’ ) 9. Special Characters ( blank, ‘+’,’#’,’-‘,’_’,……..) 10. Control Characters ( ‘\n’ , ‘\t’ , ……. ) Characters constants are usually used enclosed single quotes ‘A’ , ‘7’, Only one byte of memory location is used by a character variable In ASCII code is used to represent uniquely any one of the available 255 characters
© Copyright 2024