Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

if statement in C

In c programming language, the program execution flow is, line by line from top to bottom. That
means the c program is executed line by line from the main method. But this type of execution
flow may not be suitable for all the program solutions. Sometimes, we make some decisions or
we may skip the execution of one or more lines of code. Consider a situation, where we write a
program to check whether a student has passed or failed in a particular subject. Here, we need to
check whether the marks are greater than the pass marks or not. If marks are greater, then we
take the decision that the student has passed otherwise failed. To solve such kind of problems in
c we use the statements called decision making statements.

Decision making statements are the statements that are used to verify a given condition
and decides whether a block of statements gets executed or not based on the condition
result.

In c programming language, there are two decision making statements they are as follows...

1. if statement
2. switch statement

if statement in c

In c, if statement is used to make decisions based on a condition. The if statement verifies the
given condition and decides whether a block of statements are executed or not based on the
condition result. In c, if statement is classified into four types as follows...

1. Simple if statement
2. if - else statement
3. Nested if statement
4. if-else-if statement (if-else ladder)

Simple if statement

Simple if statement is used to verify the given condition and executes the block of statements
based on the condition result. The simple if statement evaluates specified condition. If it is
TRUE, it executes the next statement or block of statements. If the condition is FALSE, it skips
the execution of the next statement or block of statements. The general syntax and execution
flow of the simple if statement is as follows...
Simple if statement is used when we have only one option that is executed or skipped based on a
condition.

Example Program | Test whether given number is divisible by 5.

#include <stdio.h>
void main(){
int n ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%5 == 0 )
printf("Given number is divisible by 5\n") ;
printf("statement does not belong to if!!!") ;
}not belong to if!!!

if - else statement

The if - else statement is used to verify the given condition and executes only one out of the two
blocks of statements based on the condition result. The if-else statement evaluates the specified
condition. If it is TRUE, it executes a block of statements (True block). If the condition is
FALSE, it executes another block of statements (False block). The general syntax and execution
flow of the if-else statement is as follows...
The if-else statement is used when we have two options and only one option has to be executed
based on a condition result (TRUE or FALSE).

Example Program | Test whether given number is even or odd.

#include <stdio.h>
void main(){
int n ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%2 == 0 )
printf("Given number is EVEN\n") ;
else
printf("Given number is ODD\n") ;
}is ODD

Nested if statement

Writing a if statement inside another if statement is called nested if statement. The general syntax
of the nested if statement is as follows...

The nested if statement can be defined using any combination of simple if & if-else statements.

Example Program | Test whether given number is even or odd if it is below 100.

#include <stdio.h>
void main(){
int n ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n < 100 )
{
printf("Given number is below 100\n") ;
if( n%2 == 0)
printf("And it is EVEN") ;
else
printf("And it is ODD") ;
}
else
printf("Given number is not below 100") ;
}is not below 100

if - else - if statement (if-else ladder)

Writing a if statement inside else of a if statement is called if - else - if statement. The general
syntax of the if-else-if statement is as follows...

The if-else-if statement can be defined using any combination of simple if & if-else statements.

Example Program | Find the largest of three numbers.

#include <stdio.h>
void main(){
int a, b, c ;
printf("Enter any three integer numbers: ") ;
scanf("%d%d%d", &a, &b, &c) ;
if( a>=b && a>=c)
printf("%d is the largest number", a) ;
else if (b>=a && b>=c)
printf("%d is the largest number", b) ;
else
printf("%d is the largest number", c) ;
}POINO BE REMEMBERED

When we use conditional control statement like if statement, condition might be an expression
evaluated to a numerical value, a variable or a direct numerical value. If the expression value or
direct value is zero the conditon becomes FALSE otherwise becomes TRUE.

To understand more consider the following statements...

 if(10) - is TRUE
 if(x) - is FALSE if x value is zero otherwise TRUE
 if(a+b) - is FALSE if a+b value is zero otherwise TRUE
 if(a = 99) - is TRUE because a value is non-zero
 if(10, 5, 0) - is FALSE because it considers last value
 if(0) - is FALSE
 if(a=10, b=15, c=0) - is FALSE because last value is zero

