1 / 34

Lexical Elements, Operators, and the C System

Lexical Elements, Operators, and the C System. Outline. Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String Constants. Outline (continued). Operators and Punctuations Precedence and Association of Operators Increment and Decrement Operators

Download Presentation

Lexical Elements, Operators, and the C System

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

  2. Outline • Characters and Lexical Elements • Syntax Rules • Comments • Keywords • Identifiers • Constants • String Constants

  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. 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. The Compilation Process C Program Group characters into token Translate tokens into target code

  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. /* Read in two integers and print their sum. */ #include <stdio.h> 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; } Comment line using /* */ Preprocessor directive which causes stdio.h to be included, in order to use printf() and scanf() Result Input two integers: 2 4 2 + 4 = 6

  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. 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. /* 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. Keywords • C has less reserved keywords auto do goto signed unsigned break double if sizeof void case else int static volatile char enum long struct while const extern register switch continue float return typedef default for short union

  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) not#me 101_south -plus add another k _id (not advised) kamanidentifiers so_am_i legal illegal

  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. Constants • Character constant • Integer constant • Floating number constant 'a', 'G', '\n', '\t', 43, -54, 3424 132.34, 13.23e+2

  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' illegal legal

  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. 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. Precedence and Associativity of Operators • Left-to-right rule as associativity rule 1+2-3+4-5 (((1+2)-3)+4)-5

  19. Operator precedence and associativity Operator associativity () ++(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. 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 ++i cnt-- ++int_variable -- int_ variable int_variable++ int_ variable-- 777++ ++(a*b-1) not:

  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*++d 7-((-b)*(++d))

  22. Assignment Operator • "=" is assignment operator variable=right_side (expr); b=2; c=3; a=b+c; a=b=c=0; a=(b=(c=0)); a=(b=2) + (c=3);

  23. Assignment Operator • Additional assignment operators += -= *= /= %= >>= <<= &= ^= |= variable op=expression variable =variable op expression j*=k+3 j=j*(k+3);

  24. Assignment Operator • More example • Power of 2 example i+=j+k i+=(j+k) i=i+(j+k) j*k=m+5 k*=(k=(m+5)) j=j*(k=(m+5))

  25. /* Some powers of 2 are printed. */ #include <stdio.h> int main(void) { int i = 0, power = 1; while (++i <= 10) printf("%-6d", power *= 2); printf("\n"); return 0; } 2 4 8 16 32 64 128 256 512 1024

  26. C System • Preprocessor using preprocessor directive # • #include #define #include <stdio.h> #incluse<ctype.h> #include "headerfile.h" Search in other (System) places Search in the same directory

  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. 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. #include <stdio.h> #include <stdlib.h> 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. Result: 16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 12060 32225 17543 25089 21183 25137 25566 26966 4978 20495 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. C System • Seed the random-number generator • Include <time.h> • Add the following line before using rand() function srand(time(NULL);

  32. #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int i, n, seed; /* seed=time(NULL); srand(seed); alternatively we can use the following line. */ srand(time(NULL));

  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. Execution 1: 3615 12261 17611 9250 27999 30399 22387 30095 19430 28749 18292 5300 27375 19186 31060 12893 17075 15879 8091 13940 21398 413 32213 Execution 2: 5433 14959 18114 23611 7140 17969 17481 13574 3022 11455 13012 13697 8305 28395 10077 12335 20728 28449 15022 12791 14511 20328 21935

More Related