C Programming Books (1)
C Programming Books (1)
C Programming
C is a mid-level structured oriented programming language, used in general-purpose
programming, developed by Dennis Ritchie at AT&T Bell Labs, the USA, between 1969 and
1973.
Advantage of C
➢ C is the building block for many other programming languages.
➢ Programs written in C are highly portable.
➢ Several standard functions are there (like in-built) that can be used to develop
programs.
➢ C programs are collections of C library functions, and it's also easy to add functions
to the C library.
Disadvantage of C
➢ C does not provide Object Oriented Programming (OOP) concepts.
➢ There are no concepts of Namespace in C.
➢ C does not provide binding or wrapping up of data in a single unit.
➢ C does not provide Constructor and Destructor.
Identifiers
These are the variable names, function names or any other user-defined names within the
program. The standard format for any identifier name is, to begin with alphabets (upper or
lower case) or underscore (_). It is then followed by any alphabets (upper or lower case) or
digits. But it does not allow ‘@’, ‘$’ and ‘%’ to be used within the identifier. C identifiers are
case sensitive.
Var1, var2, _sum, str_name, strName, fltValue, intNumValue, avg_std_100
Keywords
These are the reserved words in C, which are used to identify the variables or perform some
functions within the program. For example, printf, scanf, if, else, for, while, loop, switch, int,
float, char, double, struct, const, goto, return, typedef, etc.
Semicolons
All codes in C have to be terminated by a semicolon. It indicates the end of the line of the
code.
printf (“Enter the name :”); getchar (); return 0;
Comments
Comments are the non-compliable lines of code in the program. They are used to give
information about the code. When the compiler encounters comments, it ignores those lines
and proceeds with the next compliable code. In C comments are written within ‘/*’ and ‘*/’ for
multiline comments and single-line comments are written after ‘//’.
Whitespaces
Whitespaces are used to separate two identifiers, keywords, any tokens or to have a blank
line, new line, etc. It differentiates any tokens from other while compiling and when a user
sees the code
First C Program
To write the first c program, open the C console and write the following code:
#include <stdio.h>
int main(){
printf("Hello C Language");
return 0;
}
#include <stdio.h> includes the standard input output library functions. The printf()
function is defined in stdio.h .
int main() The main() function is the entry point of every program in c language.
printf() The printf() function is used to print data on the console.
The scanf() function is used for input. It reads the input data from the console.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used
for successful execution and 1 for unsuccessful execution.
By menu
Now click on the compile menu then compile sub menu to compile the c program.
Then click on the run menu then run sub menu to run the c program.
By shortcut
You can view the user screen any time by pressing the alt+f5 keys.
Now press Esc to return to the turbo c++ console.
➢ Compilation process in c
Preprocessor
Compiler
The code which is expanded by the preprocessor is passed to the compiler. The compiler
converts this code into assembly code. Or we can say that the C compiler converts the pre-
processed code into assembly code.
Assembler
The assembly code is converted into object code by using an assembler. The name of the
object file generated by the assembler is the same as the source file. The extension of the
object file in DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file
is 'hello.c', then the name of the object file would be 'hello.obj'.
Linker
Mainly, all the programs written in C use library functions. These library functions are pre-
compiled, and the object code of these library files is stored with '.lib' (or '.a') extension. The
main working of the linker is to combine the object code of library files with the object code of
our program. Sometimes the situation arises when our program refers to the functions
defined in other files; then linker plays a very important role in this. It links the object code of
these files to our program. Therefore, we conclude that the job of the linker is to link the
object code of our program with the object code of the library files and other files. The output
of the linker is the executable file. The name of the executable file is the same as the source
file but differs only in their extensions. In DOS, the extension of the executable file is '.exe',
and in UNIX, the executable file can be named as 'a.out'. For example, if we are using
printf() function in a program, then the linker adds its associated code in an output file.
Let's understand through an example.
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
printf(“Hello World”);
getch();
Output :-
Hello World
#include<conio.h>
void main()
clrscr();
int a,b;
a=10;
b=5;
getch();
Output :-
The value of B =5
#include<conio.h>
void main()
clrscr();
int a,b;
scanf(“%d”,&a);
scanf (“%d”,&b);
getch();
Output :-
#include<conio.h>
void main()
clrscr();
int rno,maths,sci,eng,total;
scanf(“%d”,&rno);
scanf (“%d”,&maths);
scanf (“%d”,&sci);
scanf (“%d”,&eng);
total=maths+sci+eng;
getch();
void main()
clrscr();
float pi,radius,area;
pi=3.14;
scanf(“%f”,&radius);
area=pi*radius*radius;
getch();
#include<conio.h>
void main()
clrscr();
int a,b,c;
scanf(“%d”,&a);
scanf (“%d”,&b);
c=a+b;
b=a-b;
a=a-b;
getch();
//Write a program to find interchange value of two integer value using three variables
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int a,b,c;
scanf(“%d”,&a);
scanf (“%d”,&b);
c=a;
a=b;
b=c;
getch();
#include<conio.h>
void main()
clrscr();
float principle,r,year,s;
scanf(“%f”,& principle);
scanf(“%f”,&year);
s= principle*r*year/100;
getch();
❖ Decision Making
Decision making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the condition
is determined to be false.
Show below is the general form of a typical decision making structure found in most of the
programming languages −
C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value.
if statement
Syntax
if(boolean_expression)
C programming language assumes any non-zero and non-null values as true and if it is either zero or
null then it is assumed as false value.
Example:-
int a = 10;
if( a < 20 )
return 0;
When the above code is compiled and executed, it produces the following result:
if...else statement
An if statement can be followed by an optional else statement, which executes when the boolean
expression is false.
Syntax
if(boolean_expression)
else
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else
block of code will be executed.
C programming language assumes any non-zero and non-null values as true and if it is either zero or
null then it is assumed as false value
Example :-
int a = 100;
if( a < 20 )
else
return 0;
When the above code is compiled and executed, it produces the following result:
value of a is : 100
When using if , else if , else statements there are few points to keep in mind:
➢ An if can have zero or one else's and it must come after any else if's.
➢ An if can have zero to many else if's and they must come before the else.
➢ Once an else if succeeds, none of the remaining else if's or else's will be tested.
Syntax
if(boolean_expression 1)
else
Example :-
#include <stdio.h>
int main ()
int a = 100;
if( a == 10 )
printf("Value of a is 10\n" );
else if( a == 20 )
printf("Value of a is 20\n" );
else if( a == 30 )
printf("Value of a is 30\n" );
else
return 0;
When the above code is compiled and executed, it produces the following result:
Example:-
//Write a program to find maximum value of three integer values.
#include <stdio.h>
int main ()
Clrscr();
int a,b,c;
scanf(“%d”,&a);
scanf (“%d”,&b);
scanf (“%d”,&c);
return 0;
Example:-
//Write a program to find Grade of student marksheet
#include <stdio.h>
int main ()
Clrscr();
int rno,maths,sci,eng,total,per;
scanf(“%d”,&rno);
scanf (“%d”,&maths);
scanf (“%d”,&sci);
scanf (“%d”,&eng);
total=maths+sci+eng;
if(80>=per)
printf(“Grade is A”)
If else(70>=per)
printf(“Grade is B”)
If else(60>=per)
printf(“Grade is C”)
If else(50>=per)
printf(“Grade is D”)
If else(30>=per)
printf(“Grade is E”)
else
printf(“Fail”)
❖ switch statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each switch case.
Syntax
The syntax for a switch statement in C programming language is as follows:
switch(expression)
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
default :
/* Optional */
statement(s);
➢ The expression used in a switch statement must have an integral or enumerated type, or be
of a class type in which the class has a single conversion function to an integral or
enumerated type.
➢ You can have any number of case statements within a switch. Each case is followed by the
value to be compared to and a colon.
➢ The constant-expression for a case must be the same data type as the variable in the switch,
and it must be a constant or a literal.
➢ When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
➢ When a break statement is reached, the switch terminates, and the flow of control jumps to
the next line following the switch statement.
➢ Not every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
Example
switch(grade)
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
printf("Very Good!\n" );
break;
case 'C' :
printf("Well done\n" );
break;
printf("You passed\n" );
break;
case 'F' :
break;
default :
printf("Invalid grade\n" );
return 0;
When the above code is compiled and executed, it produces the following result:
Very Good
Your grade is B
switch(day)
case 1 :
printf("Sunday \n" );
break;
case 2 :
printf("Monday \n" );
break;
case 3 :
printf("Tuesday\n" );
break;
printf("Wednesday\n" );
break;
case 5 :
printf("Thursday\n" );
break;
case 6 :
printf("Friday\n" );
break;
case 7 :
printf("Saturday\n" );
break;
default :
printf("Out of Choice\n" );
return 0;
Int a,b,c,d;
scanf(“%d”,&a);
scanf (“%d”,&b);
printf (“\n 1) Addition \n2) Subtraction \n3) Multiplication \n4) Division \n:”);
scanf (“%d”,&c);
switch(c)
d=a+b;
break;
case 2 :
d=a-b;
break;
case 3 :
d=a*b;
break;
case 4 :
d=a/b;
break;
default :
printf("Out of Choice\n" );
return 0;
❖ C Loops
The looping can be defined as repeating the same process multiple times until a specific condition
satisfies. There are three types of loops used in the C language. In this part of the tutorial, we are
going to learn all the aspects of C loops.
Advantage of loops in C
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
Types of C Loops
There are three types of loops in C language that is given below:
1) do while
2) while
3) for
do-while loop in C
The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is
used when it is necessary to execute the loop at least once (mostly menu driven programs).
Do
//code to be executed
while(condition);
Example :-
#include<stdio.h>
int main(){
int i=1;
do
printf("%d \n",i);
i++;
while(i<=10);
return 0;
Output
1
2
3
4
5
6
7
8
9
10
//Program to print table for the given number using do while loop
#include<stdio.h>
int main()
int i=1,number=0;
scanf("%d",&number);
do
printf("%d \n",(number*i));
i++;
while(i<=10);
return 0;
Output
while(condition)
//code to be executed
#include<stdio.h>
int main()
int i=1;
while(i<=10)
printf("%d \n",i);
i++;
return 0;
Output
1
2
3
4
5
6
7
8
9
10
Properties of while loop
➢ A conditional expression is used to check the condition. The statements defined inside the while
loop will repeatedly execute until the given condition fails.
➢ The condition will be true if it returns 0. The condition will be false if it returns any non-zero
number.
➢ In while loop, the condition expression is compulsory.
➢ Running a while loop without a body is possible.
➢ We can have more than one conditional expression in while loop.
➢ If the loop body contains only one statement, then the braces are optional.
int i=2;
while(i<=10)
printf("%d \n",i);
i=i+2;
return 0;
Output
2
4
6
8
10
int main()
int i=1;
while(i<=10)
printf("%d \n",i);
i=i+2;
return 0;
Output
1
3
5
int main()
int i=123,B;
while(i>0)
B=i%10;
i=i/10;
printf(“%d”,B);
Return 0;
Output:-
321
int main()
int i=123,B,sum=0,temp;
temp=i;
while(i>0)
B=i%10;
sum=sum+(B*B*B);
i=i/10;
printf(“%d”,sum);
if(sum==temp)
else
Return 0;
❖ for loop in C
The for loop in C language is used to iterate the statements or a part of the program several times. It
is frequently used to traverse the data structures like the array and linked list.
//code to be executed
#include<stdio.h>
#include<conio.h>
int main()
int i=0;
for(i=1;i<=10;i++)
printf("%d \n",i);
return 0;
Output :-
1
2
3
4
5
6
7
8
9
10
//Write a program to sum of 1 to 10 number
#include<stdio.h>
Output :-
#include<conio.h>
1
int main() 3
{ 6
10
int i,sum=0; 15
21
for(i=1;i<=10;i++)
28
{ 36
45
sum=sum+i; 55
printf("%d \n",sum);
return 0;
int i,fact=1;
for(i=1;i<=5;i++)
printf("%d \n",fact);
return 0;
#include<stdio.h>
Output :-
#include<conio.h>
int main() 5
10
{ 15
20
int i,t=5,ans;
25
for(i=1;i<=10;i++) 30
35
{ 40
45
ans=i+t;
50
printf("%d \n",ans);
return 0;
#include<stdio.h>
Output :-
#include<conio.h>
scanf(“%d”,&a);
scanf(“%d”,&b);
for(i=1;i<=a;i++)
ans=ans*a;
return 0;
#include<stdio.h>
Output :-
#include<conio.h>
int main() 1
1
{ 2
3
int i,a=0,b=1,c=1 ; 5
for(i=1;i<=10;i++) 8
13
{ 21
34
printf("%d \n",c);
55
c=a+b;
a=b;
b=c;
printf("%d \n",c);
return 0;
Nested Loops in C
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of nesting loops in C.
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any
number of loops. The nesting level can be defined at n times. You can define any type of loop inside
another loop; for example, you can define 'while' loop inside a 'for' loop.
Inner_loop
Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while' loop.
The nested for loop means any type of loop which is defined inside the 'for' loop.
#include <stdio.h>
Output :-
int main()
printf("\n");
2) Example :-
#include <stdio.h>
Output :-
int main()
printf("\n");
3) Example :-
#include <stdio.h>
int main()
printf("\n");
return 0;
4) Example :-
for(int j=1;j<=i;j++)
printf("%d",j);
printf("\n");
5) Example :-
#include <stdio.h>
Output :-
int main()
{ 54321
int i;
5432
543
for(i=1;i<=5;i++) 54
5
{
for(int j=5;j>=i;j--)
printf("%d",j);
printf("\n");
6) Example :-
#include <stdio.h>
Output :-
int main()
{ 12345
int i;
1234
123
for(i=5;i>=1;i--) 12
1
{
for(int j=1;j<=i;j++)
printf("%d",j);
printf("\n");
Example :-
#include <stdio.h>
int main()
for(i=5;i>=1;i--) 55555
4444
{ 333
for(int j=1;j<=i;j++) 22
1
{
printf("%d",i);
printf("\n");
7) Example :-
#include <stdio.h>
int i,k; 1
12
for(i=1;i<=5;i++) 123
{ 1234
12345
for(int j=4;j>=i;j--)
printf(“ “);
for(int j=1;j<=i;j++)
printf("%d",j);
printf("\n");
8) Example :-
#include <stdio.h>
int main()
for(i=5;i>=1;i--) 5
54
{
543
for(int j=5;j>=i;j++) 5432
54321
{
printf("%d",j);
printf("\n");
9) Example :-
#include <stdio.h>
int main()
{
Output :-
int i,n;
Enter the value of n : 8
printf("Enter the value of n :");
scanf(“%d”,&n);
*
**
for(i=1;i<=n;i++) ***
****
{ *****
for(int j=1;j<=i;j++) ******
*******
{ ********
printf("*",j );
printf("\n");
10) Example :-
#include <stdio.h>
int i,j,k,l;
for(i=1;i<=10;i++)
for(j=9;j>=i;j--)
printf(" ",j );
for(k=1;k<=i;k++)
printf("*",k );
for(l=1;j<=i;j++)
printf("*",l );
printf("\n");
11) Example :-
#include <stdio.h>
int main()
int i,j,k,l;
for(i=1;i<=10;i++)
for(j=9;j>=i;j--)
for(k=1;k<=i;k++)
printf("*",k );
for(l=1;j<=i;j++)
printf("*",l );
printf("\n");
ASCII stands for American Standard Code for Information Interchange. Below is the ASCII character
table, including descriptions…
I. Alphabet Pattern: -
#include<stdio.h>
Output :-
void main()
{ A
int i, j;
BB
CCC
for(i=1;i<=5;i++) DDDD
EEEEE
{
for(j=1;j<=i;j++)
printf("%c",64+ i);
printf("\n");
#include<stdio.h>
void main()
Output :-
{
int i, j; A
for(i=1;i<=5;i++) AB
ABC
{ ABCD
ABCDE
for(j=1;j<=i;j++)
printf("%c",64+ j);
printf("\n");
Array
An array is defined as the collection of similar type of data items stored at contiguous memory
locations. Arrays are the derived data type in C programming language which can store the primitive
type of data such as int, char, double, float, etc. It also has the capability to store the collection of
derived data types, such as pointers, structure, etc. The array is the simplest data structure where
each data element can be randomly accessed by using its index number.
C array is beneficial if you have to store similar elements. For example, if we want to store the marks
of a student in 6 subjects, then we don't need to define different variables for the marks in the
different subject. Instead of that, we can define an array which can store the marks in each subject at
the contiguous memory locations.
By using the array, we can access the elements easily. Only a few lines of code are required to access
the elements of the array.
Properties of Array
The array contains the following properties.
➢ Each element of an array is of same data type and carries the same size, i.e., int =
4 bytes.
➢ Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
➢ Elements of the array can be randomly accessed since we can calculate the address
of each element of the array with the given base address and the size of the data
element.
Advantage of Array
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array
easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will
learn later.
Declaration of Array
We can declare an array in the c language in the following way.
data_type array_name[array_size];
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size
Initialization of Array
The simplest way to initialize an array is by using the index of each element. We can initialize each
element of the array by using the index. Consider the following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
❖ example
#include<stdio.h>
int main()
int i=0;
marks[0]=80;//initialization of array
marks[1]=60;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
printf("%d \n",marks[i]);
return 0;
Output:-
80
60
70
85
75
Array Declaration with Initialization
We can initialize the c array at the time of declaration. Let's see the code.
int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define the size. So it may also be written as the following
code.
int marks[]={20,30,40,50,60};
1) example :-
#include<stdio.h>
int main()
int i=0;
//traversal of array
for(i=0;i<5;i++)
return 0;
Output:-
20
30
40
50
60
2) example :-
#include<stdio.h>
int main()
int i ,a[5];
for(i=0;i<5;i++)
scand(“%d”,&a[i]);
for(i=0;i<5;i++)
return 0;
Output:-
enter the value of a [0] : 10
enter the value of a [0] : 20
enter the value of a [0] : 30
#include<stdio.h>
int main()
int i ,a[5],sum=0;
for(i=0;i<5;i++)
scand(“%d”,&a[i]);
sum=sum+a[i];
return 0;
Output:-
enter the value of a [0] : 10
enter the value of a [0] : 2
enter the value of a [0] : 3
enter the value of a [0] : 4
enter the value of a [0] : 5
#include<stdio.h>
int main()
int i ,a[5],max=0;
for(i=0;i<5;i++)
scand(“%d”,&a[i]);
max=a[0];
for(i=0;i<5;i++)
if(max<a[i])
max=a[i];
return 0;
Output:-
enter the value of a [0] : 10
enter the value of a [0] : 2
enter the value of a [0] : 30
enter the value of a [0] : 98
enter the value of a [0] : 3
5) example :-
#include<stdio.h>
int main()
int i ,rno[3],maths[3],sci[3],computer[3],total[3],per[3];
int search;
for(i=0;i<5;i++)
scand(“%d”,&rno[i]);
scand(“%d”,&maths[i]);
scand(“%d”,&sci[i]);
scand(“%d”,&computer[i]);
total[i]=maths[i]+sci[i]+computer[i];
per[i]=total[i]/3;
scand(“%d”,&serach);
for(i=0;i<5;i++)
if(rno[i]==search])
return 0;
data_type array_name[rows][columns];
int twodimen[4][3];
❖ Initialization of 2D Array
In the 1D array, we don't need to specify the size of the array if the declaration and initialization are
being done simultaneously. However, this will not work with 2D arrays. We will have to define at least
the second dimension of the array. The two-dimensional array can be declared and defined in the
following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
int main()
int i=0,j=0;
int arr[4][3]={
{2,3,4},
{3,4,5},
{4,5,6}
};
//traversing 2D array
for(i=0;i<4;i++)
for(j=0;j<3;j++)
}//end of j
}//end of i
return 0;
Output:-
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
1) example :-
#include<stdio.h>
int main()
int I,j;
int a[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}//end of j
}//end of i
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
printf(“\n”);
return 0;
Output:-
Enter the value of A :
1 2 3
4 5 6
7 8 9
The value of A :
1 2 3
4 5 6
7 8 9
int main()
int i,j;
int a[3][3],b[3][3],sum[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
sum[i][j]=a[i][j]+b[i][j];
printf(“\n”);
return 0;
Output:-
Enter the value of A :
1 2 3
4 5 6
7 8 9
The value of A :
1 2 3
4 5 6
7 8 9
❖ Multi-dimensional Arrays
In C, we can define multidimensional arrays in simple words as array of arrays. Data in
multidimensional arrays are stored in tabular form (in row major order).
data_type array_name[size1][size2]....[sizeN];
int x[2][3][4] =
};
Advantage of functions in C
There are the following advantages of C functions.
➢ By using functions, we can avoid rewriting same logic/code again and again in a program.
➢ We can call C functions any number of times in a program and from any place in a program.
➢ We can track a large C program easily when it is divided into multiple functions.
➢ Reusability is the main achievement of C functions.
➢ However, Function calling is always a overhead in a C program.
Function Aspects
There are three aspects of a C function.
➢ Function declaration A function must be declared globally in a c program to tell the compiler
about the function name, function parameters, and return type.
➢ Function call Function can be called from anywhere in the program. The parameter list must
not differ in function calling and function declaration. We must pass the same number of
functions as it is declared in the function declaration.
➢ Function definition It contains the actual statements which are to be executed. It is the most
important aspect to which the control comes when the function is called. Here, we must
notice that only one value can be returned from the function.
//code to be executed
Types of Functions
There are two types of functions in C programming:
Return Value
A C function may or may not return a value from the function. If you don't have to return any value
from the function, use void for the return type.
Let's see a simple example of C function that doesn't return any value from the function.
void hello()
{
printf("hello c");
}
If you want to return any value from the function, you need to use any data type such as int, long,
char, etc. The return type depends on the value to be returned from the function.
Let's see a simple example of C function that returns int value from the function.
return 10;
void printName();
void main ()
printf("Hello ");
printName();
void printName()
printf("Hello World");
Output
Hello World
Example for Function without argument and with return value
#include<stdio.h>
int sum();
void main()
int result;
result = sum();
printf("%d",result);
int a,b;
scanf("%d %d",&a,&b);
return a+b;
Output
The sum is 34
void main()
int a,b,result;
scanf("%d %d",&a,&b);
sum(a,b);
Output
The sum is 34
Example for Function with argument and with return value
#include<stdio.h>
void main()
int a,b,result;
scanf("%d %d",&a,&b);
result = sum(a,b);
return a+b;
Output
Call by value
➢ In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the function
call in the call by value method.
➢ In call by value method, we can not modify the value of the actual parameter by the formal
parameter.
➢ In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
➢ The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
➢ Let's try to understand the concept of call by value in c language by the example given below:
#include<stdio.h>
num=num+100;
int main()
int x=100;
return 0;
Output
#include<stdio.h>
(*num) += 100;
int main() {
int x=100;
return 0;
Output
1 A copy of the value is passed into the An address of value is passed into the
function function
2 Changes made inside the function is Changes made inside the function
limited to the function only. The values validate outside of the function also.
of the actual parameters do not change The values of the actual parameters do
by changing the formal parameters. change by changing the formal
parameters.
3 Actual and formal arguments are Actual and formal arguments are
created at the different memory created at the same memory location
location
Structure
Structure in c is a user-defined data type that enables us to store the collection of different data types.
Each element of a structure is called a member. Structures ca; simulate the use of classes and
templates as it can store various information
The ,struct keyword is used to define the structure. Let's see the syntax to define the structure in c.
struct structure_name
data_type member1;
data_type member2;
data_type memeberN;
};
1st way:
Let's see the example to declare the structure variable by struct keyword. It should be declared within
the main function.
struct employee
int id;
char name[50];
float salary;
};
2nd way:
Let's see another way to declare variable at the time of defining the structure.
int id;
char name[50];
float salary;
}e1,e2;
Example :-
#include<stdio.h>
struct sum
int a,b,c;
};
void main()
clrscr();
sum x;
scanf(“%d”,&x.a);
scanf (“%d”,&x.b);
getch();
Output :-
Enter the value of A : 55
Enter the value of B : 60
The value of A =55
The value of B = 60
Pointers
he pointer in C language is a variable which stores the address of another variable. This variable can
be of type int, char, array, function, or any other pointer. The size of the pointer depends on the
architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
Advantage of pointer
1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc.
and used with arrays, structures, and functions.
3) It makes you able to access any memory location in the computer's memory.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection
pointer used to dereference a pointer.
Pointer Example
Let's see the pointer example as explained for the above figure.
#include<stdio.h>
int main()
{
int number=50;
int *p;
p=&number; //stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the num
ber therefore printing p gives the address of number.\
}
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Usage of pointer
In c language, we can dynamically allocate memory using malloc() and calloc() functions where the
pointer is used.
Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and
improves the performance.