switch statement in C

Consider a situation in which we have more number of options out of which we need to select
only one option that is to be executed. Such kind of problems can be solved using nested
if statement. But as the number of options increases, the complexity of the program also gets
increased. This type of problems can be solved very easily using switch statement. Using switch
statement, one can select only one option from more number of options very easily. In switch
statement, we provide a value that is to be compared with a value associated with each option.
Whenever the given value matches with the value associated with an option, the execution starts
from that option. In switch statement every option is defined as a case.

The switch statement has the following syntax and execution flow diagram...
The switch statement contains one or more number of cases and each case has a value associated
with it. At first switch statement compares the first case value with the switchValue, if it gets
matched the execution starts from the first case. If it doesn't match the switch statement
compares the second case value with the switchValue and if it is matched the execution starts
from the second case. This process continues until it finds a match. If no case value matches with
the switchValue specified in the switch statement, then a special case called default is executed.

When a case value matches with the switchValue, the execution starts from that particular case.
This execution flow continues with next case statements also. To avoid this, we use "break"
statement at the end of each case. That means the break statement is used to terminate the switch
statement. However it is optional.

Example Program | Display pressed digit in words.

#include <stdio.h>
void main(){
int n ;
printf("Enter any digit: ") ;
scanf("%d", &n) ;
switch( n )
{
case 0: printf("ZERO") ;
break ;
case 1: printf("ONE") ;
break ;
case 2: printf("TWO") ;
break ;
case 3: printf("THREE") ;
break ;
case 4: printf("FOUR") ;
break ;
case 5: printf("FIVE") ;
break ;
case 6: printf("SIX") ;
break ;
case 7: printf("SEVEN") ;
break ;
case 8: printf("EIGHT") ;
break ;
case 9: printf("NINE") ;
break ;
default: printf("Not a Digit") ;
}
}
while Statement in C

Consider a situation in which we execute a single statement or block of statements repeatedly for
required number of times. Such kind of problems can be solved using looping statements in C.
For example, assume a situation where we print a message for 100 times. If we want to perform
that task without using looping statements, we have to either write 100 printf statements or we
have to write the same message for 100 times in a single printf statement. Both are complex
methods. The same task can be performed very easily using looping statements.

The looping statements are used to execute a single statement or block of statements
repeatedly until the given condition is FALSE.

C language provides three looping statements...

 while statement
 do-while statement
 for statementStatement

The while statement is used to execute a single statement or block of statements repeatedly as
long as the given condition is TRUE. The while statement is also known as Entry control
looping statement. The while statement has the following syntax...

The while statement has the following execution flow diagram...


At first, the given condition is evaluated. If the condition is TRUE, the single statement or block
of statements gets executed. Once the execution gets completed the condition is evaluated again.
If it is TRUE, again the same statements gets executed. The same process is repeated until the
condition is evaluated to FALSE. Whenever the condition is evaluated to FALSE, the execution
control moves out of the while block.

Example Program | Program to display even numbers upto 10.

#include <stdio.h>
void main(){
int n = 0;
printf("Even numbers upto 10\n");

while( n <= 10 )
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}
}

When we use while statement, we must follow the following...

 while is a keyword so it must be used only in lower case letters.


 If the condition contains variable, it must be assigned a value before it is used.
 The value of the variable used in condition must be modified according to the
requirement inside the while block.
 In while statement, the condition may be a direct integer value, a variable or a condition.
 A while statement can be an empty statement

do-while Statement in C

The do-while statement is used to execute a single statement or block of statements repeatedly as
long as given the condition is TRUE. The do-while statement is also known as Exit control
looping statement. The do-while statement has the following syntax...
The do-while statement has the following execution flow diagram...

