0% found this document useful (0 votes)
3 views

C_programming_Chapter02_SEN2201

C is a versatile, fast, and platform-independent programming language primarily used for systems programming. It features static typing, structured programming, and supports various data types and operators. The document covers fundamental concepts such as variable declarations, data types, operators, and expressions in C programming.

Uploaded by

makserjanov318
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C_programming_Chapter02_SEN2201

C is a versatile, fast, and platform-independent programming language primarily used for systems programming. It features static typing, structured programming, and supports various data types and operators. The document covers fundamental concepts such as variable declarations, data types, operators, and expressions in C programming.

Uploaded by

makserjanov318
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Review: C Programming language

• C is a fast, small,general-purpose,platform independent


programming language.
• C is used for systems programming (e.g., compilers and
interpreters, operating systems, database systems,
microcontrollers etc.)
• C is static (compiled), typed, structured and imperative.
• "C is quirky, flawed, and an enormous success."–Ritchie

1
Review: Basics

• Variable declarations: int i ; float f ;


• Intialization: char c=’A’; int x=y=10;
• Operators: +,−,∗,/,%
• Expressions: int x,y,z; x=y∗2+z∗3;
• Function: int factorial ( int n); /∗ function takes int , returns int ∗/

2
Review

Variables and data types

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

C has a small family of datatypes.


• Numeric (int,float,double)
• Character (char)
• User defined (struct,union)

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;

• The unsigned version has roughly double the range of its


signed counterparts.
• Signed and unsigned characters differ only when used in
arithmetic expressions.
• Titbit: Flickr changed from unsigned long (232 − 1) to string
two years ago.
7
Big endian vs. little endian

The individual sizes are machine/compiler dependent.


However, the following is guaranteed:
sizeof(char)<sizeof(short)<=sizeof(int)<=sizeof(long) and
sizeof(char)<sizeof(short)<=sizeof(float)<=sizeof(double)
"NUXI" problem: For numeric data types that span multiple
bytes, the order of arrangement of the individual bytes is
important. Depending on the device architecture, we have "big
endian" and "little endian" formats.

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.

