C Notes
C Notes
BCSC 201
Syntax
char variable_name;
C programming language provides the following types of loops to handle looping requirements.
1 while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition
before executing the loop body.
Syntax:
while(condition) {
statement(s);
Notes on Principles of programming
BCSC 201
for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop
variable.
2 Syntax:
for ( initialization; condition; increment/decrement ) {
statement(s);
}
do...while loop
It is more like a while statement, except that it tests the condition at the end of the loop body.
Syntax:
3
do {
statement(s);
} while( condition );
1 if(expression) {
if...else statement
An if statement can be followed by an optional else statement, which executes when the
Boolean expression is false.
if(expression) {
2
/* statement(s) will execute if the expression is true */
}
else {
/* statement(s) will execute if the expression is false */
}
nested if statements
You can use one if or else if statement inside another if or else if statement(s).
if( expression 1) {
3
/* Executes when the expression 1 is true */
if(expression 2) {
/* Executes when the expression 2 is true */
}
}
4 switch statement
A switch statement allows a variable to be tested for equality against a list of values.
switch(expression) {
Notes on Principles of programming
BCSC 201
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
You can use one switch statement inside another switch statement(s).
switch(ch1) {
case 'A':
printf("This A is part of outer switch" );
5 switch(ch2) {
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows −
type arrayName [ arraySize ];
Notes on Principles of programming
BCSC 201
This is called a single-dimensional array. The arraySize must be an integer constant greater than
zero and type can be any valid C data type. For example, to declare a 10-element array called
balance of type double, use this statement −
double balance[10];
will set the value 200.0 as the first element of the array.
The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array
is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size
[x][y], you would write something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A two-
dimensional array can be considered as a table which will have x number of rows and y number of
columns. A two-dimensional array a, which contains three rows and four columns can be shown as
follows −
Thus, every element in the array a is identified by an element name of the form a[ i ][ j ], where 'a'
is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in 'a'.
However, in memory, elements of 2-D array are stored in sequential way. Following figure
illustrating the actual memory representation of 2-D array. Such allocation is called row-major
format. Note that, in memory, first row is presented first, then the second and so on. Another
representation can be a column-major form. In general row-major form is supported by most of the
compilers.
Notes on Principles of programming
BCSC 201
Note:
Practice some programs also for theory examinations.
1. String reversal.
2. Number reversal.
3. Palindrome check.
4. Matrix addition, multiplication. (Important)
5. Linear search from array.