At first, the single statement or block of statements which are defined in do block are executed.
After execution of do block, the given condition gets evaluated. If the condition is evaluated to
TRUE, the single statement or block of statements of do block are executed again. Once the
execution gets completed again the condition is evaluated. If it is TRUE, again the same
statements are executed. The same process is repeated until the condition is evaluated to FALSE.
Whenever the condition is evaluated to FALSE, the execution control moves out of the while
block.

Example Program | Program to display even numbers upto 10.

#include <stdio.h>
void main(){
int n = 0;
clrscr() ;
printf("Even numbers upto 10\n");

do
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}while( n <= 10 ) ;
}

When we use do-while statement, we must follow the following...

 Both do and while are keywords so they must be used only in lower case letters.
 If the condition contains variable, it must be assigned a value before it is used.
 The value of the variable used in condition must be modified according to the
requirement inside the do block.
 In do-while statement the condition may be, a direct integer value, a variable or a
condition.
 A do-while statement can be an empty statement.
 In do-while, the block of statements are executed atleast once.

for Statement in C

The for statement is used to execute a single statement or a block of statements repeatedly as
long as the given condition is TRUE. The for statement has the following syntax and execution
flow diagram...

At first, the for statement executes initialization followed by condition evaluation. If the
condition is evaluated to TRUE, the single statement or block of statements of for statement are
executed. Once the execution gets completed, the modification statement is executed and again
the condition is evaluated. If it is TRUE, again the same statements are executed. The same
process is repeated until the condition is evaluated to FALSE. Whenever the condition is
evaluated to FALSE, the execution control moves out of the for block.
Example Program | Program to display even numbers upto 10.

#include <stdio.h>
void main(){
int n ;
clrscr() ;
printf("Even numbers upto 10\n");

for( n = 0 ; n <= 10 ; n++ )


{
if( n%2 == 0)
printf("%d\t", n) ;
}

getch() ;
}POINTS TO BE REMEMBERED

When we use for statement, we must follow the following...

 for is a keyword so it must be used only in lower case letters.


 Every for statement must be provided with initialization, condition and modification
(They can be empty but must be separated with ";")
Ex: for ( ; ; ) or for ( ; condition ; modification ) or for ( ; condition ; )
 In for statement, the condition may be a direct integer value, a variable or a condition.
 The for statement can be an empty statement.

Not a Digit break, continue and goto in C

In c, there are control statements which does not need any condition to control the program
execution flow. These control statements are called as unconditional control statements. C
programming language provides the following unconditional control statements...

 break
 continue
 goto

The above three statements does not need any condition to control the program execution flow.

In C, the break statement is used to perform the following two things...

1. break statement is used to terminate switch case statement


2. break statement is also used to terminate looping statements like while, do-while

and for.

When a break statement is encountered inside the switch case statement, the execution control
moves out of the switch statement directly. For example consider the following program...

Example Program | Program to perform all arithmetic operations using switch statement.

#include <stdio.h>
void main(){
int number1, number2, result ;
char operator;
printf("Enter any two integer numbers: ") ;
scanf("%d%d", &number1, &number2) ;
printf("Please enter any arithmetic operator: ");
operator = getchar();
switch(operator)
{
case '+': result = number1 + number2 ;
printf("Addition = %d", result) ;
break;
case '-': result = number1 - number2 ;
printf("Subtraction = %d", result) ;
break;
case '*': result = number1 * number2 ;
printf("Multiplication = %d", result) ;
break;
case '/': result = number1 / number2 ;
printf("Division = %d", result) ;
break;
case '%': result = number1 % number2 ;
printf("Remainder = %d", result) ;
break;
default: printf("\nWrong selection!!!") ;
}
}
When the break statement is encountered inside the looping statement, the execution control
moves out of the looping statements. The break statement execution is as shown in the following
figure.

Example Program for break statement.

#include <stdio.h>
void main(){
char ch ;
do
{
printf("Enter Y / N : ") ;
scanf("%c", &ch) ;
if(ch == 'Y')
{
printf("Okay!!! Repeat again !!!\n") ;
}
else
{
printf("Okay !!! Breaking the loop !!!") ;
break ;
}
} while( 1 ) ;
}

