0% found this document useful (0 votes)
4 views

C Programming Books (1)

C is a mid-level structured programming language developed by Dennis Ritchie, formalized by ANSI in 1988, and widely used for system applications and various programming tasks. It is known for its efficiency, portability, and foundational role in modern programming languages, though it lacks object-oriented features. The document also covers basic syntax, compilation processes, and examples of C programs.

Uploaded by

royalemihir007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Programming Books (1)

C is a mid-level structured programming language developed by Dennis Ritchie, formalized by ANSI in 1988, and widely used for system applications and various programming tasks. It is known for its efficiency, portability, and foundational role in modern programming languages, though it lacks object-oriented features. The document also covers basic syntax, compilation processes, and examples of C programs.

Uploaded by

royalemihir007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Page 1 of 70

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.

Some Fact About C Programming Language


➢ In 1988, the American National Standards Institute (ANSI) had formalized the C
language.
➢ C was invented to write UNIX operating system.
➢ C is a successor of 'Basic Combined Programming Language' (BCPL) called B
language.
➢ Linux OS, PHP, and MySQL are written in C.
➢ C has been written in assembly language.

Used of C programming Language


➢ In the beginning, C was used for developing system applications, e.g. :
Database Systems
➢ Language Interpreters
➢ Compilers and Assemblers
➢ Operating Systems
➢ Network Drivers
➢ Word Processors

C Has Become very popular for various reasons


➢ One of the early programming languages.
➢ Still, the best programming language to learn quickly.
➢ C language is reliable, simple, and easy to use.
➢ C language is a structured language.
➢ Modern programming concepts are based on C.
➢ It can be compiled on a variety of computer platforms.
➢ Universities preferred to add C programming in their courseware.

Feature of C programming language


➢ C is a robust language with a rich set of built-in functions and operators.
➢ Programs written in C are efficient and fast.
➢ C is highly portable; programs once written in C can be run on other machines with
minor or no modification.
➢ C is a collection of C library functions; we can also create our function and add it to
the C library.
➢ C is easily extensible.

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.

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 2 of 70
C Programming
➢ The modular structure makes code debugging, maintenance, and testing easier.

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.

Basic Syntax in C Programming


Tokens
All identifiers, keywords, symbols, literals, etc are together known as tokens. For example,
printf, curly braces ({,}), round braces ((,)), semicolon (;), any string statements inside printf
or code are known as tokens.
Here printf, ‘(‘, ‘Enter the name:’, ‘)’ and ‘;’ are the tokens.

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 3 of 70
C Programming
printf (“Enter the name :”); sum = var1 + var2;

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.

How to compile and run the c program


There are 2 ways to compile and run the c program, by menu and by shortcut.

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 4 of 70
C Programming
Or, press ctrl+f9 keys compile and run the program directly.

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 5 of 70
C Programming
The source code is the code which is written in a text editor and the source code file is given
an extension ".c". This source code is first passed to the preprocessor, and then the
preprocessor expands this code. After expanding the code, the expanded code is passed to
the compiler.

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 6 of 70
C Programming
//Write a program to create sum of variable
#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a,b;

a=10;

b=5;

printf(“The value of A = %d”,a);

printf(“The value of B = %d”,b);

getch();

Output :-

The value of A =10

The value of B =5

//Write a program to Input two integer value and print


#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a,b;

printf (“Enter the value of A :”);

scanf(“%d”,&a);

printf (“Enter the value of B :”);

scanf (“%d”,&b);

printf(“The value of A = %d”,a);

printf(“The value of B = %d”,b);

getch();

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 7 of 70
C Programming
}

Output :-

Enter the value of A : 55

Enter the value of A : 40

The value of A =55

The value of B =40

//Write a program to Create Marksheet of three subject


#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int rno,maths,sci,eng,total;

printf (“Enter the Roll Number :”);

scanf(“%d”,&rno);

printf (“Enter the marks of Maths :”);

scanf (“%d”,&maths);

printf (“Enter the marks of Science :”);

scanf (“%d”,&sci);

printf (“Enter the marks of English :”);

scanf (“%d”,&eng);

total=maths+sci+eng;