Figure: (from http://en.wikipedia.org/wiki/Little_endian)

9
Constants

Constants are literal/fixed values assigned to variables or used


directly in expressions.
Datatype example meaning
int i =3; integer
long l=3; long integer
integer unsigned long ul= 3UL; unsigned long
int i =0xA; hexadecimal
int i =012; octal number
float pi=3.14159 float
floating point float pi=3.141F float
double pi=3.1415926535897932384L double

10
Constants (contd.)

Datatype example meaning


’A’ character
character ’\x41’ specified in hex
’\0101’ specified in octal
"hello world" string literal
string
"hello""world" same as "hello world"
enum BOOL {NO,YES} NO=0,YES=1
enumeration
enum COLOR {R=1,G,B,Y=10} G=2,B=3

11
Declarations

The general format for a declaration is


type variable-name [=value] .
Examples:
• char x; /∗ uninitialized ∗/
• char x=’A’; /∗ intialized to ’ A’∗/
• char x=’A’,y=’B’; /∗multiple variables initialized ∗/
• char x=y=’Z’;/∗multiple initializations ∗/

12
Review

Variables and data types

Operators

Epilogue

14
Arithmetic operators

operator meaning examples


x=3+2; /∗constants∗/
+ addition y+z; /∗variables∗/
x+y+2; /∗both∗/
3−2; /∗constants∗/
- subtraction int x=y−z; /∗variables∗/
y−2−z; /∗both∗/
int x=3∗2; /∗constants∗/
* multiplication int x=y∗z; /∗variables∗/
x∗y∗2; /∗both∗/

14
Arithmetic operators (contd.)

operator meaning examples


float x=3/2; /∗produces x=1 (int /) ∗/
/ division float x=3.0/2 /∗produces x=1.5 (float /) ∗/
int x=3.0/2; /∗produces x=1 (int conversion)∗/
modulus int x=3%2; /∗produces x=1∗/
% int y=7;int x=y%4; /∗produces 3∗/
(remainder)
int y=7;int x=y%10; /∗produces 7∗/

15
Relational Operators

Relational operators compare two operands to produce a


’boolean’ result. In C any non-zero value (1 by convention) is
considered to be ’true’ and 0 is considered to be false.
operator meaning examples
3>2; /∗evaluates to 1 ∗/
> greater than
2.99>3 /∗evaluates to 0 ∗/
greater than or 3>=3; /∗evaluates to 1 ∗/
>=
equal to 2.99>=3 /∗evaluates to 0 ∗/
3<3; /∗evaluates to 0 ∗/
< lesser than
’A’<’B’/∗evaluates to 1∗/
lesser than or equal 3<=3; /∗evaluates to 1 ∗/
<=
to 3.99<3 /∗evaluates to 0 ∗/

16
Relational Operators

Testing equality is one of the most commonly used relational


operator meaning examples
3==3; /∗evaluates to 1 ∗/
== equal to
operator. ’A’==’a’/∗evaluates to 0 ∗/
3!=3; /∗evaluates to 0 ∗/
!= not equal to
2.99!=3 /∗evaluates to 1 ∗/
Gotchas:
• Note that the "==" equality operator is different from the
"=", assignment operator.
• Note that the "==" operator on float variables is tricky
because of finite precision.

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

Increment and decrement are common arithmetic operation. C


provides two short cuts for the same.
Postfix
• 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 y=x;x=x+1. x is evaluated before it is
incremented.
• y=x−− is a short cut for y=x;x=x−1. x is evaluated before it is
decremented.

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

Another common expression type found while programming in


C is of the type var = var (op) expr
• x=x+1
• x=x∗10
• x=x/2

C provides compact assignment operators that can be used


instead.
• x+=1 /∗is the same as x=x+1∗/
• x−=1 /∗is the same as x=x−1∗/
• x∗=10 /∗is the same as x=x∗10 ∗/
• x/=2 /∗ is the same as x=x/2
• x%=2 /∗is the same as x=x%2

22
Conditional Expression
A common pattern in C (and in most programming) languages
is the following:
i f ( cond )
x=<expra > ;
else
x=<exprb > ;

C provides syntactic sugar to express the same using the


ternary operator ’?:’

s i g n =x >0?1: −1; i s o d d =x %2==1?1:0;


i f ( x >0) i f ( x%2==1)
s i g n =1 i s o d d =1
else else
s i g n=−1 i s o d d =0

Notice how the ternary operator makes the code shorter and
easier to understand (syntactic sugar).
23
Review

Variables and data types

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 ∗ /

Another conversion done automatically by the compiler is ’char’


→ ’int’. This allows comparisons as well as manupilations of
character variables.
i s u p p e r =( c>=’A’ && c<=’Z’ ) ? 1 : 0 ; / ∗ c and l i t e r a l c o n s t a n t s
are c o n v e r te d t o i n t ∗ /
i f ( ! isupper )
c=c−’a’+’A’ ; / ∗ s u b t r a c t i o n i s p o s s i b l e
because o f i n t e g e r c o n v e r s i o n ∗ /

As a rule (with exceptions), the compiler promotes each term in


an binary expression to the highest precision operand.
24
Precedence and Order of Evaluation

• ++,–,(cast),sizeof have the highest priority


• *,/,% have higher priority than +,­
• ==,!= have higher priority than &&,||
• assignment operators have very low priority

Use () generously to avoid ambiguities or side effects


associated with precendence of operators.
• y=x∗3+2 /∗same as y=(x∗3)+2∗/
• x!=0 && y==0 /∗same as (x!=0) && (y==0)∗/
• d= c>=’0’&& c<=’9’/∗same as d=(c>=’0’) && (c<=’9’)∗/

25
Review: Definitions

• Variable - name/reference to a stored value (usually in


memory)
• Data type - determines the size of a variable in memory,
what values it can take on, what operations are allowed
• Operator - an operation performed using 1-3 variables
• Expression - combination of literal values/variables and
operators/functions

1
Review: Data types

• Various sizes (char, short, long, float , double)


• Numeric types - signed/unsigned
• Implementation - little or big endian
• Careful mixing and converting (casting) types

2
Review: Operators

• Unary, binary, ternary (1-3 arguments)


• Arithmetic operators, relational operators, binary (bitwise
and logical) operators, assignment operators, etc.
• Conditional expressions
• Order of evaluation (precedence, direction)

3
Review

Blocks and Compound Statements

Control Flow
Conditional Statements
Loops

Functions

Modular Programming

Variable Scope
Static Variables
Register Variables

4
Blocks and compound statements

• A simple statement ends in a semicolon:


z = foo(x+y);
• Consider the multiple statements:
temp = x+y ;
z = f o o ( temp ) ;

• Curly braces – combine into compound statement/block

4
Blocks

• Block can substitute for simple statement


• Compiled as a single unit
• Variables can be declared inside
{
i n t temp = x+y ;
z = f o o ( temp ) ;
}

• Block can be empty {}


• No semicolon at end

5
Nested blocks

• Blocks nested inside each other


{
i n t temp = x+y ;
z = f o o ( temp ) ;
{
f l o a t temp2 = x∗y ;
z += bar ( temp2 ) ;
}
}

6
Review

Blocks and Compound Statements

Control Flow
Conditional Statements
Loops

Functions

Modular Programming

Variable Scope
Static Variables
Register Variables

7
Control conditions

• Unlike C++ or Java, no boolean type (in C89/C90)


• in C99, bool type available (use stdbool.h)
• Condition is an expression (or series of expressions)
e.g. n < 3 or x < y || z < y
• Expression is non-zero ⇒ condition true
• Expression must be numeric (or a pointer)
const char s t r [ ] = "some text" ;
i f ( s t r ) / ∗ s t r i n g i s not n u l l ∗ /
return 0;

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 ;

• Additional alternative control paths


• Conditions evaluated in order until one is met; inner
statement then executed
• If multiple conditions true, only first executed
• Equivalent to nested if statements

11
Nesting if statements

i f ( x % 4 == 0 )
i f ( x % 2 == 0 )
y = 2;
else
y = 1;

To which if statement does the else keyword belong?

12
Nesting if statements

To associate else with outer if statement: use braces


i f ( x % 4 == 0 ) {
i f ( x % 2 == 0 )
y = 2;
} else
y = 1;

13
The switch statement

• Alternative conditional statement


• Integer (or character) variable as input
• Considers cases for value of variable
switch ( ch ) {
case ’Y’ : / ∗ ch == ’Y ’ ∗ /
/ ∗ do something ∗ /
break ;
case ’N’ : / ∗ ch == ’N ’ ∗ /
/ ∗ do something e l s e ∗ /
break ;
default : /∗ otherwise ∗/
/ ∗ do a t h i r d t h i n g ∗ /
break ;
}

14
Multiple cases

• Compares variable to each case in order


• When match found, starts executing inner code until
break; reached
• Execution “falls through” if break; not included

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

• Contents of switch statement a block


• Case labels: different entry points into block
• Similar to labels used with goto keyword (next lecture. . . )

16
Loop statements

• The while loop


• The for loop
• The do-while loop
• The break and continue keywords

17
The while loop

while ( / ∗ c o n d i t i o n ∗ / )
/ ∗ l o o p body ∗ /

• Simplest loop structure – evaluate body as long as


condition is true
• Condition evaluated first, so body may never be executed

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

• The “counting” loop


• Inside parentheses, three expressions, separated by
semicolons:
• Initialization: i = 1
• Condition: i <= n
• Increment: i++

• Expressions can be empty (condition assumed to be “true”)

19
The for loop

Equivalent to while loop:


int f a c t o r i a l ( int n) {
int j = 1;
int i = 1; /∗ i n i t i a l i z a t i o n ∗/
while ( i <= n / ∗ c o n d i t i o n ∗ / ) {
j ∗= i ;
i ++; / ∗ i n c r e m e n t ∗ /
}
return j ;
}

20
The for loop

• Compound expressions separated by commas


int f a c t o r i a l ( int n) {
int i , j ;
f o r ( i = 1 , j = 1 ; i <= n ; j ∗= i , i ++)
;
return j ;
}

• Comma: operator with lowest precedence, evaluated


left-to-right; not same as between function arguments

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 ∗ / ) ;