The continue statement is used to move the program execution control to the beginning of
looping statement. When continue statement is encountered in a looping statement, the
execution control skips the rest of the statements in the looping block and directly jumps to the
beginning of the loop. The continue statement can be used with looping statements like while,
do-while and for.

When we use continue statement with while and do-while statements the execution control
directly jumps to the condition. When we use continue statement with for statement the
execution control directly jumps to the modification portion (increment / decrement / any
modification) of the for loop. The continue statement execution is as shown in the following
figure...

Example Program | Program to illustrate continue statement.


#include <stdio.h>
#include<stdio.h>
void main(){
int number ;
while( 1 )
{
printf("Enter any integer number: ") ;
scanf("%d", &number) ;
if(number%2 == 0)
{
printf("Entered number is EVEN!!! Try another number!!!\n") ;
continue ;
}
else
{
printf("You have entered ODD number!!! Bye!!!") ;
}
}
}

The goto statement is used to jump from one line to another line in the program.
Using goto statement we can jump from top to bottom or bottom to top. To jump from one line
to another line, the goto statement requires a lable. Lable is a name given to the instruction or
line in the program. When we use goto statement in the program, the execution control directly
jumps to the line with specified lable.

Example Program for goto statement.

#include <stdio.h>
void main(){
printf("We are at first printf statement!!!\n") ;
goto last ;
printf("We are at second printf statement!!!\n") ;
printf("We are at third printf statement!!!\n") ;
last: printf("We are at last printf statement!!!\n") ;
}

When we use break, continue and goto statements, we must follow the following...

 The break is a keyword so it must be used only in lower case letters.


 The break statement can not be used with if statement.
 The break statement can be used only in switch case and looping statements.
 The break statement can be used with if statement, only if that if statement is written
inside the switch case or looping statements.
 The continue is a keyword so it must be used only in lower case letters.
 The continue statement is used only within looping statements.
 The continue statement can be used with if statement, only if that if statement is written
inside the looping statements.
 The goto is a keyword so it must be used only in lower case letters.
 The goto statement must requires a lable.
 The goto statement can be used with any statement like if, switch, while, do-while and
for etc,.

Introduction to Functions in C

When we write a program to solve a larger problem, we divide that larger problem into smaller
subproblems and are solved individually to make the program easier. In C, this concept is
implemented using functions. Functions are used to divide a larger program into smaller
subprograms such that program becomes easy to understand and easy to implement. A function
is defined as follows...

Function is a subpart of program used to perform specific task and is executed


individually.

Every C program must contain atleast one function called main(). However a program may also
containfunctions.

Every function in C has the following...

 Function Declaration (Function Prototype)


 Function Definition
 Function Call

Function Declaration

The function declaration tells the compiler about function name, datatype of the return value and
parameters. The function declaration is also called as function prototype. The function
declaration is performed before main function or inside main function or inside any other
function.
Function declaration syntax -

returnType functionName(parametersList);

In the above syntax, returnType specifies the datatype of the value which is sent as a return
value from the function definiton. The functionName is a user defined name used to identify the
function uniquely in the program. The parametersList is the data values that are sent to the
function definition.

Function Definition

The function definition provides the actual code of that function. The function definition is also
known as body of the function. The actual task of the function is implemented in the function
definition. That means the actual instructions to be performed by a function are written in
function definition. The actual instructions of a function are written inside the braces "{ }". The
function definition is performed before main function or after main function.

Function definition syntax -

