Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.

Similar presentations


Presentation on theme: "1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two."— Presentation transcript:

1 1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two

2 2 Lecture Two Outline C Program Structure C Language Elements Preprocessor directives Function Header, and Function body Executable statements Reserved word, Standard identifiers, user defined identifiers Data Types and Variable Declarations Expressions and Assignment in C

3 3 Lecture Two Outline Input/Output operations Function Output function Input function Place holders Escape sequences Comments in C Example

4 4 C Program Structure

5 5 C Program structure A Sample C Program Function Header Function Body

6 6 Write a program to Convert given distance in miles to kilometers. C Program structure Example for Another C Program

7 7 C Program structure A Sample C Program

8 8 C Language Elements Preprocessor Directives Are commands that give instructions to the C preprocessor Begins with a (#) as its nonblank character. C preprocessor is a system program that modifies a C program prior to its compilation Example #include –The C language cannot do I/O by itself, so we need help from the library “stdio.h” to use the screen/Keyboard ! –We can use other libraries, too, as needed. –Another popular “library” is math.h, for advanced math functions.

9 9 C Language Elements Preprocessor Directives Examples #define KMS_PER_MILE 1.609 # define AT ‘@’ # define VOTING_AGE 18 –Define is a a preprocessor directives. –Valid constant declarations –A named constant is a location in memory that we can refer to by a name, and in which a data value that cannot be changed is stored. –This directives instructs the processor to replace each occurrence of KMS_PER_MILE in the text of the C program by 1.609 before compilation begins.

10 10 C Language Elements The Function Header int main ( ) type of returned value name of function says no parameters

11 11 A C program is a collection of one or more “functions” (parts) There must be a function called main( ) A function body has two parts declaration and executable statements. Execution always begins with the first statement in function main( ) Any other functions in the program are subprograms and are not executed until they are called C Language Elements The Function Body

12 12 printf(“ Hello, World “); This statement means: Display the words Hello, World on the screen return ( 0 ); This statement STOPs the program and returns Zero to the O.S. (more about this later) C Language Elements The Executable Statements

13 13 Reserved Word A word that has special meaning in C Standard Identifiers A word having special meaning but one that a programmer may redefine (but redefinition is not recommended) User Defined Identifiers An identifier must consist only of letters, digits, and underscores. An identifier cannot begin with a digit A C reserved word cannot be used as an identifier. An identifier defined in a C standard library should not be redefined. C is a case-sensitive language. The names Pressure, pressure, and PRESSURE are viewed by the compiler as different identifier. C Language Elements Words

14 14 Invalid identifiers 1Letter begins with a number double reserved word int reserved word TWO*FOUR character * not allowed joe’s character ‘ not allowed age# character # not allowed Age-of-cat character – is not underscore character (_) Valid Identifiers Age_of_person taxRateY2k PrintHeading C Language Elements Invalid/Valid identifiers

15 15 Reserved Word int void Double return Standard Identifiers Printf scanf User Defined Identifiers KMS_PER_MILE miles kms C Language Elements Words in the second example

16 16 Integral Types represent whole numbers and their negatives declared as int, short, or long Int sample values 4578, -4578, 0 Floating Types represent real numbers with a decimal point declared as float, or double Float sample values 95.274 95.0 0.265 Character Types represent single characters declared as char Char sample values B d 4 ? * Standard Data Type in C

17 17 What about words and sentences ? a String is a sequence of characters enclosed in double quotes sample values “Hello” “Year 2004” “1234” Not in Standard C, We’ll cover it later (in Chapter 7!)

18 18 Variable A name associated with a memory cell whose value can change. Variable Declaration Statements that communicate to the compiler the names of variables in the program and the type of information stored in each variable. Variable Declaration in C

19 19 Giving a Value to a Variable You can assign (give) a value to a variable by using the assignment operator = VARIABLE DECLARATIONS char code ; int i; long national_debt; float payRate; double pi; VALID ASSIGNMENT STATEMENTS code = ‘B’ ; i = 14; national_debt = 10000000; pay_rate = 14.25; pi = 3.1415926536;

20 20 Giving a Value to a Variable

21 21 What is an Expression in C? An expression is a valid arrangement of variables, constants, and operators. in C each expression is evaluated to compute a value of a given type the value of the expression 9 + 5 is 14

22 Variable = Expression First, Expression on right is evaluated. Then the resulting value is stored in the memory location of Variable on left. Any information that was stored in the variable will be lost. NOTE: An automatic type conversion occurs after evaluation but before the value is stored if the types differ for Expression and Variable Example kms = KMS_PER_MILE * miles; Assignment Operator Syntax

23 23 What is the content of variables a and b after each step? int a, b ; a = 5 ; b = 7 ; b = a + b ; a = b - 1 ; b = 3 ;

24 24 printf (Output Function) The printf function displays the value of its format string after substituting in left-to-right order the values of the expressions in the print list for their list for their placeholders in the format string and after replacing escape sequences such as \n by their meanings. scanf (Input Function) The scanf function copies into memory data entered during the program execution. The order of the placeholders must correspond to the order of the variables in the input list. The data must be entered in the same order in the input list. You should insert one or more blank characters or carriage returns between numeric items. printf/scanf function

25 25 SYNTAX Examples : printf( format string, print list ) ; printf(format string); printf(“That equals %f kilometers. \n”, kms); printf(“enter the distance in miles> ”); printf( “Hello, World?\n“); Output Function Place holder Escape sequence

26 26 Output Function

27 27 SYNTAX Examples : scanf( format string, input list ) ; scanf(“%lf”, &miles); Input Function Place holder Ampersand

28 28 Input Function

29 29 Place Holders Function useVariable typePlaceholder printf/scanfchar%c printf/scanfint%d printfdouble%f scanfdouble%lf A placeholder is a symbol beginning with % in a format string that indicates where to display the output value

30 30 Escape sequence \n Start a new line of output \t tab printf(“This is one line \n“); printf(“ and \n this \t is \t another \n “); This is one line and this isanother The backslash (\) is called an escape character Indicates that printf is supposed to do something unusual When encountering a backslash, printf looks to the next character and combines it with the backslash to form an escape sequence

31 31 C comments A comment is used to provide information, but it is ignored by the processor. Begin with /* and ends with */ /* Name : Foulan El Foulany */ /* This is a multi line comment */

32 32 Example Write a program to create two integer variables, and store in them the values 3 and 5. Then calculate their sum and their product and display the result.

33 33 /* * Name: Ali Reda * This program gets the sum and product of 2 integers */ #include /* library containing printf */ int main ( ) { int first ;/* declaring first variable */ int second ;/* declaring second variable */ int sum ;/* variable to hold the sum */ int product ;/* variable to hold the product */ C Program

34 34 C Code Continued first = 3 ;/* assignment statement */ second = 5 ; sum = first + second ; printf(“ The sum is %d \n “, sum );/* output */ product = first * second ; printf(“ The product is %d \n “, product ); return (0); }

35 35 Output of Program The sum is 8 The product is 15

36 36 /* * Name: Ismael Youssef * This program gets the sum and product of 2 integers */ #include /* library containing printf */ int main ( ) { int first = 3 ;/* declaring and assigning first variable */ int second = 5 ;/* declaring and assigning second variable */ int sum, product ; sum = fiirst + second ; product = first * second ; printf(“ The sum is %d \n The product is %d \n “, sum, product ); return ( 0 ) ; } Same program, different style


Download ppt "1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two."

Similar presentations


Ads by Google