You are on page 1of 63
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial

C/C++ Tutorial

C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
CSU480 CSU480
CSU480 CSU480
CSU480 CSU480

CSU480

CSU480

C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480
C/C++ Tutorial CSU480 CSU480

Who is the TA?

Name: Jingjing

Name:

Jingjing Duan

Duan

Office

Office Hours:

Hours: (might

(might change)

change)

Tue && Fri

Tue

Fri 2:00-4:00

2:00-4:00

Office:

Office: Room

Room 266,

266, WVH

WVH

Email:

Class web:

Class

web: www.ccs.neu.edu/course/csu480

www.ccs.neu.edu/course/csu480

Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline

Outline

 
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
 “ “ Hello World" Program

Hello Hello World" World" Program Program

 Data Types & & Variables

Data Data Types Types && Variables Variables

Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
 printf() printf()

printf() printf()

 
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
 Arithmetic & & Logical

Arithmetic

Arithmetic && Logical

Logical

Operations Operations

Operations Operations

 
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
 Conditionals Conditionals

Conditionals Conditionals

Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
 Loops Loops

Loops Loops

 Arrays & & Strings
 Arrays & & Strings

Arrays Arrays && Strings Strings

Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()
Outline  “ “ Hello World" Program  Data Types & & Variables  printf() printf()

Pointers Pointers

Functions Functions

Command-Line Command-Line Argument Argument

Data Data Structure Structure

Memory Memory Allocation Allocation

Programming Programming Tips Tips

CC vvs.s. C++ C++

Books Books recommended recommended

Hello World Program

The

The source

source code

code

#include <stdio.h>

#include

<stdio.h>

int main()

int

main()

{{

}}

printf("Hello World\n");