• Differs from while loop – condition evaluated after each


iteration
• Body executed at least once
• Note semicolon at end

22
The break keyword

• Sometimes want to terminate a loop early


• break; exits innermost loop or switch statement to exit
early
• Consider the modification of the do-while example:
char c ;
do {
/ ∗ l o o p body ∗ /
p u t s ( "Keep going? (y/n) " ) ;
c = getchar ( ) ;
i f ( c ! = ’y’ )
break ;
/∗ other processing ∗/
} while ( / ∗ o t h e r c o n d i t i o n s ∗ / ) ;

23
The continue keyword

• Use to skip an iteration


• continue; skips rest of innermost loop body, jumping to loop
condition
• Example:
# define min ( a , b ) ( ( a ) < ( b ) ? ( a ) : ( b ) )

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

Blocks and Compound Statements

Control Flow
Conditional Statements
Loops

Functions

Modular Programming

Variable Scope
Static Variables
Register Variables

25
Functions

• Already seen some functions, including main():


i n t main ( void ) {
/ ∗ do s t u f f ∗ /
r e t u r n 0 ; / ∗ success ∗ /
}

• Basic syntax of functions explained in Lecture 1


• How to write a program using functions?

25
Divide and conquer

• Conceptualize how a program can be broken into smaller


parts
• Let’s design a program to solve linear Diophantine
equation (ax + by = c,x, y: integers):
get a, b, c from command line
compute g = gcd(a,b)
if (c is not a multiple of the gcd)
no solutions exist; exit
run Extended Euclidean algorithm on a, b
rescale x and y output by (c/g)
print solution
• Extended Euclidean algorithm: finds integers x, y s.t.

