Types of Repeated Execution
Loop: Group of instructions that are executed
repeatedly while some condition remains true.
How loops are controlled
1, 2, 3, 4,
, 4, 3, 2, 1
A self-contained program segment that carries
out some specific, well-defined task.
Every C program consists of one or more
One of these functions must be called main.
Execution of th
Recall that a structure can be declared as:
struct stud cfw_
a, b, c;
And the individual structure elements can be accessed
What is a Structure?
It is a convenient tool for handling a group of logically
related data items.
Student name, roll number, and marks.
Real part and complex part of a complex number.
This is our first look at a n
Searching and Sorting
Searching an Array:
Linear and Binary Search
Check if a given element (key) occurs in
Two methods to be discussed:
If the array elements are unsorted.
If the array elements are so
A pointer is a variable that represents the location (rather than the
value) of a data item.
They have a number of useful applications.
Enables us to access a variable that is defined outside the function.
Can be used to pass in
A string is an array of characters.
Individual characters are stored in memory in
A string is represented as a sequence of
characters terminated by the null (\0)
Arrays: pluses and minuses
+ Fast element access.
- Impossible to resize.
Many applications require resizing!
Required size not always immediately available.
Dynamic memory allocation: review
typedef struct cfw_
Pointers and arrays are very closely
linked in C.
think of array elements arranged in
consecutive memory locations.
int a, x;
pa = &a; /* pa pointer to address of a */
x = *pa; /* x = contents of pa (a in this case) */
To get somewh
What is a file?
A named collection of data, stored in secondary
Typical operations on files:
How is a file stored?
Stored as sequence of bytes, logically contiguous
(may not be
Statements and Blocks
An expression followed by a semicolon becomes a statement.
x = 5;
printf (The sum is 0\n, sum) ;
Braces cfw_ and are used to group declarations and statements
together into a compound statement, or block.