C programming
C programming is one of the popular and powerful high level languages developed by Denish Ritchie. It was originally developed for development of UNIX operating system. Later its uses expand to application development as well as system program development. So, it is also known as middle level language. In brief,
Some of the implementation of C language are:
- v It is used to develop Operating System eg. UNIX.
- v It is used to develop new programming platform such as C++, C# [C++ is used in Blockchain Technology (Bitcoin, Etherium etc), also C++ is used in various game engine (Unreal engine and many more)]
- v It is used to develop popular web browser such as Mozilla Firefox, Thunderbird etc.
- v It is used in development of various GUIs (Graphical User Interface) and IDEs (Integrated Development Environments).
- v Popular Database Management System (DBMS) like MYSQL was developed by using C.
- v Some of the features of C language are:
- v It is a structural programming language. (i.e. it support various control structure like sequence, branching, looping)
- v It is a modular programming language. (i.e. big program can be reduced into simpler small program blocks called module)
- v It supports graphics.
- v It has huge library function.
- v It is case-sensitive language.
Data type:
C support both numeric
as well as alphanumeric datas. Frequently used Data types in C are:
Type |
Data type used |
Format Specifier |
Numeric |
int (for non-decimal numbers) |
%d |
Numeric |
float (for decimal numbers) |
%f |
Alphanumeric |
char (for string) |
%s or %c |
Variable: Those entities which hold either numeric or alphanumeric
values in program and may change its value throughout the time of program
execution are known as variables.
Rule for writing variable:
- v Variable name should not start will number. Eg, 1age is invalid, age1 is valid
- v Variable name should not have blank space. Eg, first name is invalid, firstname is valid
- v Keywords cannot be used as variable name. Eg, printf = 2; is invalid, num = 2; is valid
- v Uppercase variable name are different from lowercase variable name. Eg, age = 2 is different from AGE = 2
Operators: The special symbol or sign used to perform some
specific function or operation are called operators.
Types of operators:
a) Arithmetic operator
+ , - , * , / , %
Note that Here, if c = 5/2
The result will be 2 if we
initialize ‘c’ as int c.
The result will be 2.5 if we
initialize ‘c’ as float c.
If c = 5%2 then result will be 1.
Since, % gives remainder after division.
b) Relational operator
> , < , >= , <= , !=
(not equal to) , == (equal to)
[Note: Here in C language if you
want to compare the equal to is referred by == (two equals)]
c) Assignment operator
= is an assignment operator.
[Note If a = 2 then it mean 2 is
assigned to variable a, it does not mean a also has value 2 and both are
equal.]
d) Logical operator
1. For
logical AND use &&
2. For
logical OR use ||
3. For
logical NOT use !
Header files in C
It is a file in C library with .h
extension and contains several functions declaration and definition. Such as we
use.
#include for standard input
output function eg, printf, scanf etc
#include for mathematical
function eg, pow, sin, cos etc
#include for graphical element
eg, circle, rectangle, line etc
#include for string handling
function eg, strcpy(), strlen(),strupr() etc
And more. [header file can be
added according to requirements]
Sample structure of C program:
#include
#include
void main ( )
{
getch ( );
}
Explanation: C program always execute from top left to right
bottom, so program execution starts from header file which is followed by main(
) function. Remember that main( ) is always executed first. Since, main( ) is a
function it will return some values so we write void in front of main( ) as a
return type which mean null value is returned. getch( ) will hold our output on
the screen unless an until user press some keys. It means get character. All
other statements written inside main( ) function will be executed sequentially.
If you are using void and getch don’t forget to also include conio.h as header
file.
Same program can be written as
#include
int main( )
{
return 0;
}
Here, since we have written int
main( ) The main( ) function should return integer value so we write return 0
instead of getch( )here. While using int main( ) and return 0, we don’t need to
include conio.h
Output statement:
Output in C program can be
displayed by using ‘printf’ statement.
A) In order to display only
character we can use following syntax
printf(“Sample text”);
This will display anything that
is written inside double quotation mark (“ “)
B) In order to display values of
variable we can use following syntax
printf(“format specifier”, list
of variables,…);
Eg,
int a = 2, b = 3, c;
c = a+b;
printf(“Sum is %d”, c);
It will display: Sum is c
[Note that any number of format
specifier and variable can be displayed]
Same program can be display as
printf(“Sum of %d and %d is %d”,
a, b, c);
It will display: Sum of 2 and 3
is 5
[Note: if a, b and c was
initialized as float than every %d should be replaced by %f]
Input statement:
Input in C program can be taken
by using ‘scanf’ statement.
In order to take input from
user we can use following syntax
scanf(“format specifiers”,
&variablename1, &variablename2, ….);
Suppose we want to take a and b
as input from user. To take number input we can write
int a, b;
scanf(“%d %d”, &a, &b);
In this example two numeric
variable are initialized as integer. So, we write two %d inside double
quotation followed by &variablename. & denotes address of that
variable.
Another example,
If we want to take principal,
time and rate than it can be written as
float p, t, r;
scanf(“%f %f %f”, &p, &t,
&r);
In this example three numeric variable
are initialized as float. Since, their values may be in decimal So, we write
three %f inside double quotation followed by &variablename. & denotes
address of that variable.
If we want to take character or
string as an input than first string variable should be initialized as follows:
char fname[10];
Remember if we initialized
variable as ‘char’ than variable used become string. Since string in C is array
of character we should suffix variable name with size [size] i.e maximum length
of character that the variable can hold.
[Note: While taking string as an
input we don’t need to write & in variable name]
Example
char fname[10], lname[10];
scanf(“%s %s”, fname, lname);
Since variable are initialized as
array of character we don’t need to mention & while using scanf.
Control structure in C:
Since C is a structural programming language, we can change the flow of program execution according to the requirement of the user. Following are the control structure used in C.
Control Structure used in C |
||
Sequence |
Branching |
Looping |
Program flows from top to bottom sequentially. |
Conditional Branching: if, if else, else if, switch |
for |
|
Unconditional Branching: goto |
while |
|
|
do |
Sequence: Program flows from top to bottom sequentially with out changing the flow of program execution
[Note: Don't forget to add header
file in each program]
Important C Program example
1. WAP to calculate the area of a rectangle in
c. [Hints: a = l x b]
#include<stdio.h>
int main()
{
int l, b, a;
printf (“Enter
Length: ”);
scanf (“%d”,
&l);
printf (“Enter
Breadth: “);
scanf (“%d”,
&b);
a = l * b;
printf (“The area
is %d”, a);
return 0;
}
2. WAP to calculate the simple interest in c.
[Hints: i= (ptr)/100]
#include<stdio.h>
int main()
{
float p, t, r, i;
printf (“Enter
principal time and rate: ”);
scanf (“%f %f %f”,
&p, &t, &r);
i= (p*t*r)/100;
printf (“The
interest is %f”, i);
return 0;
}
WAP to calculate the average of 3 number in c. [Hints: av=
(a+b+c)/3]
#include<stdio.h>
int main()
{
float a, b, c, av;
printf (“Enter 3
numbers: ”);
scanf (“%f %f %f”,
&a, &b, &rc);
av= (a+b+c)/3;
printf (“The
average is %f”, av);
return 0;
}
3. WAP to calculate the volume of cylinder in
c. [Hints: v= pi*r*r*h]
#include<stdio.h>
int main()
{
float pi=3.14, r,
h, v;
printf (“Enter
radius and height: ”);
scanf (“%f %f ”,
&r, &h);
v= pi*r*r*h;
printf (“The
volume is %f”, v);
return 0;
}
4. WAP to convert days into respective years,
months and days.
#include<stdio.h>
int main()
{
int days, y, m,d,
rd;
printf (“Enter
days”);
scanf (“%d”,
&days);
y = days/365;
rd = days%365;
m = rd/30;
d = rd%30;
printf (“Year = %d
Month = %d Day = %d", y, m, d);
return 0;
}
Branching: Program
flows can be changed as per the requirement of the user with or without
condition.
Conditional
Branching: Flow of program execution changes according to the
condition supplied by the user.
a) if statement
b) if else statement
c) else if ladder
a) if statement syntax:
if (condition)
{
block of statements;
}
Program example of if statement
#include<stdio.h>
int main()
{
float p;
printf (“Enter
percentage ”);
scanf (“%f”,
&p);
if (p>=40)
{
printf (“You are
Pass");
}
return 0;
}
#include<stdio.h>
int main()
{
int a;
printf (“Enter
your age ”);
scanf (“%d”,
&a);
if (a>=18)
{
printf (“You are
eligible to vote");
}
return 0;
}
b) if else statement: This statement will execute block
of statement1 if the condition is true other wise will execute block of
statement2 if the condition is false. syntax:
if (condition)
{
block of statements1;
}
else
{
block of statements2;
}
Program example of if statement
#include<stdio.h>
int main()
{
float p;
printf (“Enter
percentage ”);
scanf (“%f”,
&p);
if (p>=40)
{
printf (“You are
Pass");
}
else
{
printf("You
are fail");
}
return 0;
}
#include<stdio.h>
int main()
{
int a;
printf (“Enter
your age ”);
scanf (“%d”,
&a);
if (a>=18)
{
printf (“You are
eligible to vote");
}
else
{
printf("You
are not eligible to vote");
}
return 0;
}
5. Write a program to find greatest among two
number
#include<stdio.h>
int main()
{
int a,b;
printf (“Enter two
number”);
scanf (“%d %d”,
&a, &b);
if (a>b)
{
printf (“%d is
greatest", a);
}
else
{
printf (“%d is
greatest", b);
}
return 0;
}
6. Write a program to check whether given
number is odd or even
#include<stdio.h>
int main()
{
int n, r ;
printf (“Enter
number”);
scanf (“%d”,
&n);
r = n%2;
if (r ==0)
{
printf (“%d is
even", n);
}
else
{
printf (“%d is
odd", n);
}
return 0;
}
7. Write a program to check whether given
number is positive or negative
#include<stdio.h>
int main()
{
int n;
printf (“Enter
number”);
scanf (“%d”,
&n);
if (n>0)
{
printf (“%d is
positive", n);
}
else
{
printf (“%d is
negative", n);
}
return 0;
}
c) else if ladder This statement will execute block of
statement1 when condition1 is true, similarly will execute block of statement2
when condition2 is true, like wise block of statement_n will execute when
condition_n is true. Default statement will be executed when none of the
condition is true.syntax:
if (condition1)
{
block of statements1;
}
else if (condition2)
{
block of statements2;
}
else if (condition3)
{
block of statements3;
}
.
.
else
{
default statement;
}
Program example of if statement [Note: Don't forget to add
header file]
8. Write a program to check whether given
number is positive, negative or zero.
#include<stdio.h>
int main()
{
int n;
printf (“Enter
number”);
scanf (“%d”,
&n);
if (n>0)
{
printf (“%d is
positive", n);
}
else if (n<0)
{
printf (“%d is
negative", n);
}
else
{
printf("%d is
zero", n);
}
return 0;
}
9. Write a program to find greatest among
three number.
#include<stdio.h>
int main()
{
int a, b, c;
printf (“Enter 3
number”);
scanf (“%d %d %d”,
&a, &b, &c);
if (a>b
&& a>c)
{
printf (“%d is
greatest", a);
}
else if (b>a
&& b>c)
{
printf (“%d is
greatest", b);
}
else
{
printf (“%d is
greatest", c);
}
return 0;
}
10. Write a program to input percentage and
check whether he/she secure distinction, first division, second division, third
division or fail.
#include<stdio.h>
int main()
{
float p;
printf (“Enter
percentage”);
scanf (“%f”,
&p);
if (p>=80)
{
printf (“%f is
Distinction", p);
}
else if (p>=60
&& p<80)
{
printf (“%f is
First division", p);
}
else if (p>=50
&& p<60)
{
printf (“%f is
Second division", p);
}
else if (p>=40
&& p<50)
{
printf (“%f is
Third division", p);
}
else
{
printf (“%f is
Fail", p);
}
return 0;
}
11. Calculate Total electricity bill on the
basis of following datas.
Unit Consumed |
Charge per unit |
≤ 50 unit |
Rs 10/unit |
>50 and ≤100 |
Rs 12/unit |
>100 |
Rs 15/unit |
#include<stdio.h>
int main()
{
float u, rs;
printf (“Enter
unit consumed”);
scanf (“%f”,
&u);
if (u<=50)
{
rs = u*10;
printf (“Total
amount is %f", rs);
}
else if (u>50
&& u<=100)
{
rs = 50*10 +
(u-50)*12;
printf (“Total
amount is %f", rs);
}
else
{
rs = 50*10 + 50*12
+ (u-100)*15;
printf (“Total
amount is %f", rs);
}
return 0;
}
Unconditional Branching: Flow of program execution
changes without condition.
a) goto statement syntax
goto label;
block of statements;
label:
Here flow of program execution will go down directly from
goto to label without any condition skipping all the block of statements
within.
OR
label:
block of statements;
goto label;
Here flow of program execution will go up directly from goto
to label without any condition.
Suppose we want to take percentage from the user and check
whether he/she is pass or fail keeping pass mark to be 40. Let us make our
program will not accept percentage greater than 100.
#include<stdio.h>
int main()
{
float p;
label:
printf (“Enter
percentage ”);
scanf (“%f”,
&p);
if (P>100)
{
printf("Please enter value between
0-100");
goto label;
}
if (p>=40)
{
printf (“You are
Pass");
}
else
{
printf("You
are fail");
}
return 0;
}
12. To print 1
to 100
#include<stdio.h>
int
main()
{
int
i;
for(i=1;i<=100;i=i+1)
{
printf("%d\t",i);
}
return
0;
}
13. To print
1,3,5……………10th term
#include<stdio.h>
int
main()
{
int
i;
for(i=1;i<=10;i=i+2)
{
printf("%d\t",i);
}
return
0;
}
14. To print
2,4,6……………10th term
#include<stdio.h>
int
main()
{
int
i;
for(i=2;i<=10;i=i+2)
{
printf("%d\t",i);
}
return
0;
}
15. To print
1,8,27,64……………10th term
#include<stdio.h>
int
main()
{
int
i, a;
for(i=1;i<=10;i=i+1)
{
a=i*i*i;
printf("%d\t",a);
}
return
0;
}
16. To print
5,25,125……………10th term
#include<stdio.h>
#include<math.h>
int
main()
{
int
i, a;
for(i=1;i<=10;i=i+1)
{
a=pow(5,i);
printf("%d\t",a);
}
return
0;
}
17. To print all
numbers from 100 to 1 with their sum.
#include<stdio.h>
int
main()
{
int
i,sum=0;
for(i=100;i>=1;i--)
{
printf("%d\t",i);
sum=sum+i;
}
printf("Their
sum = %d ",sum);
return
0;
}
18. To print 0,1,1,2,3,5,……………10th
term
#include<stdio.h>
int main()
{
int a=0,b=1,c,i;
printf("%d %d",a,b);
for(i=2;i<10;++i)
{
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
return 0;
}
19. To print 7,22,11,34……………10th term
#include<stdio.h>
int main()
{
int r, n = 7,i;
for(i=1;i<=10;i++)
{
printf ("%d\t", n);
r=n%2;
if(r==0)
n = n / 2;
else
n= 3 * n + 1;
}
return 0;
}