220 likes | 228 Views
Learn about the lexical elements in C, including keywords, identifiers, constants, operators, and punctuators. Understand the syntax rules and precedence of operators in C.
E N D
Lecture 3 • Lexical elements • Some operators: • /, %, =, +=, ++, -- • precedence and associativity • #define • Readings:Chapter 2 Section 1 to 10.
Syntax of C • Like any language, C has an alphabet and rules for putting together words and punctuation to make legal program; that is called syntax of the language • C compilers detect any violation of the syntactic rule within the program • C compilers collect the characters of the program into tokens, which form the basic vocabulary of the language • Tokens are separated by space
Tokens • Tokens in C can be categorized into: • keywords, e.g., return, int • identifiers, e.g., user-defined variables, identifiers used in preprocessing statements and function names • character constants, e.g. ‘c’ • string constants, e.g., “Hello” • numeric constants, e.g., 7, 11, 3.14 • operators, e.g., = (assignment operator), ++ (increment operator) • punctuators, e.g.,; and ,
Keywords (cont’d) • There are only 32 keywords (C is small) • Each keyword in C has a reserved meaning and cannot be used as identifiers • Most of the keywords given in the previous page will be covered in this course; some keywords are deliberately omitted • Note: main is NOT a keyword. However, every program will use it as a function name. Therefore you should not use it as variable names.
Identifiers • An identifier is composed of a sequence of letters, digits and underscore • An identifier must begin with either a letter or an underscore (not recommended) • Identifiers give unique names to various objects in a program; reserved keywords such as float and int cannot be used as identifiers • In ANSI C, at least the first 31 characters of an identifier are discriminated • Always use meaningful names for identifiers!!!
Constants • Constants can be numeric-, character- or string-based, e.g., 13, 7.11, ‘\n’, “Tuesday” • Numbers are represented in decimal system but octal (preceded by 0) or hexadecimal integers (preceded by 0x) can also be represented, e.g., the following number are equal to a decimal 26 032 /* an octal integer */ 0x1a /* an hexadecimal integer */
Constants (cont’d) • Character constantsare enclosed by single quotation marks, e.g., ‘a’, ‘\n’, ‘\t’ • String constants are delimited by double quotes, e.g. “Hello world\n” • String is treated as an array of characters in C • If “ or newline characters are to be used within a string, it has to be preceded by a backslash \
Operators • Some types of operators: • Arithmetic, e.g. +, -, *, /, %, ++, -- • Assignment, e.g., = • Relational • Boolean (logical) and a few more… • Some symbols have meaning that depends on context, e.g., printf(“%d”, 40%7);
Punctuators • Punctuators such as , and ; are used to separate language elements, e.g., int a, b = 4, c = 4; a = b + c;
The / operators • The “division” operator, /, is used to compute the quotient when one number is divided by another • Example 1: float a = 20, b = 7, c; c = a/b; printf(“%f”, c); /* 2.857143 */ • Example 2: int a = 20, b = 7; float c; c = a/b; printf(“%f”, c); /* 2.000000 */
The % operators • The “modulus” operator, %, is used to compute the remainder when one integer is divided by another • Example: int a = 41, b = 7; printf(“%d”, a%b); /* 6 */ printf(“%d”, 49%b); /* 0 */ • Question: What is the value of -30%7 ? • Cannot be applied on floating point values
Expressions & statements • An expression is a meaningful combination of operators, variables and/or constants • E.g. a + b - 10 • Every expression has a value • A valid expression followed by a semicolon is a statement. • E.g. a + b - 10; (although this is not very useful) • There are other forms of statements. In general, a statement specifies some actions.
Assignment operator • The following is an assignment expression: a = 1 involving variablea, constant1 and the assignmentoperator = • The value of the above expression is defined as 1. • The expression followed by a semicolon: a = 1; is an assignment statement. • Further examples: a = (b=32) + (c=23); a = b = c = 0;
Efficient assignment operators • Generic form of efficient assignment operators variable op= expression; where op is operator; the meaning is variable = variable op (expression); • Efficient assignment operators include += -= *= /= %= >>= <<= &= ^= |=
Efficient assignment operators • Examples: a += 5; is same as a = a + 5; a -= 5; is same as a = a - 5; a += b*c; is same as a = a + (b*c); a *= b+c; is same as a = a * (b+c);
Increment & decrement operators • In terms of the actions done on variable k, • k++and++kare equivalent tok=k+1 • k--and--kare equivalent tok=k-1 • Post-increment and post-decrement: k++ and k-- • k’s value is altered AFTER evaluating the expression • int k=1, j; j=k++; /* result: j==1, k==2 */ • Pre-increment and pre-decrement: ++k and --k • k’s value is altered BEFORE evaluating the expression • int k=1, j=0; j=++k; /* result: j==2, k==2 */
Precedence & associativity of operators • An expression may have more than one operator and its precise meaning depends on the precedence and associativity of the involved operators • What is the value of variable d after executing each of the following statements int a = 1024, b = 8, c = 2, d; d = a*b+c; d = a–b–c; // (a-b)-c or a-(b-c)? d = a/b/c; // (a/b)/c or a/(b/c)?
Table of Precedence & Associativity • See p.397 of [Kelly & Pohl 2001] for a complete table with all operators in C.
Some good programming habits • Always use brackets ( ) to express your exact intention if you are not sure about the precedence/associativity • Try to avoid very complex expressions
Using #define directives • Two purposes: to define • symbolic constants, and • macros • The directives #define MAX_SCORE 100 causes all occurrences of MAX_SCORE in the C program to be replaced by 100 BEFORE the program is compiled. • It is a convention to name a symbolic constant with upper case letters.
Example use of #define /* To convert length in inches to cm */ #include <stdio.h> #define RATIO 2.54 int main(void) { float inches, cm; printf(“Enter length in inches: ”); scanf(“%f”, &inches); cm = RATIO*inches; printf(“approximately %f in cm\n”, cm); return 0; }