Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming...
-
Upload
elaine-warner -
Category
Documents
-
view
212 -
download
0
Transcript of Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming...
![Page 1: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/1.jpg)
Lexical Elements, Opera-tors,
and the C Cystem
![Page 2: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/2.jpg)
C overview recap
• functions– structured programming– return value is typed– arguments(parameters)
• pointerschar *p, s[100], **twod;
• filesifp = fopen(“my file”, “r”);
float minimum(float x, float y){ if (x < y) return x; else return y;}
![Page 3: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/3.jpg)
Syntax and Compiler
• The compiler under-stands some words and a simple grammar
• words– 32 keywords: if, int, return, .....– identifier; i, sum– constants: 1, 0, 5– string: “sum = %d\n”– operators: =, <=, +=, ++
• grammar– keyword identifier ;– while (expression) statement |
statements
#include <stdio.h>
int main(void){ int i = 1, sum = 0;
while (i <= 5) { sum += i; ++i; } printf("sum = %d\n", sum); return 0;}
C com-piler
a.out
![Page 4: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/4.jpg)
Compiler
• Task– generate a binary file from a source file
1. check if all the words are correct (lexical ana-lyzer)
2. check if the grammar is correct (syntax ana-lyzer)
3. optimization4. code generation
![Page 5: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/5.jpg)
Identifiers
• <identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>– underscore is a letter in C
• some identifiers are used in the library– printf, scanf, fopen, sqrt, pow, exp, sin, ..– _print
![Page 6: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/6.jpg)
Constants
• integer– 17 is a decimal– 017 is an octal– 0x1F is a hexadecimal
• floating– 1.0– 3.14
• character ‘a’, ‘7’ ‘+’, ‘\n’
![Page 7: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/7.jpg)
String Constant
• “any thing you can put here”
• special chatacter should be preceded by a backslash
• it’s an array of characters
• library functions for strings– strcat, strcmp, strcpy, strleng
![Page 8: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/8.jpg)
Operators
• Although C has very few keywords, it has a large number of operators
( ) [ ] -> ! ~ ++ -- + - * & (type) sizeof (right to left)* / %+ -<< >>< <= > >= == !=& ^ |&& || k = (n > 0) ? a : b; (right to left)= += -= *= /= (right to left)
![Page 9: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/9.jpg)
Precedence
• 1 + 2 – 3 + 4 – 5 /* left to right */• 1 + 2 * 3• (1 + 2 ) * 3• j *= k = m + 5 /* right to left */• k++-- /* left to right */• ++--k /* right to left */
![Page 10: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/10.jpg)
Increment and Decrement
• i++ and ++i are different
• Exercises
int a=1, b=2, c=3, d=4;
a*b/ca*b%c+1++a * b - c—7 - -b * ++d
![Page 11: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/11.jpg)
Strange Assignments
a = (b = 2) + (c =3);a = b = c = 0;
j *= k + 3;/* j = j*(k+3) OR j = j*k +3 */
j *= k = m + 5;
![Page 12: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/12.jpg)
/* 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;}
![Page 13: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/13.jpg)
C Environments
• header files– usually contain only function prototype, NOT
function code– then, where are the codes?– standard library is linked automatically– you should specify other libraries (math)
![Page 14: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/14.jpg)
#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;}
![Page 15: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/15.jpg)
gcc compiler
• gcc OR g++ are popular– C++ is a superset of C
• they call1. preprocessor2. compiler3. assembler4. linker
The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT - wikipedia
![Page 16: Lexical Elements, Operators, and the C Cystem. C overview recap functions –structured programming –return value is typed –arguments(parameters) pointers.](https://reader035.fdocuments.us/reader035/viewer/2022072006/56649f495503460f94c6aea6/html5/thumbnails/16.jpg)
gcc options
• perform parts of 4 steps• specify output files and format; “gcc xxx.c –o x”• -ansi OR specify differences• warning options “ –w”• debugging options “ –g “ “-ggdb” “-g3• optimization “-O0” “-O2”• preprocessor options• assembler options• linker options “-lm”• ......• read
http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc.pdf