print(“The marks of Maths = %d”,maths);

print(“The marks of Science = %d”,sci);

print(“The marks of English = %d”,eng);

print(“The total marks of = %d”,total);

getch();

//Write a program to find Area of Circle


#include<stdio.h>

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 8 of 70
C Programming
#include<conio.h>

void main()

clrscr();

float pi,radius,area;

pi=3.14;

printf(“Enter the value of Radius :”);

scanf(“%f”,&radius);

area=pi*radius*radius;

printf(“The area of Circle = %f”,area);

getch();

//Write a program to find interchange value of two integer


#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a,b,c;

printf (“Enter the value of A :”);

scanf(“%d”,&a);

printf (“Enter the value of B :”);

scanf (“%d”,&b);

c=a+b;

b=a-b;

a=a-b;

printf(“After interchange value of A & B \n “);

printf(“The value of A = %d”,a);

printf(“The value of B = %d”,b);

getch();

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 9 of 70
C Programming
}

//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;

printf (“Enter the value of A :”);

scanf(“%d”,&a);

printf (“Enter the value of B :”);

scanf (“%d”,&b);

c=a;

a=b;

b=c;

printf(“After interchange value of A & B \n “);

printf(“The value of A = %d”,a);

printf(“The value of B = %d”,b);

getch();

//Write a program to find simple interest


#include<stdio.h>

#include<conio.h>

void main()

clrscr();

float principle,r,year,s;

printf (“Enter the value of Principle:”);

scanf(“%f”,& principle);

printf (“Enter the value of Rate:”);

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 10 of 70
C Programming
scanf (“%f”,&r);

printf(“Enter the number of years “);

scanf(“%f”,&year);

s= principle*r*year/100;

printf(“The simple intrest are = %f”,s);

getch();

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 11 of 70
C Programming

❖ 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

An if statement consists of a Boolean expression followed by one or more statements.

Syntax

The syntax of an if statement in C programming language is:

if(boolean_expression)

/* statement(s) will execute if the boolean expression is true */

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 12 of 70
C Programming
If the boolean expression evaluates to true, then the block of code inside the if statement will be
executed. If boolean expression evaluates to false, then the first set of code after the end of the if
statement (after the closing curly brace) 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:-

#include int main ()

/* local variable definition */

int a = 10;

/* check the boolean condition using if statement */

if( a < 20 )

/* if condition is true then print the following */

printf("a is less than 20\n" );

printf("value of a is : %d\n", a);

return 0;

When the above code is compiled and executed, it produces the following result:

a is less than 20;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 13 of 70
C Programming
value of a is : 10

if...else statement

An if statement can be followed by an optional else statement, which executes when the boolean
expression is false.

Syntax

The syntax of an if...else statement in C programming language is:

if(boolean_expression)

/* statement(s) will execute if the boolean expression is true */

else

/* statement(s) will execute if the boolean expression is false */

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 :-

#include int main ()

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 14 of 70
C Programming
/* local variable definition */

int a = 100;

/* check the boolean condition */

if( a < 20 )

/* if condition is true then print the following */

printf("a is less than 20\n" );

else

/* if condition is false then print the following */

printf("a is not less than 20\n" );

printf("value of a is : %d\n", a);

return 0;

When the above code is compiled and executed, it produces the following result:

a is not less than 20;

value of a is : 100

The if...else if...else Statement


An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions using single if...else if statement.

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

The syntax of an if...else if...else statement in C programming language is:

if(boolean_expression 1)

/* Executes when the boolean expression 1 is true */

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 15 of 70
C Programming
}

else if( boolean_expression 2)

/* Executes when the boolean expression 2 is true */

else if( boolean_expression 3)

/* Executes when the boolean expression 3 is true */

else

/* executes when the none of the above condition is true */

Example :-

#include <stdio.h>

int main ()

/* local variable definition */

int a = 100;

/* check the boolean condition */

if( a == 10 )

/* if condition is true then print the following */

printf("Value of a is 10\n" );

else if( a == 20 )

/* if else if condition is true */

printf("Value of a is 20\n" );

TUTORIALS POINT Simply Easy Learning Page 40

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 16 of 70
C Programming
}