printf("Hello

World\n");

return(0);

return(0);

Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program

Hello World Program

Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
 How to to compile? $$ gcc hello.c –o –o hello
 How to to compile? $$ gcc hello.c –o –o hello
 How to to compile? $$ gcc hello.c –o –o hello
 How to to compile? $$ gcc hello.c –o –o hello
 How to to compile? $$ gcc hello.c –o –o hello

How

How toto compile?

compile?

$$ gcc

gcc hello.c

hello.c –o–o hello

hello

gcc gcc compiling command compiling command
gcc gcc compiling command compiling command
gcc gcc compiling command compiling command

gcc

gcc

compiling command

compiling

command

Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
 
hello.c hello.c source file source file
hello.c hello.c source file source file

hello.c

hello.c

source file

source

file

hello hello compiler-generated executable compiler-generated executable file file
hello hello compiler-generated executable compiler-generated executable file file
hello hello compiler-generated executable compiler-generated executable file file

hello

hello

compiler-generated executable

compiler-generated

executable file

file

Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
the Note: default output filename isis ““ a.out
the Note: default output filename isis ““ a.out
the Note: default output filename isis ““ a.out

Note: the

Note:

the default

default output

output filename

filename isis ““a.out

a.out

Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to compile? $$ gcc hello.c –o –o hello gcc gcc
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program

Hello World Program

Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
 How to to execute ./hello ./hello
 How to to execute ./hello ./hello
 How to to execute ./hello ./hello
 How to to execute ./hello ./hello
 How to to execute ./hello ./hello

How

How toto execute

execute??

./hello

./hello

Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
““././ ”” indicates the following file “hello” resides under the current directory. current directory.
““././ ”” indicates the following file “hello” resides under the current directory. current directory.
““././ ”” indicates the following file “hello” resides under the current directory. current directory.
““././ ”” indicates the following file “hello” resides under the current directory. current directory.
““././ ”” indicates the following file “hello” resides under the current directory. current directory.

““././ ”” indicates

indicates the

the following

following file

file “hello”

hello resides

resides under

under the

the

current directory.

current

directory.

Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
why Q: “.” is is not included in in $PATH environment variable? environment variable?
why Q: “.” is is not included in in $PATH environment variable? environment variable?
why Q: “.” is is not included in in $PATH environment variable? environment variable?
why Q: “.” is is not included in in $PATH environment variable? environment variable?
why Q: “.” is is not included in in $PATH environment variable? environment variable?

Q: why

Q:

why “.”

. isis not

not included

included inin $PATH

$PATH

environment variable?

environment

variable?

Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program  How to to execute ./hello ./hello ““././ ”” indicates the following file
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program

Hello World Program

 
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
A: security A: security consideration. consideration.
A: security A: security consideration. consideration.
A: security A: security consideration. consideration.

A: security

A:

security consideration.

consideration.

 
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Command Command Location Location Comment Comment
Command Command Location Location Comment Comment
Command Command Location Location Comment Comment

Command

Command

Location

Location

Comment

Comment

lsls /bin/ls /bin/ls byby the provided system system
lsls /bin/ls /bin/ls byby the provided system system
lsls /bin/ls /bin/ls byby the provided system system
lsls /bin/ls /bin/ls byby the provided system system

lsls

/bin/ls

/bin/ls

provided byby the

provided

the

system system

Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
   
lsls current directory current directory virus virus
lsls current directory current directory virus virus

lsls

current directory

current

directory

virus

virus

Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
   
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Hello World Program A: security A: security consideration. consideration. Command Command Location Location Comment Comment lsls
Data types Name Name Description Description Size* Size* Range* Range* char char oror small Character integer
Data types

Data types

Data types Name Name Description Description Size* Size* Range* Range* char char oror small Character integer
Data types Name Name Description Description Size* Size* Range* Range* char char oror small Character integer
Name Name Description Description Size* Size* Range* Range*
Name Name Description Description Size* Size* Range* Range*

Name

Name

Description

Description

Size*

Size*

Range*

Range*

char char oror small Character integer integer 11 byte signed: -128 toto 127 unsigned: 00 toto
char char oror small Character integer integer 11 byte signed: -128 toto 127 unsigned: 00 toto
char char oror small Character integer integer 11 byte signed: -128 toto 127 unsigned: 00 toto

char

char

Character oror small

Character

small

integer integer

11 byte

byte

signed:

signed: -128

-128 toto 127

127

unsigned:

unsigned: 00 toto 255

255

short int short int (short) (short) Short integer Short integer 22 signed: bytes -32768 toto 32767
short int short int (short) (short) Short integer Short integer 22 signed: bytes -32768 toto 32767
short int short int (short) (short) Short integer Short integer 22 signed: bytes -32768 toto 32767

short int

short

int

(short)

(short)

Short integer

Short

integer

  • 22 signed:

bytes

bytes

signed: -32768

-32768 toto 32767

32767

unsigned:

unsigned: 00 toto 65535

65535

int int Integer Integer 44 signed: bytes -2147483648 toto 2147483647 2147483647 unsigned: 00 toto 4294967295
int int Integer Integer 44 signed: bytes -2147483648 toto 2147483647 2147483647 unsigned: 00 toto 4294967295
int int Integer Integer 44 signed: bytes -2147483648 toto 2147483647 2147483647 unsigned: 00 toto 4294967295
int int Integer Integer 44 signed: bytes -2147483648 toto 2147483647 2147483647 unsigned: 00 toto 4294967295

int

int

Integer

Integer

  • 44 signed:

bytes

bytes

signed: -2147483648

-2147483648 toto

2147483647

2147483647

unsigned:

unsigned: 00 toto 4294967295

4294967295

long int long int (long) (long) Long integer Long integer 44 signed: bytes -2147483648 toto 2147483647
long int long int (long) (long) Long integer Long integer 44 signed: bytes -2147483648 toto 2147483647
long int long int (long) (long) Long integer Long integer 44 signed: bytes -2147483648 toto 2147483647
long int long int (long) (long) Long integer Long integer 44 signed: bytes -2147483648 toto 2147483647

long int

long

int

(long)

(long)

Long integer

Long

integer

  • 44 signed:

bytes

bytes

signed: -2147483648

-2147483648 toto

2147483647

2147483647

unsigned:

unsigned: 00 toto 4294967295

4294967295

float float Floating point Floating point number number bytes 44 3.4e +/- 3838 (7(7 digits)
float float Floating point Floating point number number bytes 44 3.4e +/- 3838 (7(7 digits)

float

float

Floating point

Floating

point

number

number

bytes

  • 44 3.4e

bytes

3.4e +/-

+/- 3838 (7(7 digits)

digits)

Data types Name Name Description Description Size* Size* Range* Range* char char oror small Character integer
double double Double precision Double precision floating point floating point number number bytes 88 1.7e 1.7e
double double Double precision Double precision floating point floating point number number bytes 88 1.7e 1.7e

double

double

Double precision

Double

precision

floating point

floating

point number

number

bytes

  • 88 1.7e

bytes

1.7e +/-

+/- 308

308 (15

(15 digits)

digits)

long long double double Long double Long double precision floating precision floating point number point number
long long double double Long double Long double precision floating precision floating point number point number
long long double double Long double Long double precision floating precision floating point number point number
long long double double Long double Long double precision floating precision floating point number point number

long

long

double

double

Long double

Long

double

precision floating

precision

floating

point number

point

number

bytes

  • 88 1.7e

bytes

1.7e +/-

+/- 308

308 (15

(15 digits)

digits)

Variable

Variable Declaration

Declaration

int length

int

length == 100;

100;

char

char num

num == ‘9’;

9; //The

//The actual

actual value

value isis 5757

float deposit

float

deposit == 240.5;

240.5;

unsigned short

unsigned

short IDID == 0x5544;

0x5544;

Try the

Try

the following

following statements,

statements, and

and see

see what

what happens

happens

unsigned char

unsigned

char value

value == -1;

-1;

printf(The value

printf(“The

value isis %d

%d \n”,

\n, value);

value);

unsigned char

unsigned

char value

value == 300;

300;

printf(The value

printf(“The

value isis %d

%d \n”,

\n, value);

value);

Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result

Result

 
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
 
       
Definition Definition Memory layout Memory layout Display Display comment comment
Definition Definition Memory layout Memory layout Display Display comment comment

Definition

Definition

Memory layout

Memory

layout

Display

Display

comment

comment

unsigned char unsigned char value == -1-1 11111111 11111111 255 255
unsigned char unsigned char value == -1-1 11111111 11111111 255 255
unsigned char unsigned char value == -1-1 11111111 11111111 255 255

unsigned char

unsigned

char

value

value == -1-1

11111111

11111111

255

255

 
unsigned char unsigned char value == 300 00101100 00101100 4444 overflow overflow
unsigned char unsigned char value == 300 00101100 00101100 4444 overflow overflow
unsigned char unsigned char value == 300 00101100 00101100 4444 overflow overflow

unsigned char

unsigned

char

value

value == 300

300

00101100

00101100

4444

overflow

overflow

Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Result Definition Definition Memory layout Memory layout Display Display comment comment unsigned char unsigned char value
Variable types  Local variable Local variable variables Local are declared within the body of aa
Variable types  Local variable Local variable variables Local are declared within the body of aa
Variable types  Local variable Local variable variables Local are declared within the body of aa
Variable types

Variable types

Variable types  Local variable Local variable variables Local are declared within the body of aa
Variable types  Local variable Local variable variables Local are declared within the body of aa
Variable types  Local variable Local variable variables Local are declared within the body of aa
 Local variable Local variable variables Local are declared within the body of aa function, and
 Local variable Local variable variables Local are declared within the body of aa function, and
 Local variable Local variable variables Local are declared within the body of aa function, and
 Local variable Local variable variables Local are declared within the body of aa function, and
 Local variable Local variable variables Local are declared within the body of aa function, and
 Local variable Local variable variables Local are declared within the body of aa function, and

Local variable

Local

variable

Local variables

Local

variables are

are declared

declared within

within the

the body

body of

of aa function,

function, and

and can

can

only bebe used

only

used within

within that

that function.

function.

 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis
 Static variable Static variable class Another of local variable isis the static type. ItIt isis

Static variable

Static

variable

Another class

Another

class of

of local

local variable

variable isis the

the static

static type.

type. ItIt isis specified

specified byby the

the

keyword static

keyword

static inin the

the variable

variable declaration.

declaration.

The

The most

most striking

striking difference

difference from

from aa non-static

non-static local

local variable

variable is,

is, aa static

static

variable isis not

variable

not destroyed

destroyed onon exit

exit from

from the

the function.

function.

 Global variable Global variable AA global variable declaration looks normal, but isis located outside any
 Global variable Global variable AA global variable declaration looks normal, but isis located outside any
 Global variable Global variable AA global variable declaration looks normal, but isis located outside any
 Global variable Global variable AA global variable declaration looks normal, but isis located outside any
 Global variable Global variable AA global variable declaration looks normal, but isis located outside any
 Global variable Global variable AA global variable declaration looks normal, but isis located outside any

Global variable

Global

variable

AA global

global variable

variable declaration

declaration looks

looks normal,

normal, but

but isis located

located outside

outside any

any

ofof the

the program's

program's functions.

functions. SoSo itit isis accessible

accessible toto all

all functions.

functions.

Variable types  Local variable Local variable variables Local are declared within the body of aa
 An example An example global int == 10; variable //global int func (int x)x) {{
 An example An example global int == 10; variable //global int func (int x)x) {{
 An example An example global int == 10; variable //global int func (int x)x) {{
 An example An example
 An example An example
 An example An example

An example

An

example

 An example An example global int == 10; variable //global int func (int x)x) {{
global int == 10; variable //global
global int == 10; variable //global
global int == 10; variable //global

int global

int

global == 10;

10;

//global variable

//global

variable

 An example An example global int == 10; variable //global int func (int x)x) {{
int func (int x)x) {{
int func (int x)x) {{
int func (int x)x) {{

int

int func

func (int

(int x)x)

{{

 An example An example global int == 10; variable //global int func (int x)x) {{
static int static int stat_var; stat_var; int int temp; temp; int int name[50]; name[50]; //static local
static int static int stat_var; stat_var; int int temp; temp; int int name[50]; name[50]; //static local
static int static int stat_var; stat_var; int int temp; temp; int int name[50]; name[50]; //static local
static int static int stat_var; stat_var; int int temp; temp; int int name[50]; name[50]; //static local
static int static int stat_var; stat_var; int int temp; temp; int int name[50]; name[50]; //static local
static int static int stat_var; stat_var; int int temp; temp; int int name[50]; name[50]; //static local

static int

static

int stat_var;

stat_var;

int

int temp;

temp;

int

int name[50];

name[50];

//static local

//static

local variable

variable

//(normal) local

//(normal)

local variable

variable

//(normal) local

//(normal)

local variable

variable

…………

…………

 An example An example global int == 10; variable //global int func (int x)x) {{
}}

}}

 An example An example global int == 10; variable //global int func (int x)x) {{
 An example An example global int == 10; variable //global int func (int x)x) {{
 An example An example global int == 10; variable //global int func (int x)x) {{
 An example An example global int == 10; variable //global int func (int x)x) {{
 An example An example global int == 10; variable //global int func (int x)x) {{
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration

Variable Definition vs Declaration

Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Definition Definition the Tell compiler about the variable: itsits type and asas well name, asas allocated
Definition Definition the Tell compiler about the variable: itsits type and asas well name, asas allocated
Definition Definition the Tell compiler about the variable: itsits type and asas well name, asas allocated
Definition Definition the Tell compiler about the variable: itsits type and asas well name, asas allocated

Definition

Definition

Tell the

Tell

the compiler

compiler about

about the

the variable:

variable: itsits type

type and

and

name, asas well

name,

well asas allocated

allocated aa memory

memory cell

cell for

for the

the

variable variable

Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Declaration Declaration DDescribe escribe information information ``about'' ``about'' the the variable variable,, doesn’t allocate doesn’t allocate
Declaration Declaration DDescribe escribe information information ``about'' ``about'' the the variable variable,, doesn’t allocate doesn’t allocate
Declaration Declaration DDescribe escribe information information ``about'' ``about'' the the variable variable,, doesn’t allocate doesn’t allocate
Declaration Declaration DDescribe escribe information information ``about'' ``about'' the the variable variable,, doesn’t allocate doesn’t allocate

Declaration

Declaration

DDescribe

escribe information

information ``about''

``about'' the

the variable

variable,,

doesn’t allocate

doesn’t

allocate memory

memory cell

cell for

for the

the variable

variable

Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
Variable Definition vs Declaration Definition Definition the Tell compiler about the variable: itsits type and asas
http://www-ee.eng.hawaii.edu/~tep/EE150/book/chap14/subsection2.1.1.4.html
http://www-ee.eng.hawaii.edu/~tep/EE150/book/chap14/subsection2.1.1.4.html

printf()

The printf()

The

printf() function

function can

can bebe instructed

instructed toto print

print

integers, floats

integers,

floats and

and string

string properly.

properly.

The

The general

general syntax

syntax isis

printf(( “format”,

printf

“format”, variables);

variables);

An

An example

example

int stud_id

int

stud_id == 5200;

5200;

char ** name

char

name == “Mike”;

Mike;

printf((%s

printf

%s ‘s‘s IDID isis %d

%d \n”,

\n, name,

name, stud_id);

stud_id);

Format

Format Identifiers

Identifiers

%d %d decimal decimal integers integers

%x %x hex hex integer integer

%c %c character character

%f %f float float and and double double number number

%s %s string string

%p %p pointer pointer

How toto specify

How

specify display

display space

space for

for aa variable

variable??

printf(The student

printf(“The

student idid isis %%55dd \n”,

\n, stud_id);

stud_id);

The value

The

value ofof stud_id

stud_id will

will occupy

occupy 55 characters

characters space

space inin the

the

print-out.

print-out.

Why “\n”

Why

“\n”

ItIt introduces

introduces aa new

new line

line onon the

the terminal

terminal screen.

screen.

escape sequence

\a\a

alert (bell)

alert

(bell) character

character

\\\\

backslash

backslash

\b\b

backspace

backspace

\?\?

question mark

question

mark

\f\f

formfeed

formfeed

\’\’

single

single quote

quote

\n\n

newline

newline

\”\”

double

double quote

quote

\r\r

carriage

carriage return

return

\000

\000

octal number

octal

number

\t\t

horizontal tab

horizontal

tab

\xhh

\xhh

hexadecimal number

hexadecimal

number

\v\v

vertical tab

vertical

tab

 
Arithmetic Operations
Arithmetic Operations
Arithmetic Assignment Operators
Arithmetic Assignment Operators
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement

Increment and Decrement

Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Operators

Operators

 
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
awkward awkward easy easy easiest easiest
awkward awkward easy easy easiest easiest
awkward awkward easy easy easiest easiest

awkward

awkward

easy

easy

easiest

easiest

Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
     
xx == x+1; xx +=+= 11 x++ x++
xx == x+1; xx +=+= 11 x++ x++
  • xx == x+1;

x+1;

  • xx +=+= 11

x++

x++

Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
xx == x-1; xx -=-= 11 x-- x--
xx == x-1; xx -=-= 11 x-- x--
xx == x-1; xx -=-= 11 x-- x--
  • xx == x-1;

x-1;

  • xx -=-= 11

x--

x--

Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11
Increment and Decrement Operators awkward awkward easy easy easiest easiest xx == x+1; xx +=+= 11

Example

Arithmetic

Arithmetic operators

operators

int ii == 10;

int

10;

int jj == 15;

int

15;

int add

int

add == ii ++ j;j; //25

//25

int diff

int

diff == jj –– i;i; //5//5

int product

int

product == ii ** j;j; //// 150

150

int quotient

int

quotient == jj // i;i; //// 11

nt residual

iint

residual == jj %% i;i; //// 55

i++;

i++;

//Increase byby 11

//Increase

i--; i--;

//Decrease byby 11

//Decrease

Comparing

Comparing them

them

int ii == 10;

int

10;

int jj == 15;

int

15;

float kk == 15.0;

float

15.0;

jj // ii == ??

jj %% ii == ??

kk // ii == ??

kk %% ii == ??

 The The Answer Answer

The

The Answer

Answer

 The The Answer Answer jj // ii == 1; jj %% ii == 5; kk
jj // ii == 1;

jj // ii

== 1;

1;

jj %% ii == 5;
jj %% ii == 5;
jj %% ii == 5;

jj %% ii == 5;

5;

kk // ii == 1.5;

kk // ii

== 1.5;

1.5;

 The The Answer Answer jj // ii == 1; jj %% ii == 5; kk
kk %% ii ItIt isis illegal For Note: %, the operands can only bebe integers.
kk %% ii ItIt isis illegal For Note: %, the operands can only bebe integers.
kk %% ii ItIt isis illegal For Note: %, the operands can only bebe integers.

kk %% ii

ItIt isis illegal

illegal..

Note: For

Note:

For %,

%, the

the operands

operands can

can only

only bebe integers.

integers.

Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations

Logical Operations

Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
 is is “true” What and “false” in in C C InIn C, there isis nono
 is is “true” What and “false” in in C C InIn C, there isis nono
 is is “true” What and “false” in in C C InIn C, there isis nono
 is is “true” What and “false” in in C C InIn C, there isis nono
 is is “true” What and “false” in in C C InIn C, there isis nono
 is is “true” What and “false” in in C C InIn C, there isis nono

What isis “true”

What

true and

and “false”

false inin CC

InIn C,

C, there

there isis nono specific

specific data

data type

type toto represent

represent “true”

true and

and “false”.

false. CC

uses value

uses

value “0”

0 toto represent

represent “false”,

false, and

and uses

uses non-zero

non-zero value

value toto stand

stand

for

for “true”.

“true”.

Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
 Logical Logical Operators Operators AA &&&& BB =>=> AA and BB
 Logical Logical Operators Operators AA &&&& BB =>=> AA and BB
 Logical Logical Operators Operators AA &&&& BB =>=> AA and BB

Logical

Logical Operators

Operators

AA &&&& BB

=>=>

AA and

and BB

AA |||| BB=>=> AA or BB

AA |||| BB=>=>

AA or

or

BB

Logical Operations  is is “true” What and “false” in in C C InIn C, there
AA ==== BB =>=> IsIs AA equal toto B?

AA ==== BB

=>=>

IsIs AA equal

equal toto B?

B?

AA !=!= BB =>=> IsIs AA not equal toto B?
AA !=!= BB =>=> IsIs AA not equal toto B?
AA !=!= BB =>=> IsIs AA not equal toto B?

AA !=!= BB

=>=>

IsIs AA not

not equal

equal toto B?

B?

Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
Logical Operations  is is “true” What and “false” in in C C InIn C, there
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>

AA >> BB

AA >=>= BB

AA << BB

AA <=<= BB

=>=>

=>=>

=>=>

=>=>

IsIs AA greater

greater than

than B?

B?

IsIs AA greater

greater than

than or

or equal

IsIs AA less

less than

than B?

B?

IsIs AA less

less than

than oror equal

equal toto B?

equal toto B?

B?

B?

 be be confused Don’t &&&& and |||| have different meanings from && and |.|. &&
 be be confused Don’t &&&& and |||| have different meanings from && and |.|. &&
 be be confused Don’t &&&& and |||| have different meanings from && and |.|. &&
 be be confused Don’t &&&& and |||| have different meanings from && and |.|. &&
 be be confused Don’t &&&& and |||| have different meanings from && and |.|. &&
 be be confused Don’t &&&& and |||| have different meanings from && and |.|. &&

Dont bebe confused

Don’t

confused

&&&& and

and |||| have

have different

different meanings

meanings from

from && and

and |.|.

&& and

and || are

are bitwise

bitwise operators.

operators.

AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>
AA >> BB AA >=>= BB AA << BB AA <=<= BB =>=> =>=> =>=> =>=>

Short circuiting

SShort

hort circuiting

circuiting means

means that

that we

we don't

don't

evaluate the

evaluate

the second

second part

part of

of anan AND

AND or

or OROR

unless

unless we

we really

really need

need toto..

Some practices

Some

practices

Please

Please compute

compute the

the value

value of

of the

the following

following

logical expressions?

logical

expressions?

ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<

int ii == 10;

int

10; int

int jj == 15;

15; int

if( ii << jj &&&& jj << k)k)

if(

if( ii !=!= jj |||| kk << j)j)

if(

if( j<=

if(

j<= kk |||| ii >> k)k)

if( jj ==== kk &&&& m)

if(

m)

int kk == 15;

15; int

int mm == 0;

0;

=>=>

=>=>

=>=>

=>=>

ii == 10; int int jj == 15; int ii << jj &&&& jj <<
if(i) =>=>

if(i) if(i)

=>=>

ii == 10; int int jj == 15; int ii << jj &&&& jj <<
if(mif(m |||| jj &&&& ii )) =>=>

if(mif(m |||| jj &&&& ii ))

=>=>

ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int ii << jj &&&& jj <<
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==

int ii == 10;

int

10; int

int jj == 15;

15; int

int kk == 15;

15; int

int mm == 0;

0;

if( ii << jj &&&& jj << k)k) =>=> false

if( if( ii << jj &&&& jj << k)k) =>=> false false

if( ii !=!= jj |||| kk << j)j) =>=> true

if( if( ii !=!= jj |||| kk << j)j) =>=> true true

ii == 10; int int jj == 15; int kk == 15; int mm ==
if( j<= kk |||| ii >> k)k) =>=> true

if( if( j<= j<= kk |||| ii >> k)k) =>=> true true

ii == 10; int int jj == 15; int kk == 15; int mm ==
if( jj ==== kk &&&& m) =>=> false

if( if( jj ==== kk &&&& m) m) =>=> false false

ii == 10; int int jj == 15; int kk == 15; int mm ==
if(i) =>=> true

if(i) if(i) =>=> true true

ii == 10; int int jj == 15; int kk == 15; int mm ==
if(mif(m |||| jj &&&& ii )) =>=> true

if(mif(m |||| jj &&&& ii )) =>=> true true

ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
Did Did you you get get the the correct correct answers? answers?
Did Did you you get get the the correct correct answers? answers?
Did Did you you get get the the correct correct answers? answers?

Did

Did you

you get

get the

the correct

correct answers?

answers?

ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==
ii == 10; int int jj == 15; int kk == 15; int mm ==

Conditionals

ifif statement

statement

Three Three basic basic formats, formats,

ifif ((expression

expression){){

statement statement ……

}}

ifif ((expression

expression)) {{

statement statement ……

}}else

else{{

statement

statement ……

}}

ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement…
ifif (( expression {{ statement… statement…
ifif (( expression {{ statement… statement…
ifif (( expression {{ statement… statement…
ifif (( expression {{ statement… statement…

ifif ((expression

expression)) {{

statement…

statement…

}} else if if (( expression {{ statement… statement… }} else{
}} else if if (( expression {{ statement… statement… }} else{
}} else if if (( expression {{ statement… statement… }} else{
}} else if if (( expression {{ statement… statement… }} else{
}} else if if (( expression {{ statement… statement… }} else{
}} else if if (( expression {{ statement… statement… }} else{
}} else if if (( expression {{ statement… statement… }} else{

}} else

else ifif ((expression

expression)) {{

statement…

statement…

}} else{

else{

statement… statement…

statement…

statement…

ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
}}

}}

ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}
ifif (( expression {{ statement… statement… }} else if if (( expression {{ statement… statement… }}

AnAn example

example

if(score >=>= 90){

if(score

90){

a_cnt

a_cnt ++;

++;

}else if(score

}else

if(score >=>= 80){

80){

b_cnt++; b_cnt++;

}else if(score

}else

if(score >=>= 70){

70){

c_cnt++; c_cnt++;

}else ifif (score>=

}else

(score>= 60){

60){

d_cnt++

d_cnt++

}else{ }else{

f_cnt++ f_cnt++

}}

 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
 The switch The switch statement statement switch (( expression switch expression )) {{
 The switch The switch statement statement switch (( expression switch expression )) {{
 The switch The switch statement statement switch (( expression switch expression )) {{
 The switch The switch statement statement switch (( expression switch expression )) {{
 The switch The switch statement statement switch (( expression switch expression )) {{

The switch

The

switch statement

statement

switch ((expression

switch

expression))

{{

case item1 case item1 :: statement statement ;; break; break; case item2 case item2 ::
case item1 case item1 :: statement statement ;; break; break; case item2 case item2 ::
case item1 case item1 :: statement statement ;; break; break; case item2 case item2 ::
case item1 case item1 :: statement statement ;; break; break; case item2 case item2 ::
case item1 case item1 :: statement statement ;; break; break; case item2 case item2 ::
case item1 case item1 :: statement statement ;; break; break; case item2 case item2 ::
case item1 case item1 :: statement statement ;; break; break; case item2 case item2 ::

case item1

case

item1::

statement

statement;;

break;

break;

case item2

case

item2::

 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
statement statement ;;

statement

statement;;

break; break; default: default: statement statement ;;
break; break; default: default: statement statement ;;
break; break; default: default: statement statement ;;
break; break; default: default: statement statement ;;
break; break; default: default: statement statement ;;

break;

break;

default:

default:

statement

statement;;

 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
break; break;

break;

break;

}}

}}

 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
 The switch The switch statement statement switch (( expression switch expression )) {{ case item1
 The switch The switch statement statement switch (( expression switch expression )) {{ case item1

Loops

for

for statement

statement

for for ((expression1 expression1;; expression2 expression2;; expression3 expression3)){{

statement…

statement…

}}

expression1 initializes;

expression1

initializes;

expression2 isis the

expression2

the terminate

terminate test;

test;

expression3 isis the

expression3

the modifier

modifier;;

 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
 An example An example int int x; x;
 An example An example int int x; x;
 An example An example int int x; x;

An example

An

example

int int x; x;

 
 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
for for (x=0; (x=0; x<3; x<3; x++) x++)

for for (x=0; (x=0; x<3; x<3; x++) x++)

 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
{{

{{

printf("x=%d printf("x=%d\\n",x); n",x);
printf("x=%d printf("x=%d\\n",x); n",x);
printf("x=%d printf("x=%d\\n",x); n",x);

printf("x=%d

printf("x=%d\\n",x);

n",x);

}}

}}

 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx
time: First time: Second time: Third time: Fourth xx == 0; xx == 1; xx

First time:

First

time:

Second time:

Second

time:

Third time:

Third

time:

Fourth time:

Fourth

time:

xx == 0;

0;

xx == 1;

1;

xx == 2;

2;

xx == 3;

3; (don’t

(dont execute

execute the

the body)

body)

 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)
 An example An example int int x; x; for for (x=0; (x=0; x<3; x<3; x++)

The while

The

while statement

statement

while ((expression

while

expression)) {{

statement

statement ……

}}

while loop

while

loop exits

exits only

only when

when the

the expression

expression isis

false.

false.

AnAn example

example

int

int xx == 3;

3;

while

while (x>0)

(x>0) {{

printf("x=%d

printf("x=%d n",x);

n",x);

x--;

x--;

}}

for <==> while

for ((expression1

for

expression1;;

expression2

expression2;;

expression3)){{

expression3

statement…

statement…

}}

equals

expression1;

expression1;

while (expression2)

while

(expression2)

{{

statement…;

statement…;

expression3;

expression3;

}}

Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
Arrays & Strings

Arrays & Strings

Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
Arrays & Strings  Arrays Arrays int ids[50]; int ids[50]; char char name[100]; name[100]; int table_of_num[30][40];
 Arrays Arrays

Arrays Arrays