Professional Documents
Culture Documents
PL1 Lecture 03
PL1 Lecture 03
PL1 Lecture 03
Programming Languages 1
Lecture 3
Global Variables [ Scope ], Structures, Enumerated Types,
Type Definitions, .. & .. Arrays of Characters [ C Strings ]
Course & Lectures are based on their counterparts in the following courses:
o Harvard University's CS50; An introduction to the intellectual enterprises of
computer science and the art of programming, Harvard School of
Engineering and Applied Sciences.
o UNSW's CS1: Higher Computing, by Richard Buckland – The University of
New South Wales.
o MIT's 6.087 Practical Programming in C (2010), 6.096 Introduction to C++
(2011), and 6.S096 Introduction to C and C++ (2013), MIT (Massachusetts
1
Institute of Technology) OpenCourseWare.
• A basic introduction to C Strings [Arrays
of Characters]
• Strings in C
• String Handling Functions
• Variables’ Scope
O • Global Variables
• User-Defined
u Data Types
t • Data Types
Outline l
i • The typedef Statement
n • Enumerated Data Type
e • Default enum Values
• Structures
• The Concept
• Examples
• A Database Program
.. a basic introduction
to [ C Strings ]
Arrays of Characters
3
Recap
Data Types in C/C++ ..
bool - a Boolean expression of either true or
false
char c1 = 'H';
char c2 = 'I';
char c3 = '!';
Recap
H I !
c1 c2 c3
Recap
72 73 33
c1 c2 c3
Recap
17
Scope: Global Variables
void f oo ( ) {
++numCalls;
}
i n t main() {
foo(); foo(); foo();
printf("%d", numCalls); / / 3
}
int numCalls = 0;
Scope:
Global i n t r a i s e To P o w e r ( int b a s e , i n t e x p o n e n t )
{
Variables numCalls = numCalls + 1;
int result = 1;
Scope: where a for ( int i = 0; i < exponent; i++ )
variable was declared, {
determines where it result = result * base;
can be accessed from .. }
r e tu r n r e s u l t ;
}
i n t m a x ( int n u m 1 , i n t n u m 2 )
{
numCalls = numCalls + 1;
int result;
if ( num1 > num2 ) { result = num1; }
e ls e { r e s u l t = n u m 2 ; }
r e t ur n r e s u l t ;
}
int numCalls = 0;
Scope:
Global i n t r a i s e To P o w e r ( int b a s e , i n t e x p o n e n t )
{
Variables numCalls = numCalls + 1;
int result = 1;
Scope: where a for ( int i = 0; i < exponent; i++ )
variable was declared, {
determines where it result = result * base;
can be accessed from .. }
• numCalls has global r e tu r n r e s u l t ;
scope – can be }
accessed from any
function i n t m a x ( int n u m 1 , i n t n u m 2 )
{
numCalls = numCalls + 1;
int result;
if ( num1 > num2 ) { result = num1; }
e ls e { r e s u l t = n u m 2 ; }
r e t ur n r e s u l t ;
}
int numCalls = 0;
Scope:
Global i n t r a i s e To P o w e r ( int b a s e , i n t e x p o n e n t )
{
Variables numCalls = numCalls + 1;
int result = 1;
Scope: where a for ( int i = 0; i < exponent; i++ )
variable was declared, {
determines where it result = result * base;
can be accessed from .. }
• numCalls has global r e tu r n r e s u l t ;
scope – can be }
accessed from any
function i n t m a x ( int n u m 1 , i n t n u m 2 )
• result has a function {
scope – each numCalls = numCalls + 1;
int result;
function can have its
if ( num1 > num2 ) { result = num1; }
own separate
e ls e { r e s u l t = n u m 2 ; }
variable named
r e t ur n r e s u l t ;
result }
int numCalls = 0;
i n t r a i s e To P o w e r ( int b a s e , i n t e x p o n e n t ) Scope
{
numCalls = numCalls + 1;
int result = 1;
for ( int i = 0; i < exponent; i++ )
{
result = result * base;
}
return r e s u l t ;
}
i n t m a x ( int n u m 1 , i n t n u m 2 )
{ Global Scope
numCalls = numCalls + 1;
int result; int
if ( num1 > num2 ) { result = num1; } numCalls
else { result = num2; }
return r e s u l t ;
}
raiseToPower function scope max function scope
double double
newHigh newLow
Scope
double squareRoot( double num ) { Cannot access variables that are out of scope.
double low = 1.0;
double high = num;
for ( int i = 0; i < 30; i = i + 1)
{
squareRoot function scope
double estimate = (high + low) / 2;
if ( estimate * estimate > num ) { double double
double newHigh = estimate; num double high
high = newHigh; low A
}
else {
double newLow = estimate; for loop scope
low = newLow; double
} int i estimate
} // A
return estimate; // ERROR
}
If statement scope else statement scope
double double
newHigh newLow
Scope
double squareRoot( double num ) { Cannot access variables that are out of scope.
double low = 1.0; Solution 1: move the code.
double high = num;
for ( int i = 0; i < 30; i = i + 1)
{
squareRoot function scope
double estimate = (high + low) / 2;
if ( estimate * estimate > num ) { double double
double newHigh = estimate; num double high
high = newHigh; low A
}
else {
double newLow = estimate; for loop scope
low = newLow; double
} int i estimate
B
if ( i == 29 ) {
return estimate; // B
}
} // A If statement scope else statement scope
} double double
newHigh newLow
Scope
double squareRoot( double num ) { Cannot access variables that are out of scope.
double low = 1.0; Solution 2: declare the variable in a higher
double high = num; scope.
double estimate;
for ( int i = 0; i < 30; i = i + 1)
squareRoot function scope
{
estimate = (high + low) / 2; double double
if ( estimate * estimate > num ) { num double high
double newHigh = estimate; low A
high = newHigh;
}
else { for loop scope
double newLow = estimate; double
low = newLow; int i estimate
}
} // A
return estimate;
} If statement scope else statement scope
double double
newHigh newLow
Scope
User-Defined
Data Types
.. Structures,
Enumerated Types, ..
& .. Type Definitions
29
Data Types
. . .
Definition:
A Structure is a collection of related data items, possibly of
different types.
Structures .. the Concept
Defines variables of
struct date today, purchaseDate; type struct date.