ax + by = gcd(a, b).

26
Computing the gcd

• Compute the gcd using the Euclidean algorithm:


i n t gcd ( i n t a , i n t b ) {
while ( b ) { / ∗ i f a < b , performs swap ∗ /
i n t temp = b ;
b = a % b;
a = temp ;
}
return a ;
}

• Algorithm relies on gcd(a, b) = gcd(b, a mod b), for natural


numbers a > b.

[Knuth, D. E. The Art of Computer Programming, Volume 1: Fundamental


Algorithms. 3rd ed. Addison-Wesley, 1997.]

© 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

Pseudocode for Extended Euclidean algorithm:


Initialize state variables (x,y)
if (a < b)
swap(a,b)
while (b > 0) {
compute quotient, remainder
update state variables (x,y)
}
return gcd and state variables (x,y)

[Menezes, A. J., et al. Handbook of Applied Cryptography. CRC Press, 1996.]

© 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

• Extended Euclidean algorithm returns gcd, and two other


state variables, x and y
• Functions only return (up to) one value
• Solution: use global variables
• Declare variables for other outputs outside the function
• variables declared outside of a function block are globals
• persist throughout life of program
• can be accessed/modified in any function

29
Divide and conquer

• Break down problem into simpler sub-problems


• Consider iteration and recursion
• How can we implement gcd(a,b) recursively?
• Minimize transfer of state between functions
• Writing pseudocode first can help

