C_programming_Chapter02_SEN2201
C_programming_Chapter02_SEN2201
1
Review: Basics
2
Review
Operators
Epilogue
3
Definitions
Datatypes:
• The datatype of an object in memory determines the set
of values it can have and what operations that can be
performed on it.
• C is a weakly typed language. It allows implicit conversions
as well as forced (potentially dangerous) casting.
Operators:
• Operators specify how an object can be manipulated
(e.g.,, numeric vs. string operations).
• operators can be unary(e.g., -,++),binary (e.g.,
+,-,*,/),ternary (?:)
3
Definitions (contd.)
Expressions:
• An expression in a programming language is a
combination of values, variables, operators, and functions
Variables:
• A variable is as named link/reference to a value stored in
the system’s memory or an expression that can be
evaluated.
Consider: int x=0,y=0; y=x+2;.
• x, y are variables
• y = x + 2 is an expression
• + is an operator.
4
Variable names
Naming rules:
• Variable names can contain letters,digits and _
• Variable names should start with letters.
• Keywords (e.g., for,while etc.) cannot be used as variable
names
• Variable names are case sensitive. int x; int X declares
two different variables.
Pop quiz (correct/incorrect):
• int money$owed; (incorrect: cannot contain $)
• int total_count (correct)
• int score2 (correct)
• int 2ndscore (incorrect: must start with a letter)
• int long (incorrect: cannot use keyword)
5
Data types and sizes
6
Numeric data types
Depending on the precision and range required, you can use
one of the following datatypes.
signed unsigned
short short int x;short y; unsigned short x;unsigned short int y;
default int x; unsigned int x;
long long x; unsigned long x;
float float x; N/A
double double x; N/A
char char x; signed char x; unsigned char x;
8
Big endian vs. little endian (cont.)
• Big endian: the most significant bits (MSBs) occupy the
lower address. This representation is used in the powerpc
processor. Networks generally use big-endian order, and
thus it is called network order.
• Little endian : the least signficant bits (LSBs) occupy the
lower address. This representation is used on all x86
compatible processors.
9
Constants
10
Constants (contd.)
11
Declarations
12
Review
Operators
Epilogue
14
Arithmetic operators
14
Arithmetic operators (contd.)
15
Relational Operators
16
Relational Operators
17
Logical operators
operator meaning examples
((9/3)==3) && (2∗3==6); /∗evaluates to 1 ∗/
&& AND
(’A’==’a’) && (3==3) /∗evaluates to 0 ∗/
2==3 || ’A’==’A’; /∗evaluates to 1 ∗/
|| OR
2.99>=3 || 0 /∗evaluates to 0 ∗/
!(3==3); /∗evaluates to 0 ∗/
! NOT
!(2.99>=3) /∗evaluates to 1 ∗/
Short circuit: The evaluation of an expression is discontinued if
the value of a conditional expression can be determined early.
Be careful of any side effects in the code.
Examples:
• (3==3) || (( c=getchar())==’y’). The second expression is not
evaluated.
• (0) && ((x=x+1)>0) . The second expression is not evaluated.
18
Increment and decrement operators
19
Increment and decrement operators
Prefix:
• ++x is a short cut for x=x+1
• −−x is a short cut for x=x−1
• y=++x is a short cut for x=x+1;y=x;. x is evaluate after it is
incremented.
• y=−−x is a short cut for x=x−1;y=x;. x is evaluate after it is
decremented.
20
Bitwise Operators
operator meaning examples
0x77 & 0x3; /∗evaluates to 0x3 ∗/
& AND
0x77 & 0x0; /∗evaluates to 0 ∗/
0x700 | 0x33; /∗evaluates to 0x733 ∗/
| OR
0x070 | 0 /∗evaluates to 0x070 ∗/
0x770 ^ 0x773; /∗evaluates to 0x3 ∗/
ˆ XOR
0x33 ^ 0x33; /∗evaluates to 0 ∗/
0x01<<4; /∗evaluates to 0x10 ∗/
« left shift
1<<2; /∗evaluates to 4 ∗/
0x010>>4; /∗evaluates to 0x01 ∗/
» right shift
4>>1 /∗evaluates to 2 ∗/
Notes:
• AND is true only if both operands are true.
• OR is true if any operand is true.
• XOR is true if only one of the operand is true.
21
Assignment Operators
22
Conditional Expression
A common pattern in C (and in most programming) languages
is the following:
i f ( cond )
x=<expra > ;
else
x=<exprb > ;
Notice how the ternary operator makes the code shorter and
easier to understand (syntactic sugar).
23
Review
Operators
Epilogue
24
Type Conversions
When variables are promoted to higher precision, data is
preserved. This is automatically done by the compiler for mixed
data type expressions.
int i ;
float f ;
f = i +3.14159; / ∗ i i s promoted t o f l o a t , f =( f l o a t ) i +3.14159 ∗ /
25
Review: Definitions
1
Review: Data types
2
Review: Operators
3
Review
Control Flow
Conditional Statements
Loops
Functions
Modular Programming
Variable Scope
Static Variables
Register Variables
4
Blocks and compound statements
4
Blocks
5
Nested blocks
6
Review
Control Flow
Conditional Statements
Loops
Functions
Modular Programming
Variable Scope
Static Variables
Register Variables
7
Control conditions
7
Conditional statements
• The if statement
• The switch statement
8
The if statement
i f ( x % 2)
y += x / 2 ;
• Evaluate condition
if (x % 2 == 0)
• If true, evaluate inner statement
y += x/2;
• Otherwise, do nothing
9
The else keyword
i f ( x % 2 == 0 )
y += x / 2 ;
else
y += ( x + 1 ) / 2 ;
• Optional
• Execute statement if condition is false
y += (x+1)/2;
• Either inner statement may be block
10
The else if keyword
i f ( x % 2 == 0 )
y += x / 2 ;
else i f ( x % 4 == 1 )
y += 2 ∗ ( ( x + 3 ) / 4 ) ;
else
y += ( x + 1 ) / 2 ;
11
Nesting if statements
i f ( x % 4 == 0 )
i f ( x % 2 == 0 )
y = 2;
else
y = 1;
12
Nesting if statements
13
The switch statement
14
Multiple cases
switch ( ch ) {
switch ( ch ) { case ’Y’ :
case ’Y’ : / ∗ do something i f
case ’y’ : ch == ’Y ’ ∗ /
/∗ do something i f case ’N’ :
ch == ’Y ’ o r / ∗ do something i f
ch == ’ y ’ ∗ / ch == ’Y ’ o r
break ; ch == ’N ’ ∗ /
} break ;
}
15
The switch statement
16
Loop statements
17
The while loop
while ( / ∗ c o n d i t i o n ∗ / )
/ ∗ l o o p body ∗ /
18
The for loop
int f a c t o r i a l ( int n) {
int i , j = 1;
f o r ( i = 1 ; i <= n ; i ++)
j ∗= i ;
return j ;
}
19
The for loop
20
The for loop
21
The do-while loop
char c ;
do {
/ ∗ l o o p body ∗ /
p u t s ( "Keep going? (y/n) " ) ;
c = getchar ( ) ;
/∗ other processing ∗/
} while ( c == ’y’ && / ∗ o t h e r c o n d i t i o n s ∗ / ) ;
22
The break keyword
23
The continue keyword
i n t gcd ( i n t a , i n t b ) {
i n t i , r e t = 1 , m i n v a l = min ( a , b ) ;
f o r ( i = 2 ; i <= m i n v a l ; i ++) {
i f ( a % i ) / ∗ i not d i v i s o r of a ∗ /
continue ;
i f ( b % i == 0 ) / ∗ i i s d i v i s o r o f both a and b ∗ /
ret = i ;
}
return r e t ;
}
24
Review
Control Flow
Conditional Statements
Loops
Functions
Modular Programming
Variable Scope
Static Variables
Register Variables
25
Functions
25
Divide and conquer
ax + by = gcd(a, b).
26
Computing the gcd
© Addison Wesley. All rights reserved. This content is excluded from our Creative Commons license.
For more information, see http://ocw.mit.edu/fairuse.
27
Extended Euclidean algorithm
© CRC Press. All rights reserved. This content is excluded from our Creative Commons license.
For more information, see http://ocw.mit.edu/fairuse.
28
Returning multiple values
29
Divide and conquer
30
Review
Control Flow
Conditional Statements
Loops
Functions
Modular Programming
Variable Scope
Static Variables
Register Variables
31
Programming modules in C
31
The Euclid module
32
The source: euclid.c
33
The extern keyword
34
The header: euclid.h
/ ∗ ensure i n c l u d e d o n l y once ∗ /
# i f n d e f __EUCLID_H__
# define __EUCLID_H__
/∗ global v a r ia b l e s ( declared i n e u c l i d . c ) ∗/
extern i n t x , y ;
/ ∗ compute gcd ∗ /
i n t gcd ( i n t a , i n t b ) ;
#endif
35
Using the Euclid module
• Want to be able to call gcd() or ext_euclid() from the
main file diophant.c
• Need to include the header file euclid.h:
#include "euclid.h" (file in “.”, not search path)
• Then, can call as any other function:
/ ∗ compute g = gcd ( a , b ) ∗ /
g = gcd ( a , b ) ;
36
Compiling with the Euclid module
1
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
37
Review
Control Flow
Conditional Statements
Loops
Functions
Modular Programming
Variable Scope
Static Variables
Register Variables
38
Variable scope
38
An example
/ ∗ The main ( ) f u n c t i o n ∗ /
i n t main ( i n t argc , char ∗∗ argv ) / ∗ e n t r y p o i n t ∗ /
{
int a = 0, b = 1, c , n;
p r i n t f ( "%3d: %d\n" , 1 , a ) ;
p r i n t f ( "%3d: %d\n" , 2 , b ) ;
f o r ( n = 3 ; n <= nmax ; n++) {
c = a + b; a = b; b = c;
p r i n t f ( "%3d: %d\n" , n , c ) ;
}
r e t u r n 0 ; / ∗ success ∗ /
}
39
Scope and nested declarations
/ ∗ The main ( ) f u n c t i o n ∗ /
i n t main ( i n t argc , char ∗∗ argv ) / ∗ e n t r y p o i n t ∗ /
{
i n t a = 0 , b = 1 , c , n , nmax = 2 5 ;
p r i n t f ( "%3d: %d\n" , 1 , a ) ;
p r i n t f ( "%3d: %d\n" , 2 , b ) ;
f o r ( n = 3 ; n <= nmax ; n++) {
c = a + b; a = b; b = c;
p r i n t f ( "%3d: %d\n" , n , c ) ;
}
r e t u r n 0 ; / ∗ success ∗ /
}
40
Static variables
s t a t i c i n t somePersistentVar = 0 ;
41
Register variables
42
Example
43
Summary
Topics covered:
• Controlling program flow using conditional statements and
loops
• Dividing a complex program into many simpler
sub-programs using functions and modular programming
techniques
• Variable scope rules and extern, static, and
register variables
44