else if( a == 30 )

/* if else if condition is true */

printf("Value of a is 30\n" );

else

/* if none of the conditions is true */

printf("None of the values is matching\n" );

printf("Exact value of a is: %d\n", a );

return 0;

When the above code is compiled and executed, it produces the following result:

None of the values is matching

Exact value of a is: 100

Example:-
//Write a program to find maximum value of three integer values.

#include <stdio.h>

int main ()

Clrscr();

int a,b,c;

printf (“Enter the value of A :”);

scanf(“%d”,&a);

printf (“Enter the value of B :”);

scanf (“%d”,&b);

printf (“Enter the value of C :”);

scanf (“%d”,&c);

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 17 of 70
C Programming
if(a>b && a>c)

printf(“A is grater then of B,C”);

if else(b>a && b>c)

printf(“B is grater then of A,C”);

if else(c>a && c>b)

printf(“C is grater than of A, B”);

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;

printf (“Enter the Roll Number :”);

scanf(“%d”,&rno);

printf (“Enter the marks of Maths :”);

scanf (“%d”,&maths);

printf (“Enter the marks of Science :”);

scanf (“%d”,&sci);

printf (“Enter the marks of English :”);

scanf (“%d”,&eng);

total=maths+sci+eng;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 18 of 70
C Programming
per=total/3;

print(“The marks of Maths = %d”,maths);

print(“The marks of Science = %d”,sci);

print(“The marks of English = %d”,eng);

print(“The total marks of = %d”,total);

printf(“The percentage is = %d”,per);

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”)

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 19 of 70
C Programming
getch();

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 20 of 70
C Programming

❖ 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 */

/* you can have any number of case statements */

default :

/* Optional */

statement(s);

The following rules apply to a switch statement:

➢ 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.

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 21 of 70
C Programming
➢ A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true.
No break is needed in the default case.

Example

#include int main ()

/* local variable definition */

char grade = 'B';

switch(grade)

case 'A' :

printf("Excellent!\n" );

break;

case 'B' :

printf("Very Good!\n" );

break;

case 'C' :

printf("Well done\n" );

break;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 22 of 70
C Programming
case 'D' :

printf("You passed\n" );

break;

case 'F' :

printf("Better try again\n" );

break;

default :

printf("Invalid grade\n" );

printf("Your grade is %c\n", grade );

return 0;

When the above code is compiled and executed, it produces the following result:

Very Good

Your grade is B

//Write a program to choice week days

#include int main ()

Int day = '1';

switch(day)

case 1 :

printf("Sunday \n" );

break;

case 2 :

printf("Monday \n" );

break;

case 3 :

printf("Tuesday\n" );

break;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 23 of 70
C Programming
case 4 :

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;

//Write a program to create simple calculator

#include int main ()

Int a,b,c,d;

printf (“Enter the value of A :”);

scanf(“%d”,&a);

printf (“Enter the value of B :”);

scanf (“%d”,&b);

printf (“\n 1) Addition \n2) Subtraction \n3) Multiplication \n4) Division \n:”);

printf (“Enter Your Choice :”);

scanf (“%d”,&c);

switch(c)

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 24 of 70
C Programming
case 1 :

d=a+b;

