View
230
Download
5
Tags:
Embed Size (px)
Citation preview
1
Structured Programming in CStructured Programming in C
Welcome to CPSC 206Welcome to CPSC 206
2
Lecture Information
http://people.cs.tamu.edu/ychen/Teaching/CPSC206
3
Lecture Topics:
0. Introduction to Computer Science1. Overview of C Ch 1, 2
2. Flow of control and functions Ch 3, 43. Character processing & fundamental data types Ch 5, 64. File I/O Ch 135. Pointers, Arrays, and Strings Ch 8, 9, 106. Structures, and linked lists Ch 12
Features of C:Features of C:
7. Enumeration type and storage classes Ch 7, 88. Recursion Ch 11
4
Review of Class on Sept. 16, Thursday
5
Chapter 2:
Lexical Elements, Operators, and the C System
6
Lexical Elements, Operators, and the C System
C is a language Alphabet syntax
What is C program?A C program is a sequence of characters
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
How a computer understands this sequence of characters?
7
Introduction
How to check a C program is correct?The characters are collected by the
compiler into syntactic units called tokens
The compiler checks whether the tokens can be formed into legal strings according to the syntax of C language.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
8
Introduction
In C language, there are six kinds of tokens:
Keywords Identifiers Constants String constants Operators Punctuators
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
9
Lexical Elements
Comment What is comment?
Arbitrary strings of symbols placed between the delimiters /* and */.
Single line comment: // text The compiler changes each comment into a
single blank character. Rules:
Multi-line comments cannot be placed in the middle of a keyword or identifier.
Multi-line comments may not be nested.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
10
Lexical Elements
Keywords What is Keywords?
Keywords are explicitly reserved words that have a strict meaning as individual tokens in C.
Examples of KeywordsData Type: int, char, long, short
Keywords cannot be redefined or used in other contexts.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
11
Lexical Elements
Identifiers What is identifier?
The names of variables, functions, labels and other user-defined items are called identifier.
Special identifier Keywords, names of functions in C library,
main Rules:
1. composed of letters, digits, and underscore _ .2. The first character must be a letter or
underscore.3. case-sensitive4. would not be defined as the special identifiers:
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
12
Lexical Element
Constants Integer constants: 0, 17
Decimal integer: 17Octal integer: 017Hexadecimal integer: 0x17
Floating constants:double, float, long double
Character constants: (enclosed between single quotes)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
13
Lexical Elements
String Constants String constant is a sequence of characters
enclosed in a pair of double quote marks. String constants are differently form
character constants. Special characters: \”, \\ You mustn't split a string constant across
lines Two string constants that are separated
only by white space are concatenated by the compiler into a single string.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
14
Lexical Elements
Operators and Punctuator Precedence and Associativity determine
precisely how expressions are evaluated. Precedence of operators indicates when they when they
will be evaluatedwill be evaluated. Associativity
“left to right”: Operations are performed from left to right
o Examples: +,-,/,%“right to left”: Operations are performed from right
to lefto Examples: ++(prefix), --(prefix)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
15
Class on Sept 21
16
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
17
Operators and Punctuators— Outline
Examples of Operators and PunctuatorsPrecedence and Associativity of
OperatorsIncrement and Decrement OperatorsAssignment Operators
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
18
Operators and Punctuators — Increment ++ and Decrement Operators --
SemanticsPrecedence and AssociativityRules
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
19
Operators and Punctuators — Increment ++ and Decrement Operators --
Increment ++i, i++ Each causes the stored value of i in memory
to be incremented by 1. Each of the expressions ++i and i++ has a
value.++i
o the stored value of i is incremented firsto the expression takes as its value the new
stored value of ii++
o the expression takes as its value the current stored value of i
o the stored value of i is incremented
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
20
Operators and Punctuators — Increment ++ and Decrement Operators --
#include <stdio.h>int main(void){ int i, j, a, b; i=0; j=0; a = ++i; b = j++; printf("a=%d, b=%d\n",a,b); return 0;}
% gcc id.c% a.outa=1, b=0
id.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
21
Operators and Punctuators — Increment ++ and Decrement Operators --
++ and + ++
Cause the value of a variable in memory to be changed
+Does not change the value of a
variable.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
22
Operators and Punctuators — Increment ++ and Decrement Operators --
Decrement Operator i-- and --i The value of i is decremented by 1. Each expression has a value.
--io the stored value of i is decremented by 1o the expression takes as its value the new
stored valued of ii--
o the expression takes as its value the current stored valued of i
o the stored value of i is decremented by 1
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
23
Operators and Punctuators — Increment ++ and Decrement Operators --
SemanticsPrecedence and AssociativityRules
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
24
Operators and Punctuators — Increment ++ and Decrement Operators --
Precedence and Associativity
Associativity ++ (postfix) -- (postfix) Left to right +(unary) –(unary) ++(prefix) --(prefix) Right
to left * / % Left to right + - Left to right
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
25
Operators and Punctuators — Increment ++ and Decrement Operators --
Precedence and Associativity +(unary) –(unary) ++(prefix) --(prefix) Right to left
#include <stdio.h>int main(void){ int a=2; int result; result = - --a; printf("a=2, - --a = %d\n",result); return 0;}
% gcc id2.c% a.outa=2, - --a = -1
id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
Question1: What is the value of a after the operation --a?
Question2: What is the value of the expression --a?
26
Operators and Punctuators — Increment ++ and Decrement Operators --
Examples#include <stdio.h>int main(void){ int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a * b - c --; result2 = 7 - - b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b * ++ d = %d\n",result2); return 0;}
++ a * b – c --
++ (postfix) -- (postfix) Left to right+(unary) –(unary) ++(prefix) --(prefix) right to left* / % left to right+ - left to right
Precedence and Associativity
)()( )( ++ a * b – 3 2 * b – 3 4 – 3
1
)( )()(id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
27
Operators and Punctuators — Increment ++ and Decrement Operators --
Examples#include <stdio.h>int main(void){ int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = 7 - - b * ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b * ++ d = %d\n",result2); return 0;}
7 - - b * ++ d
++ (postfix) -- (postfix) Left to right+(unary) –(unary) ++(prefix) --(prefix) right to left* / % left to right+ - left to right
Precedence and Associativity
7 - (-2) * 5 7 - (-10) 17
)( )(
)(id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
28
Operators and Punctuators — Increment ++ and Decrement Operators --
Examples#include <stdio.h>int main(void){ int a=1, b=2, c=3, d=4; int result1, result2; result1 = ++ a*b - c --; result2 = 7 - - b* ++ d; printf("++ a * b – c -- = %d\n",result1); printf(“ 7 - - b* ++ d = %d\n",result2); return 0;}
% gcc id1.c% a.out++ a * b - c -- = 1 7 - - b* ++ d = 17
id2.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
29
Operators and Punctuators — Increment ++ and Decrement Operators --
SemanticsPrecedence and AssociativityRules
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
30
Operators and Punctuators — Increment ++ and Decrement Operators --
Rules Applied to variables but not
to constants or ordinary expressions
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
31
Operators and Punctuators— Increment ++ and Decrement Operators --
Examples:
#include <stdio.h>int main(void){ int a, result1, result2; a = 1; result1 = ++1; result2 = -- -a; return 0;}
% gcc id3.cid3.c: In function `main':id3.c:6: error: invalid lvalue in incrementid3.c:7: error: invalid lvalue in decrement
id3.c
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
32
Operators and Punctuators— Increment ++ and Decrement Operators --
Summary ++
++i: the stored value of i is incremented; the expression takes as its value the new stored valued of i
i++: the expression takes as its value the current stored valued of i; the stored value of i is incremented by 1.
-- --i: the stored value of i is decremented by 1; the
expression takes as its value the new stored valued of i i--: the expression takes as its value the current stored
valued of i; the stored value of i is decremented by 1. Applied to variables but not to constants or
ordinary expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
33
Operators and Punctuators— Outline
Examples of Operators and PunctuatorsPrecedence and Associativity of
OperatorsIncrement and Decrement OperatorsAssignment Operators
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
34
Operators and Punctuators— Assignment Operators
Example: An assignment expression with = Format: variable = right_side
Two operands o variable o right_side: an expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
35
Operators and Punctuators— Assignment Operators
Example: An assignment expression with = Format: variable = right_side Results:
The value of right_side is assigned to variable
Assignment expression variable = right_size has a value.
o The value of right_side is the value of the assignment expression.
Example: the value of c is 9
Assignment expression: a=2+(b=c+1)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
36
Operators and Punctuators— Assignment Operators
#include <stdio.h>int main(void){ int a, b, c; int a1, b1, c1; b = 2; c = 3; a = b + c; printf("b = %d, c = %d, a = %d \n",b, c, a);
a1=(b1 = 2) + (c1 = 3); printf("b1 = %d, c1 = %d, a1 = %d \n",b1, c1, a1);
return 0;}
% gcc ass1.c% a.outb = 2, c = 3, a = 5b1 = 2, c1 = 3, a1 = 5
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
37
Operators and Punctuators— Assignment Operators
Assignment operators = op=:
+=, -=, *=, / =, %=, ……
Semantics: variable op= expression
equivalent to variable = variable op (expression) Example:
var*= expr var=var * expra *= 3 a = a * 3
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
38
Operators and Punctuators— Assignment Operators
Assignment operators Precedence:
all the assignment operators have the same precedence
Lower than all the other operators which have been introduced (such as + - )
Associativity:right to left
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
39
Operators and Punctuators— Assignment Operators
#include <stdio.h>int main(void){ int a, b, c; a = b = c = 0; printf("b = %d, c = %d, a = %d \n", b, c, a); return 0;} % gcc ass2.c
% a.outb = 0, c = 0, a = 0
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
40
Operators and Punctuators— Assignment Operators
#include <stdio.h>int main(void){ int i=1, j=2, k=3, m=4; i += j + k; printf(" j = %d, k = %d, i += j+k = %d \n",j, k, i); printf(" m = %d, k = %d, ",m, k); j *= k = m + 5; printf("j *= k = m + 5 = %d \n",j); printf("k = %d \n",k); return 0;}
% gcc ass3.c% a.out j = 2, k = 3, i += j+k = 6 m = 4, k = 3, j *= k = m + 5 = 18 k = 9
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
41
Operators and Punctuators— Assignment Operators
Summary Assignment operators
Precedence: they have the same precedence
o Lower than all the other operators which have been introduced (such as + - )
Associativity: right to left variable op= expr
variable = variable op (expr)The value of the expression is the value
of the expr
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
42
Operators and Punctuators— Summary
Precedence and Associativity of OperatorsIncrement and Decrement Operators
i++, i++ i--, --i
Assignment Operators Variable op= expression
Variable = variable op (expression)The value of the expression is the value of
the expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
43
Lexical Elements
Summary Comments Keywords Identifiers Constants String Constants Operators and Punctuators
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
44
Lexical Elements
Comment What is comment?
Arbitrary strings of symbols placed between the delimiters /* and */.
Single line comment: // text The compiler changes each comment into a
single black character. Rules:
Multi-line comments cannot be placed in the middle of a keyword or identifier.
Multi-line comments may not be nested.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
45
Lexical Elements
Keywords What is Keywords?
Keywords are explicitly reserved words that have a strict meaning as individual tokens in C.
Examples of KeywordsData Type: int, char, long, short
Keywords cannot be redefined or used in other contexts.
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
46
Lexical Elements
Identifiers What is identifier?
The names of variables, functions, labels and other user-defined items are called identifier.
Special identifier Keywords, names of functions in C library,
main Rules:
1. composed of letters, digits, and underscore _ .2. The first character must be a letter or
underscore.3. case-sensitive4. would not be defined as the special identifiers:
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C SystemLexical Lexical ElementsElements
47
Lexical Element
Constants Integer constants: 0, 17
Decimal integer: 17Octal integer: 017Hexadecimal integer: 0x17An integer may be too large to be stored in
a machine word. Floating constants: double, float, long double Character constants: (enclosed between single
quotes)
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
48
Lexical Elements
String Constants String constant is a sequence of characters
enclosed in a pair of double quote marks. String constants are differently form
character constants. Special characters: \”, \\ You mustn't split a string constant across
lines Two string constants that are separated
only by white space are concatenated by the compiler into a single string.
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
49
Lexical Elements
Operators and Punctuators Precedence and Associativity of Operators Increment and Decrement Operators
i++, i++i--, --i
Assignment OperatorsVariable op= expression
Variable = variable op (expression)o The value of the expression is the value of the
expression
Lexical Lexical ElementsElements
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
50
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
51
An Example: Computing Powers of 2— Outline
Program pow_of_2 Dissection of the pow_of_2 Program
Lexical ElementsCommentsKeywordsIdentifiersConstantsString ConstantsOperators and Punctuators
52
Purpose: prints on a line some powers of 2.
An Example: Computing Powers of 2— power_of_2.c
/* Some powers of 2 are printed. */
#include <stdio.h>
int main(void){ int e = 0, power_of_two = 1;
while (++e <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0;}
pow_of_2.c
CommentsKeywordsIdentifiersConstantsString ConstantsOperators and Punctuators
How many times the body of the loop is executed?
power_of_two = power_of_two * 2
53
An Example: Computing Powers of 2— power_of_2.c
/* Some powers of 2 are printed. */
#include <stdio.h>
int main(void){ int exponent = 0, power_of_two = 1;
while (++exponent <= 10) printf("%5d", power_of_two *= 2); printf("\n"); return 0;}
% gcc pow_of_2.c% a.out 2 4 8 16 32 64 128 256 512 1024%
Purpose: prints on a line some powers of 2.
pow_of_2.c
54
Outline
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
55
The C System
C is a small language The core language is small Non-essential functionality, such as math functions
or file handling, is provided by a standardized set of library routines.The standard library contains many useful
functions that add considerable power and flexibility to the c System.
What is standard library?How to use a function in standard library?
56
The C System— The Standard Library
What is Standard library? (Contd.) A collection of header files and library files. Header file:
The names and characteristics of functions are included into computer files called header file.
Library file: The actual implementation of functions
are separated into a library file.The library contains compiled code that is
unreadable to humans.
57
The C System— The Standard Library
Examples of header files: <math.h>: For computing common mathematical
functions <stdio.h>: Provides the core input and output
capabilities of the C language. <stdlib.h>: For performing a variety of operations,
including conversion, pseudo-random numbers, memory allocation, process control, environment, signaling, searching, and sorting.
<string.h>: For manipulating several kinds of strings.
Check Appendix A for Details
58
The C System— The Standard Library
How to use a function in the standard library?
/*The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972.*/
#include <stdio.h>int main(void){ printf(“Hello, world!\n”); return 0;}
59
The C System— The Standard Library
How to use a function in the standard library? The programmer needs to provide the
function prototype.Including appropriate header files.
Do we need to locate the function in the library file? No. The system knows where to find the code
that corresponds to functions from the standard library.
60
The C System— The Standard Library
Steps to use a function in the standard library. Find the header file which contains the
prototype of the function. Use #include preprocessing directive to
include the appropriate file.
61
The C System— An example: prn_rand Program
Purpose of prn_rand program: Use rand() to generate some randomly
distributed integers.
Where is prototype of rand()? stdlib.h
62
The C System— An example: prn_rand Program/*Printing random numbers. */#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%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0;}
Hearder files are included
i=0;while (i++<n){……}
63
The C System— An example: prn_rand Program/*Printing random numbers. */#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%6==0) printf("\n"); printf("%9d", rand()); } printf("\n"); return 0;}
% gcc prn_rand.c% a.out
Some randomly distributed integers will be printedHow many do you want to see? 17
16838 5758 10113 17515 31051 5627 23010 7419 16212 4086 2749 12767 9084 12060 32225 17543 25089%
64
End of Chapter 2
An Example — Characters and Lexical ElementsLexical Elements
Comments Keywords Identifiers Constants String Constants Operators and Punctuators
An Example: Computing Powers of 2The C System
Lexical Elements, Operators, and the C SystemLexical Elements, Operators, and the C System
Read Chapter 2Sections 2.1-
2.12
65
End of Chapter 2