Title: Lexical Elements, Operators, and the C System
1Lexical Elements, Operators, and the C System
2Outline
- Characters and Lexical Elements
- Syntax Rules
- Comments
- Keywords
- Identifiers
- Constants
- String Constants
3Outline (continued)
- Operators and Punctuations
- Precedence and Association of Operators
- Increment and Decrement Operators
- Assignment Operators
- Example
- The C System (preprocessor and standard library)
4Characters 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
5The Compilation Process
C Program
Group characters into token
Translate tokens into target code
6Characters 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 - / ( )
lt gt ' " !
\ , . / ? Space character
blank space, new-line, tab, etc.
7/ Read in two integers and print their sum.
/ include ltstdio.hgt int main(void) int
a, b, sum printf("Input two integers ")
scanf("dd", 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
8main 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
9Comments
- 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 lengthheight /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.
- /
11Keywords
- C has less reserved keywords
auto do goto signed unsigned break double if
sizeof void case else int static volatile c
har enum long struct while const extern regi
ster switch continue float return typedef def
ault for short union
12Identifier --- 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)
notme 101_south -plus add another
k _id (not advised) kamanidentifiers so_am_i
legal
illegal
13Identifier --- 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.
14Constants
- Character constant
- Integer constant
- Floating number constant
'a', 'G', '\n', '\t',
43, -54, 3424
132.34, 13.23e2
15String Constants
- string constants are identified by double quote "
" a string of text" "" " " "a bc" "/ 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
16Operators 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
17Precedence 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 (23)
1 23
(12)3
18Precedence and Associativity of Operators
- Left-to-right rule as associativity rule
12-34-5
(((12)-3)4)-5
19Operator 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
-ab-c ((-a)b)-c
20Increment 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 i
nt_ variable--
777 (ab-1)
not
21Increment and Decrement Operators
- The difference between postfix and prefix
int a,b,c0 ac bc printf(d d d\n",
a,b,c)
i i ii1
ab-c-- ((a)b)-(c--) 7bd 7-((-b)(d
))
22Assignment Operator
- "" is assignment operator
variableright_side (expr) b2 c3 abc
abc0
a(b(c0))
a(b2) (c3)
23Assignment Operator
- Additional assignment operators
- / gtgt ltlt
variable opexpression
variable variable op expression
jk3 jj(k3)
24Assignment Operator
- More example
- Power of 2 example
ijk i(jk) ii(jk) jkm5 k(k(m5))
jj(k(m5))
25/ Some powers of 2 are printed. / include
ltstdio.hgt int main(void) int i 0, power
1 while (i lt 10) printf("-6d",
power 2) printf("\n") return 0
2 4 8 16 32 64 128 256
512 1024
26C System
- Preprocessor using preprocessor directive
- include define
include ltstdio.hgt incluseltctype.hgt include
"headerfile.h"
Search in other (System) places
Search in the same directory
27C 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, )
28C 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
29include ltstdio.hgt include ltstdlib.hgt int
main(void) int i, n printf("\ns\ns",
"Some randomly distributed integers
will be printed.", "How many do you want to
see? ") scanf("d", n) for (i 0 i lt
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))
30Result
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.
31C System
- Seed the random-number generator
- Include lttime.hgt
- Add the following line before using rand()
function
srand(time(NULL)
32include ltstdio.hgt include ltstdlib.hgt include
lttime.hgt int main(void) int i, n,
seed / seedtime(NULL) srand(seed)
alternatively we can use the following line. /
srand(time(NULL))
33 printf("\ns\ns", "Some randomly
distributed integers will be printed.",
"How many do you want to see? ") scanf("d",
n) for (i 0 i lt n i) if (i
10 0) putchar('\n')
printf("7d", rand()) printf("\n\n")
return 0
34Execution 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