Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Structured Programming in C Welcome to CPSC 206.

Similar presentations


Presentation on theme: "1 Structured Programming in C Welcome to CPSC 206."— Presentation transcript:

1 1 Structured Programming in C Welcome to CPSC 206

2 2 Lecture Information http://people.cs.tamu.edu/ychen/Teaching/CPSC206

3 3 Lecture Topics: 0. Introduction to Computer Science 1. Overview of CCh 1, 2 2. Flow of control and functionsCh 3, 4 3. Character processing & fundamental data typesCh 5, 6 4. File I/O Ch 13 5. Pointers, Arrays, and StringsCh 8, 9, 10 6. Structures, and linked listsCh 12 Features of C: 7. Enumeration type and storage classesCh 7, 8 8. RecursionCh 11

4 4 Review of Class on Sept. 16, Thursday

5 5 Chapter 2: Lexical Elements, Operators, and the C System

6 6 Lexical Elements, Operators, and the C System  C is a language  Alphabet  syntax  What is C program? A C program is a sequence of characters Lexical Elements, Operators, and the C System How a computer understands this sequence of characters?

7 7 Introduction  How to check a C program is correct? The characters are collected by the compiler into syntactic units called tokens The compiler checks whether the tokens can be formed into legal strings according to the syntax of C language. Lexical Elements, Operators, and the C System

8 8 Introduction  In C language, there are six kinds of tokens:  Keywords  Identifiers  Constants  String constants  Operators  Punctuators Lexical Elements, Operators, and the C System

9 9 Lexical Elements Comment  What is comment? Arbitrary strings of symbols placed between the delimiters /* and */. Single line comment: // text  The compiler changes each comment into a single blank character.  Rules: Multi-line comments cannot be placed in the middle of a keyword or identifier. Multi-line comments may not be nested. Lexical Elements, Operators, and the C System Lexical Elements

10 10 Lexical Elements  Keywords  What is Keywords? Keywords are explicitly reserved words that have a strict meaning as individual tokens in C.  Examples of Keywords Data Type: int, char, long, short  Keywords cannot be redefined or used in other contexts. Lexical Elements, Operators, and the C System Lexical Elements

11 11 Lexical Elements  Identifiers  What is identifier? The names of variables, functions, labels and other user-defined items are called identifier.  Special identifier Keywords, names of functions in C library, main  Rules: 1.composed of letters, digits, and underscore _. 2.The first character must be a letter or underscore. 3.case-sensitive 4.would not be defined as the special identifiers: Lexical Elements, Operators, and the C System Lexical Elements

12 12 Lexical Element  Constants  Integer constants: 0, 17 Decimal integer: 17 Octal integer: 017 Hexadecimal integer: 0x17  Floating constants: double, float, long double  Character constants: (enclosed between single quotes) Lexical Elements Lexical Elements, Operators, and the C System

13 13 Lexical Elements  String Constants  String constant is a sequence of characters enclosed in a pair of double quote marks.  String constants are differently form character constants.  Special characters: \”, \\  You mustn't split a string constant across lines  Two string constants that are separated only by white space are concatenated by the compiler into a single string. Lexical Elements Lexical Elements, Operators, and the C System

14 14 Lexical Elements  Operators and Punctuator  Precedence and Associativity determine precisely how expressions are evaluated. when they will be evaluated  Precedence of operators indicates when they will be evaluated.  Associativity “left to right”: Operations are performed from left to right oExamples: +,-,/,% “right to left”: Operations are performed from right to left oExamples: ++(prefix), --(prefix) Lexical Elements Lexical Elements, Operators, and the C System

15 15 Class on Sept 21

16 16 Outline  An Example — Characters and Lexical Elements  Lexical Elements  Comments  Keywords  Identifiers  Constants  String Constants  Operators and Punctuators  An Example: Computing Powers of 2  The C System Lexical Elements, Operators, and the C System

17 17 Operators and Punctuators — Outline  Examples of Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators  Assignment Operators Lexical Elements Lexical Elements, Operators, and the C System

18 18 Operators and Punctuators — Increment ++ and Decrement Operators --  Semantics  Precedence and Associativity  Rules Lexical Elements Lexical Elements, Operators, and the C System

19 19 Operators and Punctuators — Increment ++ and Decrement Operators --  Increment ++i, i++  Each causes the stored value of i in memory to be incremented by 1.  Each of the expressions ++i and i++ has a value. ++i othe stored value of i is incremented first othe expression takes as its value the new stored value of i i++ othe expression takes as its value the current stored value of i othe stored value of i is incremented Lexical Elements Lexical Elements, Operators, and the C System