30
Review

Blocks and Compound Statements

Control Flow
Conditional Statements
Loops

Functions

Modular Programming

Variable Scope
Static Variables
Register Variables

31
Programming modules in C

• C programs do not need to be monolithic


• Module: interface and implementation
• interface: header files
• implementation: auxilliary source/object files
• Same concept carries over to external libraries (next
week. . . )

31
The Euclid module

• Euclid’s algorithms useful in many contexts


• Would like to include functionality in many programs
• Solution: make a module for Euclid’s algorithms
• Need to write header file (.h) and source file (.c)

32
The source: euclid.c

Implement gcd() in euclid.c:


/ ∗ The gcd ( ) f u n c t i o n ∗ /
i n t gcd ( i n t a , i n t b ) {
while ( b ) { / ∗ i f a < b , performs swap ∗ /
i n t temp = b ;
b = a % b;
a = temp ;
}
return a ;
}

Extended Euclidean algorithm implemented as


ext_euclid(), also in euclid.c

33
The extern keyword

• Need to inform other source files about functions/global


variables in euclid.c
• For functions: put function prototypes in a header file
• For variables: re-declare the global variable using the
extern keyword in header file
• extern informs compiler that variable defined somewhere
else
• Enables access/modifying of global variable from other
source files

34
The header: euclid.h

Header contains prototypes for gcd() and ext_euclid():

/ ∗ 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 ) ;

/ ∗ compute g = gcd ( a , b ) and s o l v e ax+by=g ∗ /


int ext_euclid ( int a , int 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 ) ;

/ ∗ compute x and y u s i n g Extended E u c l i d e a n a l g . ∗ /


g = ext_euclid (a , b ) ;

• Results in global variables x and y


/ ∗ r e s c a l e so ax+by = c ∗ /
grow = c / g ;
x ∗= grow ;
y ∗= grow ;

36
Compiling with the Euclid module

• Just compiling diophant.c is insufficient


• The functions gcd() and ext_euclid() are defined in
euclid.c; this source file needs to be compiled, too
• When compiling the source files, the outputs need to be
linked together into a single output
• One call to gcc can accomplish all this:
athena%1 gcc -g -O0 -Wall diophant.c
euclid.c -o diophant.o
• diophant.o can be run as usual

1
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.

37
Review

Blocks and Compound Statements

Control Flow
Conditional Statements
Loops

Functions

Modular Programming

Variable Scope
Static Variables
Register Variables

38
Variable scope

• scope – the region in which a variable is valid


• Many cases, corresponds to block with variable’s
declaration
• Variables declared outside of a function have global scope
• Function definitions also have scope

38
An example

What is the scope of each variable in this example?


i n t nmax = 2 0 ;

/ ∗ 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

How many lines are printed now?


i n t nmax = 2 0 ;

/ ∗ 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

• static keyword has two meanings, depending on where


the static variable is declared
• Outside a function, static variables/functions only visible
within that file, not globally (cannot be extern’ed)
• Inside a function, static variables:
• are still local to that function
• are initialized only during program initialization
• do not get reinitialized with each function call

s t a t i c i n t somePersistentVar = 0 ;

41
Register variables

• During execution, data processed in registers


• Explicitly store commonly used data in registers – minimize
load/store overhead
• Can explicitly declare certain variables as registers using
register keyword
• must be a simple type (implementation-dependent)
• only local variables and function arguments eligible
• excess/unallowed register declarations ignored, compiled
as regular variables
• Registers do not reside in addressed memory; pointer of a
register variable illegal

42
Example

Variable scope example, revisited, with register variables:


/ ∗ The main ( ) f u n c t i o n ∗ /
i n t main ( r e g i s t e r i n t argc , r e g i s t e r char ∗∗ argv )
{
r e g i s t e r i n t a = 0 , b = 1 , c , n , nmax = 2 0 ;
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 ∗ /
}

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

You might also like