Title: Fall 2004
1Fall 2004 SYEN 4385/4386 Computer Systems
Senior Design Review of the C Language
Instructor Dr. Xian Liu
2- Three Lists in Your C Book
- Precedence of operators
- Format specifiers
- Data types
- Any C books should include these lists
3- Table of Contents
- Section 1 Background
-
- 1.1 Review of basics
- 1.2 Features of C
- 1.3 Evolution of C
- 1.4 Why learning C
-
- Section 2 Essentials of C
-
- 2.1 A Minimum C code
- 2.2 Basic program structure
- 2.3 Some important rules
- 2.4 Comments
- 2.5 include
- 2.6 define
- 2.7 Declaration and initialization of vars
- 2.8 Arithmetic ops
4Section 3 Basic I/O 3.1 printf() 3.2 scanf() 3.3
Formatted output 3.4 UNIX I/O redirection Section
4 Control Flow 4.1 if 4.2 if-else 4.3 if-else
if-else 4.4 for 4.5 Bit-wise ops 4.6 Basic file
I/O 4.7 and -- 4.8 K and K 4.9 K-- and
--K 4.10 while 4.11 do-while 4.12 break 4.13
continue 4.14 Nested loops 4.15 Case
5Section 5 Data Types 5.1 int 5.2 float 5.3
double 5.4 char 5.5 type casting Section 6
Functions 6.1 General format 6.2 An example 6.3
void 6.4 Features of C z(agtb)?ab x
0.5 - / 6.5 Global vars 6.6 Local
vars 6.7 Static local vars
6Section 7 Pointers and Arrays 7.1 Pointers and
addresses 7.2 Arrays --- 1D 7.3 Pointers and
function arguments --- Call by value --- Array
as arguments --- Alter arguments inside
functions 7.4 Pointers and arrays 7.5 Chars,
strings, and arrays 7.6 Character pointers and
functions 7.7 Arrays of pointers 7.8 Multiple
subscripted arrays 7.9 Pointers and multiple
subscripted arrays 7.10 Command-line
arguments 7.11 Pointer to functions
7Section 8 Structures 8.1 Intro 8.2 Structures
and functions 8.3 Arrays of structures 8.5
Sizeof 8.6 Self-referential 8.7 Unions 8.8 The
type cast operator Section 9 Dynamic Memory
Allocation --- calloc, malloc --- linked-list
8- 1. Background
- 1.1 Review of Basics
- Machine language object code
- Assembly language
- High level language source code
- Compiler
- Assembler
- The obj code is usually not immediately
executable - - Need other obj code, e.g., library
- A collection of routines
?
9- Linker combine your obj code to lib. obj code
- Result an executable thing
- Preprocessor part of compiler
- - Job combine header files with your source
code - Then do compilation
?
10- 1.2 Features of C
- Middle level language
- Initially used for system software
- - What is system software?
- Unix was almost totally rewritten in C
- - Pros
- - Cons
?
11- 1.3 Evolution of C
- Invented by Dennis Richie
- A standard book
- The C Language, by Brian Kernighan and Dennis
Richie (1978) - ANSI (American National Standard Institute) C
(1989) - Slightly modified in 1996
- 1.4 Why learning C?
- Nowadays C is widely used to develop application
software - - What is application software?
- It is easier to learn C or Java if you know C
already
12-
- 2. Essentials of C
- 2.1 A Minimum C Code
- Every C code must have one and only one main()
function - main()
-
-
- Your code starts executing things inside main
13-
- 2.2 Basic Program Structure
- / comments /
-
- Preprocessor directives
- / e.g. include ltstdio.hgt /
-
- main()
-
- / comments /
- Declaration statements
- / comments /
- Other C statements
-
14-
- 2.3 Some Important Rules
- - Rule 1 All statements must end with a
semi-colon "" -
- - Rule 2 Keywords, function names, and variable
names are case sensitive -
- e.g. include is not Include
-
- main() is not Main()
- image_flag is not Image_Flag
15-
- 2.4 Comments
- - Comments are enclosed by the "/" and "/" pair
-
- - Comments can be placed almost anywhere in a C
program - - Except in the middle of any C keywords,
function name or variable name - x/ comments /2 7.0
- - Nested comments are not allowed
- / comment1 / comment2/ /
16- 2.5 Preprocessor Directive include
-
- Tells compiler to include the content of a file
in your program -
- general formats
- include ltfilenamegt
- - Tells compiler to search for the file in
- a prearranged directory /usr/include/
- include "filename"
- - Tells compiler to search for the file first
in - the current working directory, and then in
the prearranged directory -
- Typical files to be include are header files
with suffix h -
- They contain information about the library
functions using in your program - e.g. stdio.h (to use scanf()and printf())
- math.h (to use sqrt())
17- 2.6 Preprocessor Directive define
- General format
- define string1 string2
- - Tells compiler to replace every subsequent
occurrence of "string 1 with "string2" -
- - define is mainly used for assigning symbolic
names to program constants - e.g. Want to evaluate area for a circle, the
equation is - define PI 3.14159
- main()
- float r, A
-
- r 10.0
- A PIrr
-
18- 2.7 Variable Declaration and Initialization
-
- All C variables must be declared before they can
be used - General form
- data_type var1, var2, var5
-
- Examples of data_type are
- int i,j / integers /
- float x,y / single precision real numbers
/ - double z / double precision real numbers /
19- variable name
- - letters of the alphabet a to z
- - digits 0 to 9
- - underscore _
- must start with either a letter or the
underscore - e.g. a_2, _bBc legal
- 2_a illegal
- case sensitive i.e. count is not Count
- variables can be initialized in the declaration
statement - e.g. float a 1.0
- statements can extent over several lines, but
must end with a semi-colon"" - e.g. float a 1.0, b 2.0, d 3.0, e
4.0, f 5.0, - g 3.0
20- 2.8 Arithmetic Operations
- Arithmetic operators for int, float, and double
variables - addition
- - subtraction
- multiplication
- / division
-
- e.g. The equation x 3y/z 2y is written in C
as - x 3 y / z 2 y
21- order of precedence
- - All operators have a priority. High priority
operators are evaluated before low priority ones.
Operators of the same priority are evaluated from
left to right. -
- - From high to low priority the order for some
C operators is - ()
- /
- -
-
- - e.g. a b c means (a b) c left to
right - - e.g. a b c means a (b c) has
higher priority than
22- 2.9 Mathematical Functions
- sin(), cos(), sqrt(), log(), exp(), log10(),
pow() - to use them must have include ltmath.hgt
- e.g.
-
- / calculate /
- x pow(y,4)
232.10 An Example include ltstdio.hgt include
ltmath.hgt main() / Declare and initialize
variables. / double x2, u, z u sqrt(x)
z sin(u) / Print result / printf("The
result of sin(sqrt(2)) is 15.10f \n", z)
printf("The result of sin(sqrt(2)) is 15.2f
\n", z) printf("The result of sin(sqrt(2)) is
-15.2f \n", z) printf("The result of
sin(sqrt(2)) is .10f \n", z) printf("The
result of sin(sqrt(2)) is f \n", z)
printf("The result of sin(sqrt(2)) is e \n",
z) printf("The result of sin(sqrt(2)) is E
\n", z) printf("The result of sin(sqrt(2)) is
g \n", z) printf("The result of sin(sqrt(2))
is G \n", z)
24- Put a header into your code in all following
assignments in C - Example
/
/ /
/ / Assignment 0
/ /
/ / Course SYEN 4385 Section
/ / Author Your Name
/ / Date 09/30/2004
/ /
/ / Program name foo_1.c
/ /
/ / Description This program shows
/ / the features that
/ /
/ /
/
25- 2.11 Compiling and Running the C Program
- To compile, do
- gcc sin_square.c -lm
- - Option -lm tells the computer to link the
math library - - An executable code, a.out, is automatically
created - - Then, type ./a.out
- Another way
- gcc sin_square.c -o sin_square -lm
- - This will result in an executable code,
sin_square - - Then, type ./ sin_square
26- 3. Basic Input and Output
- 3.1 printf()
- printf is a library function
- Need include ltstdio.hgt
- Display text and data values on standard output
- monitor - Example 1
- printf(Hello, me\n")
- Output on your monitor will be
- Hello, me
- \n is the newline character
- Example 2
- int i float a
- printf(d f, i, a)
- f is a format specifier (format code) which
tells computer that the data is a float number.
27- Example 3 display more than one variables
float x1 1.0, x2 2.0, x3 3.0 int i 4
printf(output f,f,",x1,x2)
printf("f\n",x3) printf("d\n",i) - Output
gtoutput 1.000000, 2.000000, 3.000000 gt4 gt
28- 3.2 scanf()
- Need include ltstdio.hgt
- Reads data from standard input (keyboard)
-
- Example 1 read one float number
-
- float x2
- scanf("f",x2)
-
- The symbol is C's address of operation x2
denotes the address of the variable x2 -
29- Example 2 read more than one number
- scanf("ff", x3, x4)
- Example 3 read different types of data
- scanf("ddff", i, j, x1, x2)
303.3 Formatted Output w.pf for decimal notation
w.pe for scientific notation wd for integers
- where w total field width p of digits
after the decimal point
double x2, u, z u sqrt(x) z sin(u)
printf("expression1 15.10f \n", z)
printf("expression2 15.2f \n", z)
printf("expression3 -15.2f \n", z)
printf("expression4 .10f \n", z)
printf("expression5 f \n", z)
printf("expression6 e \n", z)
printf("expression7 E \n", z)
printf("expression8 g \n", z)
printf("expression9 G \n", z) expression1
0.9877659460 expression2 0.99
expression3 0.99 expression4
0.9877659460 expression5 0.987766 expression6
9.877659e-01 expression7 9.877659E-01
expression8 0.987766 expression9 0.987766
31- 3.4 Linux I/O Redirection
- In Linux, you can redirect I/O from standard I/O
(keyboard and screen) to some data files. - Example
- ./a.out gt my_output_file
32- 4. Control Flow
- Expression, statement, and blocks
- - An expression such as y4 or printf()becomes a
statement when it is followed by a semicolon - - In C, the semicolon is a statement terminator,
rather than a separator - - Braces are used to group things together
into a compound statement, or block - - No semicolon after the right brace
-
- x7 y10 yy1
- xy6x
-
33- - Operators used in conditional expressions
-
- Relational operators
- gt greater than
- gt greater or equal to
- lt less than
- lt less or equal to
- equal to
- ! not equal to
-
- Logical Operators
- AND
34- 4.1 The if Statement
- General format
- if ( expression )
- statement 1
-
- statement n
- - expression can be any valid C expression
(conditional or math expressions). - - statements will be executed when expression is
evaluated to be non-zero. - - When there is only one statement, the braces
are not needed.
35- Example 1
- if(a gt b) c1
- - means if a is greater than or equal to b, then
set c to 1. -
- Example 2
- if((ab)(cgtd))
- e1 f2
- g2fe
-
- - means if a is equal to b and also c is greater
than d, then set e to 1 and set f to 2 next,
evaluate equation (2f e) and assign the value
to g.
36- Example 3.
- If (a3) b1
- means if a is equal to 3, set b to 1
-
- Example 4.
- If (a3) b1
- result b is always set to 1
37- 4.2 The if - else Structure
- General form
- if (expression )
- statement a1
- .
- .
- .
- statement aN
- else
- statement b1
- .
- .
- .
- statement bM
- If expression is evaluated to non-zero,
statement a1 to aN will be executed otherwise,
statement b1 to bM will be executed.
38- 4.3 The if - else if - else Structure
- General form
- if (expression_1 )
- statement 1a statement 1b ...
-
- else if ( expression_2 )
- statement 2a statement 2b ...
-
- .
- .
- .
- else if ( expression_N )
- statement Na statement Nb ...
-
- else
- statement a statement b ...
39- The if - else if - else Structure
- Starting from expression_1 if any of these
expression is evaluated to be nonzero, statements
belonging to that block will be executed - For example, if expression_1 is zero, but
expression_2 is nonzero, then statements 2a, 2b
... will be executed - If all expressions are zero, then the statements
belonging to else will be executed. - In C, nonzero means true
40- 4.4 The for Loop
- General Format
- for (initialization conditional test increment)
-
- statement_1
- .
- .
- .
-
- for (expr1 expr2 expr3)
-
-
- - Usually, expr3 updates the loop-control
variable, e.g. ii1
41- Example 1
- i1
- for(k0 klt2 kk1)
- ii7
-
- action k i new i
- start 0 1 8
- 1 8 15
- 2 15 22
- stop
- Result i 22
42- Example 2
- i1
- j2
- for( k3 klt5 kk1)
- ii1
- jj2
-
- action k i new i j new j
- start 3 1 2 2 4
- 4 2 3 4 6
- 5
- stop
43- Example 3
-
- i0
- for(k5 klt0 kk1) ii1
- Action k i new i
- start 5
- stop
- - Results No loop! (k start with 5 which is not
less than zero).
44- Example 4
- i0
- for(k5 kgt0 kk1) ii1
- - Result infinite loop! BAD!!! (k is always
greater than zero)
45- The for Loop in C is more flexible than other
languages - (1) You dont have to use the loop-control
variable to break the loop - k5
- for (i0 k!7 ii1)
- kk1
-
-
- Any of the 3 parts in the for loop may be
omitted - The semicolons must remain
- (2) expr1 can be omitted
-
- i50
- for( i i--)
-
-
46- (3) If expr2 is omitted, it means always true,
resulting in an infinite loop - for(a1 aa2)
-
-
-
- (4)
- for( )
-
-
-
-
- (5) You can update the loop-control variable
inside the loop body - for(i4 ilt16)
-
- printf(d, i)
- ii1
-