20 20 Operators and Punctuators — Increment ++ and Decrement Operators -- #include int main(void) { int i, j, a, b; i=0; j=0; a = ++i; b = j++; printf("a=%d, b=%d\n",a,b); return 0; } % gcc id.c % a.out a=1, b=0 id.c Lexical Elements Lexical Elements, Operators, and the C System

21 21 Operators and Punctuators — Increment ++ and Decrement Operators --  ++ and +  ++ Cause the value of a variable in memory to be changed  + Does not change the value of a variable. Lexical Elements Lexical Elements, Operators, and the C System

22 22 Operators and Punctuators — Increment ++ and Decrement Operators --  Decrement Operator i-- and --i  The value of i is decremented by 1.  Each expression has a value. --i othe stored value of i is decremented by 1 othe expression takes as its value the new stored valued of i i-- othe expression takes as its value the current stored valued of i othe stored value of i is decremented by 1 Lexical Elements Lexical Elements, Operators, and the C System

23 23 Operators and Punctuators — Increment ++ and Decrement Operators --  Semantics  Precedence and Associativity  Rules Lexical Elements Lexical Elements, Operators, and the C System

24 24 Operators and Punctuators — Increment ++ and Decrement Operators --  Precedence and Associativity Associativity  ++ (postfix) -- (postfix) Left to right  +(unary) –(unary) ++(prefix) --(prefix) R ight to left  * / % Left to right  + -Left to right Lexical Elements Lexical Elements, Operators, and the C System

25 25 Operators and Punctuators — Increment ++ and Decrement Operators --  Precedence and Associativity  +(unary) –(unary) ++(prefix) --(prefix) Right to left #include int main(void) { int a=2; int result; result = - --a; printf("a=2, - --a = %d\n",result); return 0; } % gcc id2.c % a.out a=2, - --a = -1 id2.c Lexical Elements Lexical Elements, Operators, and the C System Question1: What is the value of a after the operation --a ? Question2: What is the value of the expression --a?

26 26 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples #include int main(void) { int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a * b - c --; result2 = 7 - - b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b * ++ d = %d\n",result2); return 0; } ++ a * b – c -- ++ (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix)right to left * / % left to right + -left to right Precedence and Associativity ) ( ) ( ) ( ++ a * b – 3 2 * b – 3 4 – 3 1 ) ( ) ( ) ( id2.c Lexical Elements Lexical Elements, Operators, and the C System

27 27 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples #include int main(void) { int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = 7 - - b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b * ++ d = %d\n",result2); return 0; } 7 - - b * ++ d ++ (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix)right to left * / % left to right + -left to right Precedence and Associativity 7 - (-2) * 5 7 - (-10) 17 ) ( ) ( ) ( id2.c Lexical Elements Lexical Elements, Operators, and the C System

28 28 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples #include int main(void) { int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = 7 - - b* ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b* ++ d = %d\n",result2); return 0; } % gcc id1.c % a.out ++ a * b - c -- = 1 7 - - b* ++ d = 17 id2.c Lexical Elements Lexical Elements, Operators, and the C System

29 29 Operators and Punctuators — Increment ++ and Decrement Operators --  Semantics  Precedence and Associativity  Rules Lexical Elements Lexical Elements, Operators, and the C System

30 30 Operators and Punctuators — Increment ++ and Decrement Operators --  Rules  Applied to variables but not to constants or ordinary expressions Lexical Elements Lexical Elements, Operators, and the C System

31 31 Operators and Punctuators — Increment ++ and Decrement Operators --  Examples: #include int main(void) { int a, result1, result2; a = 1; result1 = ++1; result2 = -- -a; return 0; } % gcc id3.c id3.c: In function `main': id3.c:6: error: invalid lvalue in increment id3.c:7: error: invalid lvalue in decrement id3.c Lexical Elements Lexical Elements, Operators, and the C System

32 32 Operators and Punctuators — Increment ++ and Decrement Operators --  Summary  ++ ++i: the stored value of i is incremented; the expression takes as its value the new stored valued of i i++: the expression takes as its value the current stored valued of i; the stored value of i is incremented by 1.  -- --i: the stored value of i is decremented by 1; the expression takes as its value the new stored valued of i i--: the expression takes as its value the current stored valued of i; the stored value of i is decremented by 1.  Applied to variables but not to constants or ordinary expression Lexical Elements Lexical Elements, Operators, and the C System

33 33 Operators and Punctuators — Outline  Examples of Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators  Assignment Operators Lexical Elements Lexical Elements, Operators, and the C System

34 34 Operators and Punctuators — Assignment Operators  Example: An assignment expression with =  Format: variable = right_side Two operands ovariable oright_side: an expression Lexical Elements Lexical Elements, Operators, and the C System

