1 / 65

Welcome to CPSC 206

Welcome to CPSC 206. Structured Programming in C. Lecture Information. http://people.cs.tamu.edu/ychen/Teaching/CPSC206. 0. Introduction to Computer Science 1. Overview of C Ch 1, 2. Lecture Topics:. Features of C:. 2. Flow of control and functions Ch 3, 4

hashim-reed
Download Presentation

Welcome to CPSC 206

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome to CPSC 206 Structured Programming in C

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

  3. 0. Introduction to Computer Science 1. Overview of C Ch 1, 2 Lecture Topics: Features of C: 2. Flow of control and functions Ch 3, 4 3. Character processing & fundamental data types Ch 5, 6 4. File I/O Ch 13 5. Pointers, Arrays, and Strings Ch 8, 9, 10 6. Structures, and linked lists Ch 12 7. Enumeration type and storage classes Ch 7, 8 8. Recursion Ch 11

  4. Review of Class on Sept. 16, Thursday

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

  6. Lexical Elements, Operators, and the C System 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 How a computer understands this sequence of characters?

  7. Lexical Elements, Operators, and the C System 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.

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

  9. Lexical Elements, Operators, and the C System Lexical Elements 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.

  10. Lexical Elements, Operators, and the C System Lexical Elements 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.

  11. Lexical Elements, Operators, and the C System Lexical Elements 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: • composed of letters, digits, and underscore _ . • The first character must be a letter or underscore. • case-sensitive • would not be defined as the special identifiers:

  12. Lexical Elements, Operators, and the C System Lexical Element Lexical Elements • 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)

  13. Lexical Elements, Operators, and the C System Lexical Elements 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.

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

  15. Class on Sept 21

  16. Lexical Elements, Operators, and the C System 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

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

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

  19. Lexical Elements, Operators, and the C System Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements • 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 • the stored value of i is incremented first • the expression takes as its value the new stored value of i • i++ • the expression takes as its value the current stored value of i • the stored value of i is incremented

  20. Lexical Elements, Operators, and the C System Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements #include <stdio.h> 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; } id.c % gcc id.c % a.out a=1, b=0

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

  22. Lexical Elements, Operators, and the C System Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements • Decrement Operator i-- and --i • The value of i is decremented by 1. • Each expression has a value. • --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

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

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

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

  26. Lexical Elements, Operators, and the C System ) ) ) ) ( ( ( ( ) ) ( ( Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements ++ a * b – c -- • Examples ++ a * b – 3 #include <stdio.h> 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; } id2.c 2 * b – 3 4 – 3 1 • ++ (postfix) -- (postfix) Left to right • +(unary) –(unary) ++(prefix) --(prefix) right to left • * / % left to right • + - left to right • Precedence and Associativity

  27. Lexical Elements, Operators, and the C System ) ) ) ( ( ( Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements • Examples 7 - - b * ++ d #include <stdio.h> 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; } id2.c 7 - (-2) * 5 7 - (-10) 17 • ++ (postfix) -- (postfix) Left to right • +(unary) –(unary) ++(prefix) --(prefix) right to left • * / % left to right • + - left to right • Precedence and Associativity

  28. Lexical Elements, Operators, and the C System Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements • Examples #include <stdio.h> 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; } id2.c % gcc id1.c % a.out ++ a * b - c -- = 1 7 - - b* ++ d = 17

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

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

  31. Lexical Elements, Operators, and the C System Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements • Examples: id3.c #include <stdio.h> 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

  32. Lexical Elements, Operators, and the C System Operators and Punctuators— Increment ++ and Decrement Operators -- Lexical Elements • 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

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

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

  35. Lexical Elements, Operators, and the C System Operators and Punctuators— Assignment Operators Lexical Elements • 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. • The 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)

  36. Lexical Elements, Operators, and the C System Operators and Punctuators— Assignment Operators Lexical Elements % gcc ass1.c % a.out b = 2, c = 3, a = 5 b1 = 2, c1 = 3, a1 = 5 #include <stdio.h> 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; }

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

  38. Lexical Elements, Operators, and the C System Operators and Punctuators— Assignment Operators Lexical Elements • 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

  39. Lexical Elements, Operators, and the C System Operators and Punctuators— Assignment Operators Lexical Elements #include <stdio.h> 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

  40. Lexical Elements, Operators, and the C System Operators and Punctuators— Assignment Operators Lexical Elements #include <stdio.h> 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

  41. Lexical Elements, Operators, and the C System Operators and Punctuators— Assignment Operators Lexical Elements • Summary • Assignment operators • Precedence: they have the same precedence • Lower 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

  42. Lexical Elements, Operators, and the C System Operators and Punctuators— Summary Lexical Elements • 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

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

  44. Lexical Elements, Operators, and the C System Lexical Elements 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.

  45. Lexical Elements, Operators, and the C System Lexical Elements 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.

  46. Lexical Elements, Operators, and the C System Lexical Elements 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: • composed of letters, digits, and underscore _ . • The first character must be a letter or underscore. • case-sensitive • would not be defined as the special identifiers:

  47. Lexical Elements, Operators, and the C System Lexical Element Lexical Elements • 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)

  48. Lexical Elements, Operators, and the C System Lexical Elements 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.

  49. Lexical Elements, Operators, and the C System Lexical Elements Lexical Elements • Operators and Punctuators • 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

  50. Lexical Elements, Operators, and the C System 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

More Related