printf("\n sum = %d”,d);

break;

case 2 :

d=a-b;

printf("\n Subtraction = %d”,d);

break;

case 3 :

d=a*b;

printf("\n Multiplication = %d”,d);

break;

case 4 :

d=a/b;

printf("\n Division = %d”,d);

break;

default :

printf("Out of Choice\n" );

return 0;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 25 of 70
C Programming

❖ 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.

Why use loops in C language?


The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the
program so that instead of writing the same code again and again, we can repeat the same code for
a finite number of times. For example, if we need to print the first 10 natural numbers then, instead
of using the printf statement 10 times, we can print inside a loop which runs up to 10 iterations.

Advantage of loops in C

1) It provides code reusability.


2) Using loops, we do not need to write the same code again and again.

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).

The syntax of do-while loop is given below:

Do

//code to be executed

while(condition);

Example :-

#include<stdio.h>

int main(){

int i=1;

do

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 26 of 70
C Programming
{

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;

printf("Enter a number: ");

scanf("%d",&number);

do

printf("%d \n",(number*i));

i++;

while(i<=10);

return 0;

Output

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 27 of 70
C Programming
Enter a number: 5
5
10
15
20
25
30
35
40
45
50
while loop in C
The while loop in c is to be used in the scenario where we don't know the number of iterations in
advance. The block of statements is executed in the while loop until the condition specified in the
while loop is satisfied. It is also called a pre-tested loop.

The syntax of while loop in c language is given below:

while(condition)

//code to be executed

Flowchart of while loop in C

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 28 of 70
C Programming
Example of the while loop in C language
Let's see the simple program of while loop that prints table of 1.

#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.

Write a program to find Even number


#include<stdio.h>

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 29 of 70
C Programming
int main()

int i=2;

while(i<=10)

printf("%d \n",i);

i=i+2;

return 0;

Output
2
4
6
8
10

Write a program to find Odd number


#include<stdio.h>

int main()

int i=1;

while(i<=10)

printf("%d \n",i);

i=i+2;

return 0;

Output
1
3
5

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 30 of 70
C Programming
7
9
Write a program to break (Reverse) the integer value
#include<stdio.h>

int main()

int i=123,B;

while(i>0)

B=i%10;

i=i/10;

printf(“%d”,B);

Return 0;

Output:-

321

//Write a program to find Armstrong number.


#include<stdio.h>

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)

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 31 of 70
C Programming
{

printf(“this is an Armstrong number “);

else

printf(“this is not an Armstrong number “);

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.

Syntax of for loop in C


The syntax of for loop in c language is given below:

for(Expression 1; Expression 2; Expression 3)

//code to be executed

Flowchart of for loop in C

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 32 of 70
C Programming

For loop Examples


Let's see the simple program of for loop that prints table of 1.

#include<stdio.h>

#include<conio.h>

int main()

int i=0;

for(i=1;i<=10;i++)

printf("%d \n",i);

return 0;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 33 of 70
C Programming
}

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;

//Write a program to find Factorial Number Output :-


#include<stdio.h>
1
#include<conio.h> 2
6
int main() 24
{ 120

int i,fact=1;

for(i=1;i<=5;i++)

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 34 of 70
C Programming
fact=fact+i;

printf("%d \n",fact);

return 0;

//Write a program to create Table of 5

#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;

//Write a program to find power of N number

#include<stdio.h>
Output :-
#include<conio.h>

int main() Enter the value of Base : 4


Enter the value of Power :2
{ Power of 4 number is : 16
int a,b,I,ans;

printf(“Enter the value of Base :”);

scanf(“%d”,&a);

printf(“Enter the value of Power :”);

scanf(“%d”,&b);

for(i=1;i<=a;i++)

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 35 of 70
C Programming
{

ans=ans*a;

printf("Power of %d numer is = %d \n",a,ans);

return 0;

//Write a program to Create Fibonacci series

#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.

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 36 of 70
C Programming
Syntax of Nested loop
Outer_loop

Inner_loop

// inner loop statements.

// outer loop statements.

Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while' loop.

Nested for loop

The nested for loop means any type of loop which is defined inside the 'for' loop.

for (initialization; condition; update)

for(initialization; condition; update)

// inner loop statements.

// outer loop statements.

1) Example of nested for loop

#include <stdio.h>
Output :-
int main()

{ Enter the value of n : 5

int i,n; 12345


12345
printf("Enter the value of n :");
12345
scanf(“%d”,&n); 12345
12345
// Displaying the n tables.

for(i=1;i<=n;i++) // outer loop

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 37 of 70
C Programming
{

for(int j=1;j<=5;j++) // inner loop

printf("%d",j); // printing the value.

printf("\n");

2) Example :-

#include <stdio.h>
Output :-
int main()

{ Enter the value of n : 5

int i,n; 11111


22222
printf("Enter the value of n :");
33333
scanf(“%d”,&n); 44444
55555
// Displaying the n tables.

for(i=1;i<=n;i++) // outer loop

for(int j=1;j<=5;j++) // inner loop

printf("%d",i); // printing the value.

printf("\n");

3) Example :-

#include <stdio.h>

int main()

int rows, i, j, number = 1;

printf("Enter the number of rows: ");

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 38 of 70
C Programming
scanf("%d", &rows);

for (i = 1; i <= rows; i++) Output :-


{
Enter the number of rows: 4
for (j = 1; j <= i; ++j)
1
{ 2 3
4 5 6
printf("%d ", number);
7 8 9 10
++number;

printf("\n");

return 0;

4) Example :-

#include <stdio.h> Output :-


int main()
Enter the value of n : 7
{
1
int i,n;
12
printf("Enter the value of n :"); 123
1234
scanf(“%d”,&n); 12345
123456
for(i=1;i<=n;i++)
1234567
{

for(int j=1;j<=i;j++)

printf("%d",j);

printf("\n");

5) Example :-

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 39 of 70
C Programming

#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()

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 40 of 70
C Programming
{ Output :-
int i;

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 main() Output :-


{

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()

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 41 of 70
C Programming
{
Output :-
int i;

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>

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 42 of 70
C Programming
int main()

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--)

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 43 of 70
C Programming
printf(" ",j );

for(k=1;k<=i;k++)

printf("*",k );

for(l=1;j<=i;j++)

printf("*",l );

printf("\n");

❖ Assignments for extra practice :-

ASCII stands for American Standard Code for Information Interchange. Below is the ASCII character
table, including descriptions…

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 44 of 70
C Programming

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 45 of 70
C Programming

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");

II. Alphabet Pattern: -

#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");

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 46 of 70
C Programming

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

1) Code Optimization: Less code to the access the data.

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.

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 47 of 70
C Programming
Disadvantage of 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];

Now, let us see the example to declare the array.

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;

int marks[5];//declaration of array

marks[0]=80;//initialization of array

marks[1]=60;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 48 of 70
C Programming
marks[2]=70;

marks[3]=85;

marks[4]=75;

//traversal of array

for(i=0;i<5;i++)

printf("%d \n",marks[i]);

}//end of for loop

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};

Let's see the C program to declare and initialize the array in C.

1) example :-
#include<stdio.h>

int main()

int i=0;

int marks[5]={20,30,40,50,60};//declaration and initialization of array

//traversal of array

for(i=0;i<5;i++)

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 49 of 70
C Programming
printf("%d \n",marks[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++)

printf(“enter the value of a [%d] : “,i)

scand(“%d”,&a[i]);

for(i=0;i<5;i++)

printf(“\nthe value of a [%d] : %d “,i,a[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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 50 of 70
C Programming

enter the value of a [0] : 40


enter the value of a [0] : 50

the value of a [0] :10


the value of a [0] :20
the value of a [0] :30
the value of a [0] :40
the value of a [0] :50
3) example :-
//write a program to sum of Array values

#include<stdio.h>

int main()

int i ,a[5],sum=0;

for(i=0;i<5;i++)

printf(“enter the value of a [%d] : “,i)

scand(“%d”,&a[i]);

sum=sum+a[i];

printf(“\nthe sum of array : %d”,sum);

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 51 of 70
C Programming

the sum of array : 24


4) example :-

//write a program to find maximum value of Array

#include<stdio.h>

int main()

int i ,a[5],max=0;

for(i=0;i<5;i++)

printf(“enter the value of a [%d] : “,i)

scand(“%d”,&a[i]);

max=a[0];

for(i=0;i<5;i++)

if(max<a[i])

max=a[i];

printf(“\n The maximum value of : %d”,max);

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 52 of 70
C Programming

the sum of array : 98

5) example :-

//write a program to create marksheet of three student using 1D Array

#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++)

printf(“enter the Roll Number : “);

scand(“%d”,&rno[i]);

printf(“enter the marks of Maths : “);

scand(“%d”,&maths[i]);

printf(“enter the marks of Science : “);

scand(“%d”,&sci[i]);

printf(“enter the marks of Computer : “);

scand(“%d”,&computer[i]);

total[i]=maths[i]+sci[i]+computer[i];

per[i]=total[i]/3;

printf(“enter your searching roll number : “);

scand(“%d”,&serach);

for(i=0;i<5;i++)

if(rno[i]==search])

printf(“\n The Roll number = %d”,rno[i]);

printf(“\n Marks of Maths = %d”,maths[i]);

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 53 of 70
C Programming
printf(“\n Marks of Science = %d”,sci[i]);

printf(“\n Marks of Computer = %d”,computer[i]);

printf(“\n Total marks of = %d”,total[i]);

printf(“\n Percentage of = %d”,per[i]);

return 0;

❖ Two Dimensional Array


The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices
which can be represented as the collection of rows and columns. However, 2D arrays are created to
implement a relational database lookalike data structure. It provides ease of holding the bulk of data
at once which can be passed to any number of functions wherever required.

❖ Declaration of two dimensional Array


The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];

Consider the following example.

int twodimen[4][3];

Here, 4 is the number of rows, and 3 is the number of columns.

❖ 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}};