returnType functionName(parametersList)
{

Actual code...

Function Call

The function call tells the compiler when to execute the function definition. When a function call
is executed, the execution control jumps to the function definition where the actual code gets
executed and returns to the same functions call once the execution completes. The function call
is performed inside main function or inside any other function or inside the function itself.

Function call syntax -

functionName(parameters);Advantages of Functions

 Using funcions we can implement modular programming.


 Functions makes the program more readable and understandable.
 Using functions the program implementation becomes easy.
 Once a function is created it can be used many times (code re-usability).
 Using functions larger program can be divided into smaller modules.

Types of Functions in C

In C Programming Language, based on providing the function definition, functions are divided
into two types. Those are as follows...

 System Defined Functions


 User Defined Functions

System Defined Functions

The C Programming Language provides pre-defined functions to make programming easy. These
pre-defined functions are known as syatem defined functions. The system defined function is
defined as follows...

The function whose definition is defined by the system is called as system defined
function.

The system defined functions are also called as Library Functions or Standard
Functions or Pre-Defined Functions. The implementation of system defined functions is
already defined by the system.

In C, all the system defined functions are defined inside the header
files like stdio.h, conio.h, math.h, string.h etc., For example, the
funtions printf() and scanf() are defined in the header file called stdio.h.

Whenever we use system defined functions in the program, we must include the respective
header file using #include statement. For example, if we use a system defined function sqrt() in
the program, we must include the header file called math.h because the function sqrt() is
defined in math.h. System defined functions are declared in header files

 System defined functions are implemented in .dll files. (DLL stands for Dynamic Link
Library).
 To use system defined functions the respective header file must be included.

User Defined Functions:In C programming language, users can also create their own functions.
The functions that are created by users are called as user defined functions. The user defined
function is defined as follows...
The function whose definition is defined by the user is called as user defined function.

That means the function that is implemented by user is called as user defined function. For
example, the function main is implemented by user so it is called as user defined function.

In C every user defined function must be declared and implemented. Whenever we make
function call the function definition gets executed. For example, consider the following program
in which we create a fucntion called addition with two paramenters and a return value.

#include <stdio.h>
void main(){
int num1, num2, result ;
int addition(int,int) ; // function declaration
clrscr() ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);

result = addition(num1, num2) ; // function call

printf("SUM = %d", result);


}
int addition(int a, int b) // function definition
{
return a+b ;
}

In the above example program, the function declaration statement "int addition(int,int)" tells
the compiler that there is a function with name addition which takes two integer values as
parameters and returns an integer value. The function call statement takes the execution control
to the additon() definition along with values of num1 and num2. Then function
definition executes the code written inside it and comes back to the function call along
with return value.

In the concept of functions, the function call is known as "Calling Function" and the function
definition is known as "Called Function".

When we make a function call, the execution control jumps from calling function to called
function. After executing the called function, the execution control comes back to calling
function from called function. When the control jumps from calling function to called function it
may carry one or more data values called "Paramenters" and while coming back it may carry a
single value called "return value". That means the data values transferred from calling function
to called function are called as Parameters and the data value transferred from called funcion to
calling function is called Return value.
Based on the data flow between the calling function and called function, the functions are
classified as follows...

 Function without Parameters and without Return value

 Function with Parameters and without Return value

 Function without Parameters and with Return value

 Function with Parameters and with Return value

Function without Parameters and without Return value

In this type of functions there is no data transfer between calling function and called function.
Simply the execution control jumps from calling function to called function and executes called
function, and finally comes back to the calling function. For example, consider the following
program...

#include <stdio.h>
void main(){
void addition() ; // function declaration
addition() ; // function call
}
void addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
printf("Sum = %d", num1+num2 ) ;
}

Function with Parameters and without Return value

In this type of functions there is data transfer from calling function to called function
(parameters) but there is no data transfer from called function to calling function (return value).
The execution control jumps from calling function to called function along with the parameters
and executes called function, and finally comes back to the calling function. For example,
consider the following program...

#include <stdio.h>
void main(){
int num1, num2 ;
void addition(int, int) ; // function declaration
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
addition(num1, num2) ; // function call
}
void addition(int a, int b) // function definition
{
printf("Sum = %d", a+b ) ;
}

Function without Parameters and with Return value

In this type of functions there is no data transfer from calling function to called function
(parameters) but there is data transfer from called function to calling function (return value). The
execution control jumps from calling function to called function and executes called function,
and finally comes back to the calling function along with a return value. For example, consider
the following program...

