Fall 2004
Advertisement
This presentation is the property of its rightful owner.
1 / 46

Fall 2004 SYEN 4385/4386 Computer Systems Senior Design Review of the C Language Instructor: Dr. Xian Liu PowerPoint PPT Presentation

Fall 2004 SYEN 4385/4386 Computer Systems Senior Design Review of the C Language Instructor: Dr. Xian Liu . Three Lists in Your C Book Precedence of operators Format specifiers Data types Any C books should include these lists . Table of Contents Section 1: Background

Download Presentation

Fall 2004 SYEN 4385/4386 Computer Systems Senior Design Review of the C Language Instructor: Dr. Xian Liu

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


C1 1 x liu 2004

Fall 2004

SYEN 4385/4386

Computer Systems Senior Design

Review of the C Language

Instructor: Dr. Xian Liu


C1 1 x liu 2004

  • Three Lists in Your C Book

  • Precedence of operators

  • Format specifiers

  • Data types

  • Any C books should include these lists


C1 1 x liu 2004

  • 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

  • 2.9 Math functions

  • 2.10 An example

  • 2.11 Compile and run a C code


C1 1 x liu 2004

Section 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


C1 1 x liu 2004

Section 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=(a>b)?a:b

x += 0.5; -=; *=; /=;

6.5 Global vars

6.6 Local vars

6.7 Static local vars


C1 1 x liu 2004

Section 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


C1 1 x liu 2004

Section 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


C1 1 x liu 2004

  • 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

  • Two parts: header file, obj code


C1 1 x liu 2004

  • 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


C1 1 x liu 2004

  • 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


C1 1 x liu 2004

  • 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


C1 1 x liu 2004

  • 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


C1 1 x liu 2004

  • 2.2 Basic Program Structure

  • /* comments */

  • # Preprocessor directives

  • /* e.g. #include <stdio.h> */

  • main()

  • {

  • /* comments */

  • Declaration statements;

  • /* comments */

  • Other C statements;

  • }


C1 1 x liu 2004

  • 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


C1 1 x liu 2004

  • 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*/ */


C1 1 x liu 2004

  • 2.5 Preprocessor Directive #include

  • Tells compiler to include the content of a file in your program

  • general formats

  • #include <filename>

  • - 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())


C1 1 x liu 2004

  • 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 = PI*r*r;

  • }


C1 1 x liu 2004

  • 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 */


C1 1 x liu 2004

  • 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;


C1 1 x liu 2004

  • 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;


C1 1 x liu 2004

  • 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 “+”


C1 1 x liu 2004

  • 2.9 Mathematical Functions

  • sin(), cos(), sqrt(), log(), exp(), log10(), pow()

  • to use them must have #include <math.h>

  • e.g.

  • /* calculate */

  • x = pow(y,4);


C1 1 x liu 2004

2.10 An Example

#include <stdio.h>

#include <math.h>

main(){

/* Declare and initialize variables. */

double x=2, 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);

}


C1 1 x liu 2004

- Puta 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 …… */

/* */

/* ============================================ */


C1 1 x liu 2004

  • 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


C1 1 x liu 2004

  • 3. Basic Input and Output

  • 3.1 printf()

  • printf is a library function

  • Need: #include <stdio.h>

  • 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.


C1 1 x liu 2004

- 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:

>output: 1.000000, 2.000000, 3.000000

>4

>


C1 1 x liu 2004

  • 3.2 scanf()

  • Need: #include <stdio.h>

  • 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


C1 1 x liu 2004

  • Example 2: read more than one number:

  • scanf("%f%f", &x3, &x4);

  • Example 3: read different types of data:

  • scanf("%d%d%f%f", &i, &j, &x1, &x2);


C1 1 x liu 2004

3.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 x=2, 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


C1 1 x liu 2004

  • 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 > my_output_file


C1 1 x liu 2004

  • 4. Control Flow

  • Expression, statement, and blocks

  • - An expression such as y=4 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

  • {

  • x=7; y=10; y=y+1;

  • x=y+6+x;

  • }


C1 1 x liu 2004

  • - Operators used in conditional expressions

  • Relational operators:

  • > greater than

  • >= greater or equal to

  • < less than

  • <= less or equal to

  • == equal to

  • != not equal to

  • Logical Operators

  • && AND

  • || OR

  • ! NOT


C1 1 x liu 2004

  • 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.


C1 1 x liu 2004

  • Example 1:

  • if(a >= b) c=1;

  • - means: if a is greater than or equal to b, then set c to 1.

  • Example 2:

  • if((a==b)&&(c>d))

  • { e=1; f=2;

  • g=2*f+e; }

  • - 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.


C1 1 x liu 2004

  • Example 3.

  • If (a==3) b=1;

  • means: if a is equal to 3, set b to 1

  • Example 4.

  • If (a=3) b=1;

  • result: b is always set to 1


C1 1 x liu 2004

  • 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.


C1 1 x liu 2004

  • 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; ...

  • }


C1 1 x liu 2004

  • 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


C1 1 x liu 2004

  • 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. i=i+1


C1 1 x liu 2004

  • Example 1:

  • i=1;

  • for(k=0; k<=2; k=k+1)

  • {i=i+7;

  • }

  • action k i new i

  • start 0 1 8

  • 1 8 15

  • 2 15 22

  • stop

  • Result: i = 22


C1 1 x liu 2004

  • Example 2:

  • i=1;

  • j=2;

  • for( k=3; k<5; k=k+1)

  • {i=i+1;

  • j=j+2;

  • }

  • action k i new i j new j

  • start 3 1 2 2 4

  • 4 2 3 4 6

  • 5

  • stop

  • Result: i =3, j =6


C1 1 x liu 2004

  • Example 3:

  • i=0;

  • for(k=5; k<0; k=k+1) i=i+1;

  • Action k i new i

  • start 5

  • stop

  • - Results: No loop! (k start with 5 which is not less than zero).


C1 1 x liu 2004

  • Example 4:

  • i=0;

  • for(k=5; k>0; k=k+1) i=i+1;

  • - Result: infinite loop! BAD!!! (k is always greater than zero)


C1 1 x liu 2004

  • The for Loop in C is more flexible than other languages

  • (1) You don’t have to use the loop-control variable to break the loop

  • k=5;

  • for (i=0; k!=7; i=i+1)

  • { k=k+1;

  • …… …… ……

  • }

  • Any of the 3 parts in the for loop may be omitted

  • The semicolons must remain

  • (2) expr1 can be omitted

  • i=50;

  • for( ; i; i--){

  • …… …… ……

  • }


C1 1 x liu 2004

  • (3) If expr2 is omitted, it means “always true”, resulting in an infinite loop

  • for(a=1; ;a=a+2)

  • {

  • …… …… ……

  • }

  • (4)

  • for(; ;)

  • {

  • …… …… ……

  • }

  • (5) You can update the loop-control variable inside the loop body

  • for(i=4; i<16;)

  • {

  • printf(“%d”, i);

  • i=i+1;

  • }


  • Login