❖ Two-dimensional array example


#include<stdio.h>

int main()

int i=0,j=0;

int arr[4][3]={

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 54 of 70
C Programming
{1,2,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++)

printf("arr[%d] [%d] = %d \n",i,j,arr[i][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];

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 55 of 70
C Programming
printf(“Enter the value of A: \n\n”);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

scanf("%d",&a[i][j]);

}//end of j

}//end of i

printf(“\n The value of A: \n\n”);

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 56 of 70
C Programming
2) example :-

// write a program to matrix sum of a and b


#include<stdio.h>

int main()

int i,j;

int a[3][3],b[3][3],sum[3][3];

printf(“Enter the matrix of A: \n\n”);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

scanf("%d",&a[i][j]);

printf(“Enter the matrix of B: \n\n”);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

scanf("%d",&b[i][j]);

printf(“\n The sum of A + B matrix : \n\n”);

for(i=0;i<3;i++)

for(j=0;j<3;j++)

sum[i][j]=a[i][j]+b[i][j];

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 57 of 70
C Programming
printf("%d\t",sum[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).

We can declare an array in the c language in the following way.

data_type array_name[size1][size2]....[sizeN];

data_type: Type of data to be stored in the array.

Here data_type is valid C/C++ data type

array_name: Name of the array

size1, size2,... ,sizeN: Sizes of the dimensions

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 58 of 70
C Programming
Initializing Three-Dimensional Array: Initialization in Three-Dimensional array is same as that of Two-
dimensional arrays. The difference is as the number of dimension increases so the number of nested
braces will also increase.

int x[2][3][4] =

{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },

{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }

};