35 35 Operators and Punctuators — Assignment Operators  Example: An assignment expression with =  Format: variable = right_side  Results: The value of right_side is assigned to variable Assignment expression variable = right_size has a value. oThe value of right_side is the value of the assignment expression.  Example: the value of c is 9 Assignment expression: a=2+(b=c+1) Lexical Elements Lexical Elements, Operators, and the C System

36 36 Operators and Punctuators — Assignment Operators #include int main(void) { int a, b, c; int a1, b1, c1; b = 2; c = 3; a = b + c; printf("b = %d, c = %d, a = %d \n",b, c, a); a1=(b1 = 2) + (c1 = 3); printf("b1 = %d, c1 = %d, a1 = %d \n",b1, c1, a1); return 0; } % gcc ass1.c % a.out b = 2, c = 3, a = 5 b1 = 2, c1 = 3, a1 = 5 Lexical Elements Lexical Elements, Operators, and the C System

37 37 Operators and Punctuators — Assignment Operators  Assignment operators  =  op=: +=, -=, *=, / =, %=, ……  Semantics:  variable op= expression equivalent to variable = variable op (expression)  Example: var*= expr  var=var * expr a *= 3  a = a * 3 Lexical Elements Lexical Elements, Operators, and the C System

38 38 Operators and Punctuators — Assignment Operators  Assignment operators  Precedence: all the assignment operators have the same precedence Lower than all the other operators which have been introduced (such as + - )  Associativity: right to left Lexical Elements Lexical Elements, Operators, and the C System

39 39 Operators and Punctuators — Assignment Operators #include int main(void) { int a, b, c; a = b = c = 0; printf("b = %d, c = %d, a = %d \n", b, c, a); return 0; } % gcc ass2.c % a.out b = 0, c = 0, a = 0 Lexical Elements Lexical Elements, Operators, and the C System

40 40 Operators and Punctuators — Assignment Operators #include int main(void) { int i=1, j=2, k=3, m=4; i += j + k; printf(" j = %d, k = %d, i += j+k = %d \n",j, k, i); printf(" m = %d, k = %d, ",m, k); j *= k = m + 5; printf("j *= k = m + 5 = %d \n",j); printf("k = %d \n",k); return 0; } % gcc ass3.c % a.out j = 2, k = 3, i += j+k = 6 m = 4, k = 3, j *= k = m + 5 = 18 k = 9 Lexical Elements Lexical Elements, Operators, and the C System

41 41 Operators and Punctuators — Assignment Operators  Summary  Assignment operators Precedence: they have the same precedence oLower than all the other operators which have been introduced (such as + - ) Associativity: right to left  variable op= expr  variable = variable op (expr) The value of the expression is the value of the expr Lexical Elements Lexical Elements, Operators, and the C System

42 42 Operators and Punctuators — Summary  Precedence and Associativity of Operators  Increment and Decrement Operators  i++, i++  i--, --i  Assignment Operators  Variable op= expression  Variable = variable op (expression) The value of the expression is the value of the expression Lexical Elements Lexical Elements, Operators, and the C System

43 43 Lexical Elements  Summary  Comments  Keywords  Identifiers  Constants  String Constants  Operators and Punctuators Lexical Elements, Operators, and the C System

44 44 Lexical Elements Comment  What is comment? Arbitrary strings of symbols placed between the delimiters /* and */. Single line comment: // text  The compiler changes each comment into a single black character.  Rules: Multi-line comments cannot be placed in the middle of a keyword or identifier. Multi-line comments may not be nested. Lexical Elements, Operators, and the C System Lexical Elements

45 45 Lexical Elements  Keywords  What is Keywords? Keywords are explicitly reserved words that have a strict meaning as individual tokens in C.  Examples of Keywords Data Type: int, char, long, short  Keywords cannot be redefined or used in other contexts. Lexical Elements, Operators, and the C System Lexical Elements

46 46 Lexical Elements  Identifiers  What is identifier? The names of variables, functions, labels and other user-defined items are called identifier.  Special identifier Keywords, names of functions in C library, main  Rules: 1.composed of letters, digits, and underscore _. 2.The first character must be a letter or underscore. 3.case-sensitive 4.would not be defined as the special identifiers: Lexical Elements, Operators, and the C System Lexical Elements

47 47 Lexical Element  Constants  Integer constants: 0, 17 Decimal integer: 17 Octal integer: 017 Hexadecimal integer: 0x17 An integer may be too large to be stored in a machine word.  Floating constants: double, float, long double  Character constants: (enclosed between single quotes) Lexical Elements Lexical Elements, Operators, and the C System

48 48 Lexical Elements  String Constants  String constant is a sequence of characters enclosed in a pair of double quote marks.  String constants are differently form character constants.  Special characters: \”, \\  You mustn't split a string constant across lines  Two string constants that are separated only by white space are concatenated by the compiler into a single string. Lexical Elements Lexical Elements, Operators, and the C System