#include <stdio.h>
void main(){
int result ;
int addition() ; // function declaration
result = addition() ; // function call
printf("Sum = %d", result) ;
}
int addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
return (num1+num2) ;
}

Function with Parameters and with Return value

In this type of functions there is data transfer from calling function to called function
(parameters) and also from called function to calling function (return value). The execution
control jumps from calling function to called function along with parameters and executes called
function, and finally comes back to the calling function along with a return value. For example,
consider the following program...

#include <stdio.h>
void main(){
int num1, num2, result ;
int addition(int, int) ; // function declaration
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
result = addition(num1, num2) ; // function call
printf("Sum = %d", result) ;}
int addition(int a, int b) // function definition
{
return (a+b) ;
}Points to be Remembered

 The parameters specified in calling function are said to be Actual Parameters.


 The parameters declared in called function are said to be Formal Parameters.
 The value of actual parameters is always copied into formal parameters.

Parameter Passing in C

When a function gets executed in the program, the execution control is transferred from calling
function to called function and executes function definition, and finally comes back to the calling
function. When the execution control is transferred from calling function to called function it
may carry one or more number of data values. These data values are called as parameters.

Parameters are the data values that are passed from calling function to called function.

In C, there are two types of parameters and they are as follows...

 Actual Parameters
 Formal Parameters

The actual parameters are the parameters that are speficified in calling function. The formal
parameters are the parameters that are declared at called function. When a function gets
executed, the copy of actual parameter values are copied into formal parameters.

In C Programming Language, there are two methods to pass parameters from calling function to
called function and they are as follows...

 Call by Value
 Call by Reference

Call by Value

In call by value parameter passing method, the copy of actual parameter values are copied to
formal parameters and these formal parameters are used in called function. The changes made
on the formal parameters does not effect the values of actual parameters. That means, after
the execution control comes back to the calling function, the actual parameter values remains
same. For example consider the following program...

#include <stdio.h>
void main(){
int num1, num2 ;
void swap(int,int) ; // function declaration
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1, num2) ; // calling function
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
}
void swap(int a, int b) // called function
{
int temp ;
temp = a ;
a=b;
b = temp ;
}: num1 = 10, num2 = 20

In the above example program, the variables num1 and num2 are called actual parameters and
the variables a and b are called formal parameters. The value of num1 is copied into a and the
value of num2 is copied into b. The changes made on variables a and b does not effect the values
of num1 and num2.

Call by Reference

In Call by Reference parameter passing method, the memory location address of the actual
parameters is copied to formal parameters. This address is used to access the memory locations
of the actual parameters in called function. In this method of parameter passing, the formal
parameters must be pointer variables.

That means in call by reference parameter passing method, the address of the actual parameters
is passed to the called function and is recieved by the formal parameters (pointers). Whenever we
use these formal parameters in called function, they directly access the memory locations of
actual parameters. So the changes made on the formal parameters effects the values of actual
parameters. For example consider the following program...

#include <stdio.h>
void main(){
int num1, num2 ;
void swap(int *,int *) ; // function declaration
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1, &num2) ; // calling function
printf("\nAfter swap: num1 = %d, num2 = %d", num1, num2);
}
void swap(int *a, int *b) // called function
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
In the above example program, the addresses of variables num1 and num2 are copied to pointer
variables a and b. The changes made on the pointer variables a and b in called function effects
the values of actual parameters num1 and num2 in calling function.

Inter Function Communication in C

When a function gets executed in the program, the execution control is transferred from calling
function to called function and executes function definition, and finally comes back to the calling
function. In this process, both calling and called functions have to communicate each other to
exchange information. The process of exchanging information between calling and called
functions is called as inter function communication.

In C, the inter function communication is classified as follows...

 Downward Communication
 Upward Communication
 Bi-directional Communication

Downward Communication

In this type of inter function communication, the data is transferred from calling function to
called function but not from called function to calling function. The functions with parameters
and without return value are considered under downward communication. In the case of
downward communication, the execution control jumps from calling function to called function
along with parameters and executes the function definition,and finally comes back to the calling
function without any return value. For example consider the following program...