Accessing elements in Three-Dimensional Arrays: Accessing elements in Three-Dimensional Arrays is


also similar to that of Two-Dimensional Arrays. The difference is we have to use three loops instead of
two loops for one additional dimension in Three-dimensional Arrays.

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 59 of 70
C Programming
❖ Functions
In c, we can divide a large program into the basic building blocks known as function. The function
contains the set of programming statements enclosed by {}. A function can be called multiple times to
provide reusability and modularity to the C program. In other words, we can say that the collection of
functions creates a program. The function is also known as procedureor subroutinein other
programming languages.

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.

SN C function aspects Syntax


1 Function declaration return_type function_name (argument list);
2 Function call function_name (argument_list)
3 Function definition return_type function_name (argument list)
{function body;}

The syntax of creating function in c language is given below:

return_type function_name(data_type parameter...)

//code to be executed

Types of Functions
There are two types of functions in C programming:

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 60 of 70
C Programming
1) Library Functions: are the functions which are declared in the C header files such as scanf(),
printf(), gets(), puts(), ceil(), floor() etc.
2) User-defined functions: are the functions which are created by the C programmer, so that he/she
can use it many times. It reduces the complexity of a big program and optimizes the code.

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.

Example without return value:

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.

Example with return value:


int get()

return 10;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 61 of 70
C Programming
Different aspects of function calling
A function may or may not accept any argument. It may or may not return any value. Based on these
facts, There are four different aspects of function calls.

➢ function without arguments and without return value


➢ function without arguments and with return value
➢ function with arguments and without return value
➢ function with arguments and with return value

Example for Function without argument and return value