49 49 Lexical Elements  Operators and Punctuators  Precedence and Associativity of Operators  Increment and Decrement Operators i++, i++ i--, --i  Assignment Operators Variable op= expression o  Variable = variable op (expression) oThe value of the expression is the value of the expression Lexical Elements Lexical Elements, Operators, and the C System

50 50 Outline  An Example — Characters and Lexical Elements  Lexical Elements  Comments  Keywords  Identifiers  Constants  String Constants  Operators and Punctuators  An Example: Computing Powers of 2  The C System Lexical Elements, Operators, and the C System

51 51 An Example: Computing Powers of 2 — Outline  Program pow_of_2  Dissection of the pow_of_2 Program Lexical Elements Comments Keywords Identifiers Constants String Constants Operators and Punctuators

52 52 Purpose: prints on a line some powers of 2. An Example: Computing Powers of 2 — power_of_2.c /* Some powers of 2 are printed. */ #include int main(void) { int e = 0, power_of_two = 1; while (++e <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0; } pow_of_2.c Comments Keywords Identifiers Constants String Constants Operators and Punctuators How many times the body of the loop is executed? power_of_two = power_of_two * 2

53 53 An Example: Computing Powers of 2 — power_of_2.c /* Some powers of 2 are printed. */ #include int main(void) { int exponent = 0, power_of_two = 1; while (++exponent <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0; } % gcc pow_of_2.c % a.out 2 4 8 16 32 64 128 256 512 1024 % Purpose: prints on a line some powers of 2. pow_of_2.c

54 54 Outline  An Example — Characters and Lexical Elements  Lexical Elements  Comments  Keywords  Identifiers  Constants  String Constants  Operators and Punctuators  An Example: Computing Powers of 2  The C System Lexical Elements, Operators, and the C System

55 55 The C System C is a small language  The core language is small  Non-essential functionality, such as math functions or file handling, is provided by a standardized set of library routines. The standard library contains many useful functions that add considerable power and flexibility to the c System. What is standard library? How to use a function in standard library?

56 56 The C System — The Standard Library  What is Standard library? (Contd.)  A collection of header files and library files.  Header file: The names and characteristics of functions are included into computer files called header file.  Library file: The actual implementation of functions are separated into a library file. The library contains compiled code that is unreadable to humans.

57 57 The C System — The Standard Library  Examples of header files:  : For computing common mathematical functions  : Provides the core input and output capabilities of the C language.  : For performing a variety of operations, including conversion, pseudo-random numbers, memory allocation, process control, environment, signaling, searching, and sorting.  : For manipulating several kinds of strings. Check Appendix A for Details

58 58 The C System — The Standard Library  How to use a function in the standard library? /*The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972.*/ #include int main(void) { printf(“Hello, world!\n”); return 0; }

59 59 The C System — The Standard Library  How to use a function in the standard library?  The programmer needs to provide the function prototype. Including appropriate header files.  Do we need to locate the function in the library file? No. The system knows where to find the code that corresponds to functions from the standard library.

60 60 The C System — The Standard Library  Steps to use a function in the standard library.  Find the header file which contains the prototype of the function.  Use #include preprocessing directive to include the appropriate file.

61 61 The C System — An example: prn_rand Program  Purpose of prn_rand program:  Use rand() to generate some randomly distributed integers.  Where is prototype of rand()?  stdlib.h

62 62 The C System — An example: prn_rand Program /*Printing random numbers. */ #include int main(void) { int i, n; printf("\n%s\n%s", "Some randomly distributed integers will be printed", "How many do you want to see? "); scanf("%d", &n); for(i=0;i<n;++i){ if (i%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0; } Hearder files are included i=0; while (i++<n){ …… }

63 63 The C System — An example: prn_rand Program /*Printing random numbers. */ #include int main(void) { int i, n; printf("\n%s\n%s", "Some randomly distributed integers will be printed", "How many do you want to see? "); scanf("%d", &n); for(i=0;i<n;++i){ if (i%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0; } % gcc prn_rand.c % a.out Some randomly distributed integers will be printed How many do you want to see? 17 16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 12060 32225 17543 25089 %

64 64 End of Chapter 2  An Example — Characters and Lexical Elements  Lexical Elements  Comments  Keywords  Identifiers  Constants  String Constants  Operators and Punctuators  An Example: Computing Powers of 2  The C System Lexical Elements, Operators, and the C System Read Chapter 2 Sections 2.1-2.12

65 65 End of Chapter 2


Download ppt "1 Structured Programming in C Welcome to CPSC 206."

Similar presentations


Ads by Google