C Programming Tutorial - Introduction to C


Resources > C Quick Notes > {{currentSubTopic}}

Control Statements


A statement in C is a valid expression to perform the desired task. It ends with a semicolon(;). A valid C statement has to follow two rules such as syntax and semantic. Syntax is related to the grammar of the language and semantic is the meaning or logic behind a word within a statement. A statement can be a declaration, assignment, expression or a function call.

Control Statements

Statements that enable the user to determine the flow of the program are called control statements or a control structure. Control statements are broadly classified as:
2. Selection
5.Function call

1. Sequential:

In a program, if all the statements are executed sequentially then that control structure is called as a sequential control structure. By default, a program has sequential control structure.

2. Selection:

In a program, when a statement or a group of statements are executed depending upon the evaluation of a condition then it is called as selection statements or conditional statements. If the condition is true then certain set of statements are executed, or else either other set of statements are executed or no statement is executed. In C, the following conditional statements are used.
a. If
b. if .... else
c. switch- case

a. if statement

The if statement is one of the conditional statements. It is used to make decisions. If the evaluation of the given condition is true, then it directs the computer to execute certain statements.

The structure of an if statement is as follows:

Nested if:


If condition1 is true, then it enters inside the block. If condition2 is true, then it executes statement1 and prints statement2. If condition2 is false, then it only executes statement2.

	printf("divisible by 2\n");
	printf("divisible by 5");

 if n=10, then 
	divisible by 2
	divisible by 5
 if n=25
	divisible by 5

b. if..else statement

The if..else statement is one of the conditional statements. It is used to make decisions. If the evaluation of the given condition is true, then it directs the computer to execute particular statements, if false then to execute another set of statements.
The structure of if ...else statement is as follows:


c. switch statement

The switch statement is a selection statement. It is an extension of if ….. else statement. In if….. .else, we can have only two branches either true or false, but switch statement permits any number of branches. The branches are called as cases.

switch 	(variable or expression)
	case value1:
	case value2:
	case valuen:

3. Iteration

In a program, a statement or group of statements are executed repeatedly depending on the evaluation of a condition is called as Iterative structure or loops. The difference between selection and Iteration is, the selection executes the statements only once, whereas Iterative statements executes repeatedly. In C, following Iterative statements are used.
a. for
b. while
c. do while

a. for loop

The most widely used looping is probably for loop. It is used for iterative execution.
The general syntax of the for loop is:

for(initialisation ; condition ; updation)

b. while loop

while loop is an iterative control statement. It executes a segment of code repeatedly. It is also called as pre-test while loop.
The general form of the loop is:


c. do..while loop

do..while loop is an iterative control statement. It executes a segment of code repeatedly until the condition fails. It is also called as post-test while loop.
The general form of the loop is :


The difference between while and do..while loop is that, in while loop the body of the loop is executed first time only when the condition is true but in do..while the body of the loop is executed first time, before it checks the condition. Therefore even if the evaluation of the condition is false, at least once the loop will be executed.


A function is a block of statements, given a suitable name, to perform a specific task when invoked.

Parts of function

void main()
		float i=10.5;
		double sq;
		double square(float); // function declaration	
					// Actual parameter
		sq=square(i);		// function call
		printf("The square is %lf",sq);
double square(float j) 		// formal parameter/ placeholder
		double s;		
		s=j*j;			// Function Definition
		return s;		// Return Type

Function declaration

The function declaration provides information about the function definition to the compiler so that the compiler knows how many bytes to retrieve and how to interpret the value returned by the function. The function declaration can occur either inside or outside any function. It takes the form of function definition header except that instead of specifying parameter names, it lists the types of the identifiers. It ends with a semicolon. .When a function call is encountered, the compiler checks the function call with its declaration, so that the correct argument types are used. The syntax of the function declaration is:

returntype function_name(par1,par2....parn);
double square(float);

Function call

A function is a dormant entity which can act only when it is invoked i.e called. A function call is specified by the function name followed by the values of the parameters enclosed with parentheses terminated by a semicolon(;). The parameters that are placed inside the parentheses are called as actual parameter or arguments. The arguments can be a variable, constant or an expression or a function call itself:


Use of functions

1) Functions break up long continuous programs into a series of individual modules that are related to each other. Hence the program becomes more concise and it makes the logic clear thus, it is easier for the programmer to read and comprehend it.
2) Since it is divided into small tasks, it is easier to identify which part of the program is responsible for particular error and thus the debugging becomes easier.
3) To modify the program, it is not necessary to make changes in the entire program but only in the related functions.
4) It saves the programmer the trouble of having to type the same code again and again.
5) A loop can repeat a series of instructions only if each iteration immediately follows the previous one. Whereas calling a function by contrast, can cause a series of instructions to be repeated at any point within the program. Therefore it is more useful than the loops.

Structures and Unions

If all the data elements of a program are of the same type, it can be represented as an array. If the elements are of different types such as storing the details of a student, employee, etc., then arrays are inadequate to do the task. Under such circumstances structures are used. Structure is a collection of logically related data items grouped together under a single name, called a structure tag. The data items that make up a structure are called its members or fields and it can be of any type. Since a structure is derived from other data types it is called as user defined data type or derived data type.

Defining a Structure

A structure definition is specified by the keyword struct. This is followed by a template surrounded by braces, which describes the members of the structure.

struct student
{	int roll;
	char name[20];
	float fees;

Here struct is a keyword followed by the name of the structure student. Roll no., name and fees are called as members or fields. Members can be of any data type. It can either be primary or secondary or even bit fields.

struct student
{	int roll;
	char name[20];
	float fees;

void main()
	struct student s3,s4, s2={102,”Bark”,185.70};
	printf(“%d-%s-%f”,s1.roll,s1.name,s1.fees); → 101-Arun-175.50

The period (.) is the structure member operator. The period operator enables us to access every element of a structure variable. It follows a structure variable name followed by a member name.

printf(“%d”,s3.roll); →103


A union is declared exactly like a structure, except that the keyword union is used instead of struct. However, the members within a union share the same memory. Hence, all the members of the union variable cannot be initialized at the same time rather only one member can be initialized. The last initialized member occupies the memory location. Therefore only the last initialized member can be accessed.

union sample
{	int a;
	float b;


The typedef keyword allows the user to define a new data type which is equivalent to existing data types. Here the new name is only for name sake whereas in reality it is only a representation of the existing data type.

The general form of defining a new data type is:
typedef existing-type new-type;
eg. typedef int age;
age person;

Here person is a variable of type age. Since age is a representation of int ,for person variable, two bytes will be allocated.


Pre-processor is a program that modifies the source code before it is passed to the compiler. The modified source code is called as expanded source program. Each of the pre-processor directives begin with a # symbol. It can be placed anywhere in the program but generally it is placed at the beginning of a program.

The features of pre-processors:
1. Macro expansion
2. File Inclusion
3. Conditional Compilation

Constant Definition

If a constant is to be replaced at different parts of a program we define the constant with #define directives. These are called as symbolic constants. Constant names are generally defined in uppercase for better understanding.

#define PI 3.14
void main()
	float radius,area,perimeter;
	printf(“The area =%f and the perimeter =%f”,area,perimeter);

In the above example, PI is a constant name. Wherever PI is used in the program, it will be replaced by 3.14. There must be a space between the constant name and the value. It should not end with semicolon. In one line more than one macro can be defined, each one should be separated by a comma.
Different ways of defining macros:

#define PROMPT printf(“Enter a number”)
#define AND &&,OR ||
#define GREAT (x>10&&x<20)
#define P printf

Macros with Arguments

Like functions, macros also can have parameters, but the arguments is not given the data type because the name has to be replaced by the actual parameter while expanding.

#define PI 3.14
#define AREA(r)  PI*r*r
void main()
	float ra=2, ar;
	printf(“The area=%f”,ar); → The area=12.560000

Conditional Compilation

Conditional compilation allows selective inclusion of lines of source text on the basis of a computed condition. Conditional compilation is performed using the following preprocessor directives. Every conditional preprocessor ends with #endif.
i. ifdef
ii. ifndef
iii. if
iv. else
v. elif
vi. endif