Download presentation
Presentation is loading. Please wait.
Published byOphelia Sherman Modified over 8 years ago
1
1 Lexical Elements, Operators, and the C System
2
2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String Constants
3
3 Outline (continued) Operators and Punctuations Precedence and Association of Operators Increment and Decrement Operators Assignment Operators Example The C System (preprocessor and standard library)
4
4 Characters and Lexical Elements Rule of program is called syntax The program that check the legality of C code is called the compiler Compiler collects the characters of the program into tokens Six tokens: keywords, identifiers, constants, string constant, operators, and punctuations
5
5 C Program Group characters into token Translate tokens into target code The Compilation Process
6
6 Characters and Lexical Elements Basic characters being used in a C program These characters are collected by the compiler into syntactic units called tokens. Lowercase: a b c … z Uppercase: A B C … Z Digits: 0 1 2 … 9 Others: + - * / ( ) { } [ ] ' " ! # $ % ^ ~ & | \ ; :,. / ? Space character: blank space, new-line, tab, etc.
7
7 /* Read in two integers and print their sum. */ #include int main(void) { int a, b, sum; printf("Input two integers: "); scanf("%d%d", &a, &b); sum = a + b; printf("%d + %d = %d\n", a, b, sum); return 0; } Input two integers: 2 4 2 + 4 = 6 Comment line using /* */ Preprocessor directive which causes stdio.h to be included, in order to use printf() and scanf() Result
8
8 main is identifier, as a function name; () is an operator; int is a keyword; {, }, ;, and, are punctuations; a,b,and sum are identifiers. printf and scanf are identifiers as function names " is punctuation Sequence characters in the double quotes is a string constant & is an operator for memory address = and + are operators, one for assignment and another one for arithmetic adding C compiler ignore white space
9
9 Comments Comments are not token. Compiler ignores the comments statement You can put comment statement in either blank line or white space after the C statement, such as Different comments area = length*height; /*calculate area of rectangle */
10
10 /* a comment */ /*** another comment ***/ /**********************/ /* * A comment can be written in this fashion * to set it off from the surrounding code. / /********************************* * If you wish, you can * * put comments in a box. * *********************************/
11
11 Keywords C has less reserved keywords autodogotosignedunsigned breakdoubleifsizeofvoid caseelseintstaticvolatile charenumlongstructwhile constexternregisterswitchcontinue floatreturntypedefdefaultfor shortunion
12
12 Identifier --- naming Identifier is a token It composed of a sequence of letters, digits, and underscore _ Identifier is case-sensitive (i.e., age is different from Age) k _id (not advised) kamanidentifiers so_am_i not#me 101_south -plus add another legal illegal
13
13 Identifier --- naming Be unique identifier No keyword can be used as identifier Size of identifier depends on systems (at least 31 in ANSI C). Always choose meaningful identifier for naming variables, such as tax_rate, price, … Underscore is used to create a single name, but has the meaning of word, such as C_class_student.
14
14 Constants Character constant Integer constant Floating number constant 'a', 'G', '\n', '\t', 43, -54, 3424 132.34, 13.23e+2
15
15 String Constants string constants are identified by double quote " " a string of text" "" " "a = b+c;" "/* this is not a comment */" "a string with double quotes \" within" "a single backslash \\ is in this string" "abd" "efg" /* "this is not a string" */ "and Neither is this " 'dgashgahg' legal illegal
16
16 Operators and Punctuators Arithmetic operators ( + - * / %) + and – has four meanings; one is as arithmetic operator for addition, the second is for changing the sign of value, such as c= -a – b; The third one is used as increment or decrement operator such as ++ and – The fourth one is used as additional operator
17
17 Precedence and Associativity of Operators Operators have rules of precedence and associativity that are used to determine how expressions are calculated and what is the order of operations Example 1+ 2*3 1 +(2*3) (1+2)*3
18
18 Precedence and Associativity of Operators Left-to-right rule as associativity rule 1+2-3+4-5(((1+2)-3)+4)-5
19
19 Operator precedence and associativity Operatorassociativity () ++(postfix) --(postfix)left to right + (unary) – (unary) ++ (prefix) – (prefix) right to left * / %left to right + -left to right = += -= *= /= %= etc.Right to left -a*b-c((-a)*b)-c
20
20 Increment and Decrement Operators Increment and decrement by 1 They are unary operators They both can be used as both postfix and prefix They have higher precedence ++int_variable-- int_ variable int_variable++int_ variable-- ++i cnt-- 777++ ++(a*b-1) not:
21
21 Increment and Decrement Operators The difference between postfix and prefix int a,b,c=0; a=++c; b=c++; printf(%d %d %d\n", a,b,++c); ++i; i++; i=i+1; ++a*b-c--((++a)*b)-(c--) 7—b*++d7-((-b)*(++d))
22
22 Assignment Operator "=" is assignment operator variable=right_side (expr); b=2; c=3; a=b+c; a=(b=2) + (c=3); a=b=c=0; a=(b=(c=0));
23
23 Assignment Operator Additional assignment operators +=-=*=/=%= >>=<<=&=^= |= variable op=expressionvariable =variable op expression j*=k+3j=j*(k+3);
24
24 Assignment Operator More example Power of 2 example i+=j+ki+=(j+k)i=i+(j+k) j*k=m+5k*=(k=(m+5))j=j*(k=(m+5))
25
25 2 4 8 16 32 64 128 256 512 1024 /* Some powers of 2 are printed. */ #include int main(void) { int i = 0, power = 1; while (++i <= 10) printf("%-6d", power *= 2); printf("\n"); return 0; }
26
26 C System Preprocessor using preprocessor directive # – #include#define #include #incluse #include "headerfile.h" Search in other (System) places Search in the same directory
27
27 C System Usually, header file contains function definitions On UNIX, the header files are located in the directory /usr/include On Borland C, it may be found in c:\bc\include Put function prototypes or function registration in the header file int printf(const char * format, … ); int scanf(const char *format, …);
28
28 C System Standard library – C has many useful built-in and useful functions – The collection of functions is called "library" – Example of using functions built in the existing library
29
29 #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 % 10 == 0) putchar('\n'); printf("%7d", rand()); } printf("\n\n"); return 0; } Width of field for print is 7 Needs srand(time(Null));
30
30 16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 12060 32225 17543 25089 21183 25137 25566 26966 4978 20495 Result: If you run the code again, you will get the same result 16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 12060 32225 17543 25089 21183 25137 25566 26966 4978 20495 Why? We have not generate seed yet.
31
31 C System Seed the random-number generator Include Add the following line before using rand() function srand(time(NULL);
32
32 #include int main(void) { int i, n, seed; /* seed=time(NULL); srand(seed); alternatively we can use the following line. */ srand(time(NULL));
33
33 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 % 10 == 0) putchar('\n'); printf("%7d", rand()); } printf("\n\n"); return 0; }
34
34 5433 14959 18114 23611 7140 17969 17481 13574 3022 11455 13012 13697 8305 28395 10077 12335 20728 28449 15022 12791 14511 20328 21935 3615 12261 17611 9250 27999 30399 22387 30095 19430 28749 18292 5300 27375 19186 31060 12893 17075 15879 8091 13940 21398 413 32213 Execution 1: Execution 2:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.