#include <stdio.h>
void main(){
int num1, num2 ;
void addition(int, int) ; // function declaration
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
addition(num1, num2) ; // calling function }
void addition(int a, int b) // called function
{

printf("SUM = %d", a+b) ;

}= 30
Upward Communication

In this type of inter function communication, the data is transferred from called function to
calling function but not from calling function to called function. The functions without
parameters and with return value are considered under upward communication. In the case of
upward communication, the execution control jumps from calling function to called function
without parameters and executes the function definition, and finally comes back to the calling
function along with a return value. For example consider the following program...

#include <stdio.h>
void main(){
int result ;
int addition() ; // function declaration
result = addition() ; // calling function
printf("SUM = %d", result) ;
}
int addition() // called function
{
int num1, num2 ;
num1 = 10;
num2 = 20;
return (num1+num2) ;
}

Bi - Directional Communication

In this type of inter function communication, the data is transferred from calling function to
called function and also from called function to calling function. The functions with parameters
and with return value are considered under bi-directional communication. In the case of bi-
drectional communication, the execution control jumps from calling function to called function
along with parameters and executes the function definition, and finally comes back to the calling
function along with a return value. For example consider the following program...

#include <stdio.h>
void main(){
int num1, num2, result ;
int addition(int, int) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
result = addition(num1, num2) ; // calling function
printf("SUM = %d", result) ;
}
int addition(int a, int b) // called function
{
return (a+b) ; }
Standard Functions in C

The standard functions are built-in functions. In C programming language, the standard functions
are declared in header files and defined in .dll files. In simple words, the standard functions can
be defined as "the ready made functions defined by the system to make coding more easy". The
standard functions are also called as library functions or pre-defined functions.

In C when we use standard functions, we must include the respective header file
using #include statement. For example, the function printf() is defined in header
file stdio.h (Standard Input Output header file). When we use printf() in our program, we must
include stdio.hheader file using #include<stdio.h> statement.

C Programming Language provides the following header files with standard functions.

Header
Purpose Example Functions
File

stdio.h Provides functions to perform standard I/O operations printf(), scanf()

conio.h Provides functions to perform console I/O operations clrscr(), getch()

math.h Provides functions to perform mathematical operations sqrt(), pow()

string.h Provides functions to handle string data values strlen(), strcpy()

stdlib.h Provides functions to perform general functions calloc(), malloc()

Provides functions to perform operations on time and


time.h time(), localtime()
date

Provides functions to perform - testing and mapping of


ctype.h isalpha(), islower()
character data values

setjump(),
setjmp.h Provides functions that are used in function calls
longjump()

Provides functions to handle signals during program


signal.h signal(), raise()
execution
Provides Macro that is used to verify assumptions made
assert.h assert()
by the program

Defines the location specific settings such as date


locale.h setlocale()
formats and currency symbols

Used to get the arguments in a function if the arguments va_start(), va_end(),


stdarg.h
are not specified by the function va_arg()

errno.h Provides macros to handle the system calls Error, errno

float.h Provides constants related to floating point data values

Defines the maximum and minimum values of various


limits.h
variable types like char, int and long

stddef.h Defines various variable types

graphics.h Provides functions to draw graphics. circle(), rectangle()

Scope of Variable in C

When we declare a variable in a program, it can not be accessed against the scope rules.
Variables can be accessed based on their scope. Scope of a variable decides the portion of a
program in which the variable can be accessed. Scope of the variable is defined as follows...

Scope of a variable is the portion of the program where a defined variable can be
accessed.

The variable scope defines the visibility of variable in the program. Scope of a variable depends
on the position of variable declaration.

In C programming language, a variable can be declared in three different positions and they are
as follows...

 Before the function definition (Global Declaration)


 Inside the function or block (Local Declaration)
 In the function definition parameters (Formal Parameters)

Before the function definition (Global Declaration)


Declaring a variable before the function definition (outside the function definition) is
called global declaration. The variable declared using global declaration is called global
variable. Tha global variable can be accessed by all the functions that are defined after the
global declaration. That means the global variable can be accessed any where in the program
after its declaration. The global variable scope is said to be file scope.

#include <stdio.h>
int num1, num2 ;
void main(){
void addition() ;
void subtraction() ;
void multiplication() ;
num1 = 10 ;
num2 = 20 ;
printf("num1 = %d, num2 = %d", num1, num2) ;
addition() ;
subtraction() ;
multiplication() ;
}
void addition()
{
int result ;
result = num1 + num2 ;
printf("\naddition = %d", result) ;
}
void subtraction()
{
int result ;
result = num1 - num2 ;
printf("\nsubtraction = %d", result) ;
}
void multiplication()
{
int result ;
result = num1 * num2 ;
printf("\nmultiplication = %d", result) ;
}

In the above example program, the variables num1 and num2 are declared as global variables.
They are declared before the main() function. So, they can be accessed by function main() and
other functions that are defined after main(). In the above example, the functions main(),
addition(), subtraction() and multiplication() can access the variables num1 and num2.

Inside the function or block (Local Declaration)


Declaring a variable inside the function or block is called local declaration. The variable
declared using local declaration is called local variable. The local variable can be accessed only
by the function or block in which it is declared. That means the local variable can be accessed
only inside the function or block in which it is declared.

#include <stdio.h>
#include<conio.h>
void main(){
void addition() ;
int num1, num2 ;
clrscr() ;
num1 = 10 ;
num2 = 20 ;
printf("num1 = %d, num2 = %d", num1, num2) ;
addition() ;
getch() ;
}
void addition()
{
int sumResult ;
sumResult = num1 + num2 ;
printf("\naddition = %d", sumResult) ;
}
The above example program shows an error because, the variables num1 and num2 are declared
inside the function main(). So, they can be used only inside main() function and not in addition()
function.

In the function definition parameters (Formal Parameters)

The variables declared in function definition as parameters have local variable scope. These
variables behave like local variables in the function. They can be accessed inside the function but
not outside the function.

#include <stdio.h>
void main(){
void addition(int, int) ;
int num1, num2 ;
num1 = 10 ;
num2 = 20 ;
addition(num1, num2) ;
}
void addition(int a, int b)
{
int sumResult ;
sumResult = a + b ;
printf("\naddition = %d", sumResult) ;
}

In the above example program, the variables a and b are declared in function definition as
parameters. So, they can be used only inside the addition() function.

Recursive Functions in C

In C programming language, function calling can be made from main() function, other functions
or from same function itself. The recursive function is defined as follows...

A function called by itself is called recursive function.

The recursive functions should be used very carefully because, when a function called by itself it
enters into infinite loop. And when a function enters into the infinite loop, the function execution
never gets completed. We should define the condition to exit from the function call so that the
recursive function gets terminated.

When a function is called by itself, the first call remains under execution till the last call gets
invoked. Every time when a function call is invoked, the function returns the execution control to
the previous function call.

#include <stdio.h>
int factorial( int ) ;
void main( )
{
int fact, n ;
printf(“Enter any positive integer: ”) ;
scanf(“%d”, &n) ;
fact = factorial( n ) ;
printf(“Factorial of %d is %d”, n, fact) ;
}
int factorial( int n )
{
int temp ;
if( n == 0)
return 1 ;
else
temp = n * factorial( n-1 ) ; // recursive function call
return temp ;
}

In the above example program, the factorial() function call is initiated from main() function with
the value 3. Inside the factorial() function, the function calls factorial(2), factorial(1) and
factorial(0) are called recursively. In this program execution process, the function call
factorial(3) remains under execution till the execution of function calls factorial(2), factorial(1)
and factorial(0) gets completed. Similarly the function call factorial(2) remains under execution
till the execution of function calls factorial(1) and factorial(0) gets completed. In the same way
the function call factorial(1) remains under execution till the execution of function call
factorial(0) gets completed. The complete execution process of the above program is shown in
the following figure...

You might also like