#include<stdio.h>

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;

printf("\nGoing to calculate the sum of two numbers:");

result = sum();

printf("%d",result);

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 62 of 70
C Programming
int sum()

int a,b;

printf("\nEnter two numbers");

scanf("%d %d",&a,&b);

return a+b;

Output

Going to calculate the sum of two numbers:

Enter two numbers 10


24

The sum is 34

Example for Function with argument and without return value


#include<stdio.h>

void sum(int, int);

void main()

int a,b,result;

printf("\nGoing to calculate the sum of two numbers:");

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

sum(a,b);

void sum(int a, int b)

printf("\nThe sum is %d",a+b);

Output

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 63 of 70
C Programming

Going to calculate the sum of two numbers:

Enter two numbers 10


24

The sum is 34
Example for Function with argument and with return value
#include<stdio.h>

int sum(int, int);

void main()

int a,b,result;

printf("\nGoing to calculate the sum of two numbers:");

printf("\nEnter two numbers:");

scanf("%d %d",&a,&b);

result = sum(a,b);

printf("\nThe sum is : %d",result);

int sum(int a, int b)

return a+b;

Output

Going to calculate the sum of two numbers:


Enter two numbers:10
20
The sum is : 30

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 64 of 70
C Programming
➢ Call by value and Call by reference
There are two methods to pass the data into the function in C language, i.e., call by value and call by
reference.

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>

void change(int num)

printf("Before adding value inside function num=%d \n",num);

num=num+100;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 65 of 70
C Programming
printf("After adding value inside function num=%d \n", num);

int main()

int x=100;

printf("Before function call x=%d \n", x);

change(x);//passing value in function

printf("After function call x=%d \n", x);

return 0;

Output

Before function call x=100


Before adding value inside function num=100
After adding value inside function num=200
After function call x=100
Call by reference
➢ In call by reference, the address of the variable is passed into the function call as the actual
parameter.
➢ The value of the actual parameters can be modified by changing the formal parameters since
the address of the actual parameters is passed.
➢ In call by reference, the memory allocation is similar for both formal parameters and actual
parameters. All the operations in the function are performed on the value stored at the
address of the actual parameters, and the modified value gets stored at the same address.

Consider the following example for the call by reference.

#include<stdio.h>

void change(int *num) {

printf("Before adding value inside function num=%d \n",*num);

(*num) += 100;

printf("After adding value inside function num=%d \n", *num);

int main() {

int x=100;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 66 of 70
C Programming
printf("Before function call x=%d \n", x);

change(&x);//passing reference in function

printf("After function call x=%d \n", x);

return 0;

Output

Before function call x=100


Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
Difference between call by value and call by reference

No. Call by value Call by reference

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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 67 of 70
C Programming

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;

};

Declaring structure variable


We can declare a variable for the structure so that we can access the member of the structure easily.
There are two ways to declare structure variable:

1) By struct keyword within main() function


2) By declaring a variable at the time of defining the structure.

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;

};

Now write given code inside the main() function.

2nd way:

Let's see another way to declare variable at the time of defining the structure.

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 68 of 70
C Programming
struct employee

int id;

char name[50];

float salary;

}e1,e2;

Example :-
#include<stdio.h>

struct sum

int a,b,c;

};

void main()

clrscr();

sum x;

printf (“Enter the value of A :”);

scanf(“%d”,&x.a);

printf (“Enter the value of B :”);

scanf (“%d”,&x.b);

printf(“The value of A = %d”,x.a);

printf(“The value of B = %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

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 69 of 70
C Programming

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.

2) We can return multiple values from a function using the pointer.

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.

int *a;//pointer to int

char *c;//pointer to char

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.\

printf("Value of p variable is %d \n",*p); // As we know that * is used to dere


ference a pointer therefore if we print *p, we will get the value stored at the ad
dress contained by p.
return 0;

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.
Page 70 of 70
C Programming

}
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50

Usage of pointer

There are many applications of pointers in c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where the
pointer is used.

2) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and
improves the performance.

Global Computer Classes


Add:- FF-50, Kishan Complex , Opp Yash Complex , Gotri Main Road , Vadodara.

You might also like