C Language
C Language
M I T – Knowledge Centre
C History
C is a programming language developed at AT &
T Laboratories of USA
Developed between 1969 and 1973 along with
Unix
Develop by Dennis Ritchie.
Designed for systems programming
Operating systems
Utility programs
Compilers
Filters
M I T – Knowledge Centre
C language
Currently, the most commonly-used
language for embedded systems
“High-level assembly”
Very portable: compilers exist for virtually
every processor
Easy-to-understand compilation
Produces efficient code
M I T – Knowledge Centre
Hello World in C Preprocessor used to
share information among
#include <stdio.h> source files
- Clumsy
{ + Very flexible
M I T – Knowledge Centre
Program mostly a
collection of functions
Hello World in C “main” function special:
the entry point
#include <stdio.h> “void” qualifier
indicates function does
not return anything
void main()
{
printf(“Hello, world!\n”);
} “void” qualifier indicates
function does not return
anything
M I T – Knowledge Centre
C Constants
Primary Constants
Integer
Real
Character
Secondary Constants
Array
Pointer
Structure
Union
Enum etc...
M I T – Knowledge Centre
C variables
In C, a quantity which may vary during
execution is called a Variable.
Variable names are names given to
locations in the memory of computer
where different constants are stored.
Locations may contain Integer, Real and
Character Constant.
M I T – Knowledge Centre
C Keywords
Keywords are the words whose meaning
has already been explained to the C
compiler.
The keywords cannot be used as variable
names because if we do so we are trying to
assign a new meaning to the keyword,
Which is not allowed by computer.
There are 32 keywords available in C
Like auto, double, if, int, static, long etc...
M I T – Knowledge Centre
C Instructions
Type Declaration Instruction
To declare the type of variable used in a C program.
Input / Output Instruction
To perform the function of supplying input data to a program
and obtaining the output result.
Arithmetic Instruction
To perform arithmetic operations between constants & variables.
Control Instruction
To control the sequence of execution of various stmts in a C
program.
M I T – Knowledge Centre
Simple Example
To calculate simple interest
Void main()
{
int p, n;
float r, si;
p=1000;
n=3;
r=8.5;
si=p*n*r/100;
M I T – Knowledge Centre
The Decision Control Structure
If statement
If else statement
The conditional operators
M I T – Knowledge Centre
If statement
The general form of if statement is
If ( this condition is true )
Execute this statement;
The keyword if tells the compiler that what
follows is a decision control instruction.
Example…
if ( 5 < 10 )
printf( "Five is now less than ten, that's a big surprise" );
M I T – Knowledge Centre
M I T – Knowledge Centre
If Else statement
The if statement is false then else part is
executed.
if ( TRUE )
{
/* Execute these statements if TRUE */
}
else
{
/* Execute these statements if FALSE */
}
M I T – Knowledge Centre
Example Nested if else
Void main()
{
int m1, m2, m3, m4, m5, per;
printf (“Enter marks in five subjects”);
scanf (“%d %d %d %d %d”, &m1, &m2, &m3, &m4, &m5);
per = (m1+m2+m3+m4+m5)/5;
if (per>=60)
printf (“First Division”);
else
{
if (per>=50)
printf (“Second Division”);
else
{
if (per>=40)
printf (“Third Division”);
else
printf (“Fail”);
}
}
}
M I T – Knowledge Centre
Conditional Operators
The Conditional Operators ? and : are sometimes
called ternary operator since they take three
arguments.
The general form is
M I T – Knowledge Centre
The loop control Structure
M I T – Knowledge Centre
The while loop
General form is
while ( condition )
{
Code to execute while the condition is true
}
M I T – Knowledge Centre
Understanding a while loop
Example of while: Calculation of simple interest for 3
sets of p, n and r
Void main()
{
int p, n, count;
float r, si;
count = 1;
while ( count <= 3 )
{
printf (“Enter values of p, n and r”);
scanf (“%d, %d, %f”, &p, &n, &r);
si = p * n * r /100;
printf ( “simple interest = Rs. %f”, si );
M I T – Knowledge Centre
The for loop
General form is
for ( initialize counter ; test counter ; increment counter )
{
do this;
and this;
and this;
}
M I T – Knowledge Centre
Example of for loop for Calculation of simple
interest for 3 sets of p, n and r
Void main()
{
int p, n, count;
float r, si;
for ( count = 1; count<=3; count = count+1 )
{
printf (“ Enter values of p, n, and r”);
scanf (“%d %d %f,”, &p, &n, &r );
si = p * n * r / 100;
printf (“Simple interest = Rs. %f”, si);
}
}
M I T – Knowledge Centre
Break and Continue statement
When the keyword break is encountered
inside any C loop, control automatically
passes to the first statement after the loop.
When the keyword continue is
encountered inside any C loop, control
automatically passes to the beginning of
the loop.
M I T – Knowledge Centre
The do-while loop
General form is
do
{
this;
and this;
and this;
}
While ( this condition is true );
M I T – Knowledge Centre
Example of do-while loop
Void main()
{
do
{
printf (“Hello”);
}
while ( 4 < 1 );
}
The program will execute once, then the condition is tested.
M I T – Knowledge Centre
The case Control Structure
M I T – Knowledge Centre
Decisions using Switch
The control statement which allows us to make a decision from the
number of choices is called a switch.
The keyword case is an integer or a character constant.
The general form is
switch ( integer expression )
{
case constant 1 :
do this;
case constant 2 :
do this;
case constant 3 :
do this;
default :
do this;
}
M I T – Knowledge Centre
Example of Switch statement
void main()
{
int i=2;
switch ( i )
{
case 1:
printf (“I am in case 1”);
case 2:
printf (“I am in case 2”);
case 3:
printf (“I am in case 3”);
default :
printf (“I am in default”);
}
}
M I T – Knowledge Centre
Output
I am in case 2
I am in case 3
I am in default
M I T – Knowledge Centre
Switch statement using break
void main()
{
int i=2;
switch ( i )
{
case 1:
printf (“I am in case 1”);
break;
case 2:
printf (“I am in case 2”);
break;
case 3:
printf (“I am in case 3”);
break;
default :
printf (“I am in default”);
}
}
M I T – Knowledge Centre
Some useful Tips
We can use case in any order as we want.
We can use char values in case and switch
statement.
We can mix integer and character
constants in different cases of a switch.
M I T – Knowledge Centre
The goto statement
Consider the following program
Void main()
{
int goals;
printf (“Enter the number of goals scored against India”);
scanf (“%d”, &goals);
if ( goals <= 5 )
goto sos;
else
{
printf (“About time soccer players learnt C\n”);
printf (“and said goodbye! to soccer”);
exit(); /*terminates program execution*/
}
sos;
printf (“To err in human!”);
}
M I T – Knowledge Centre
Output
Enter the number of goals scored against
India 3
To err is human!
Enter the number of goals scored against
India 7
About time soccer players learnt C
and said goodbye! to soccer
M I T – Knowledge Centre
Functions
What is Function
Passing values between functions
Scope rule of Functions
Pointers
M I T – Knowledge Centre
What is Function
A Function is a self contained blocks of statements that perform
coherent task of some kind.
A function gets called when the function name is followed by a
semicolon ( ; )
Let us look at a simple example.
Void main()
{
message();
printf (“\nAurangabad”);
}
message()
{
printf (“\nMIT”);
}
Output:
MIT
Aurangabad
M I T – Knowledge Centre
Example of Function
Void main()
{
printf (“\n I am in India”);
italy();
brazil();
argentina();
}
italy()
{
printf (“\n I am in Italy”);
}
brazil()
{
printf (“\n I am in Brazil”);
}
argentina()
{
printf(“\n I am in Argentina”);
}
M I T – Knowledge Centre
Passing values between Functions
/* Sending and receiving values between functions * /
Void main()
{
int a, b, c, sum;
Printf (“\n Enter any three numbers”);
Scanf (“%d %d %d”, &a, &b, &c);
Sum = calsum ( a, b, c);
Printf (“\n sum = %d”, sum);
}
calsum (x, y, z)
int x, y, z;
{
int d;
d = x + y + z;
return (d);
}
M I T – Knowledge Centre
Output
Enter any three number 10 20 30
Sum = 60
M I T – Knowledge Centre
Pointers
Definition
Example
M I T – Knowledge Centre
Pointers
Pointer are used to "point" to locations in
memory.
when we have a pointer, we need the
ability to both request the memory location
it stores and the value stored at that
memory location.
The general form is
<variable_type> *<name>;
M I T – Knowledge Centre
Example of Pointer
main() Is a format specifier for printing
an unsigned integer
{
int i=3;
Printf (“\n Address of i = %u”, &i);
Printf (“\n Value of i= %d”, i);
}
M I T – Knowledge Centre
Location Name
Output
Address of i = 6485
Value of i = 3
Location Number
Value at Location
M I T – Knowledge Centre
Arrays
What is array
Array Initialisation
Example
M I T – Knowledge Centre
What is Arrays
Lets take an example
main()
{
int x;
x = 5;
x = 10;
printf (“\n x=%d”, x);
}
No doubt, this program will print the value of x as 10. Why so?
Because when a value 10 is assigned to x, the earlier value
of x i.e. 5 is lost. So we use array to avoid this lost.
M I T – Knowledge Centre
Definition - Arrays
Thus array is a collection of similar elements. These
similar elements could be all ints, or all floats, or all
chars etc..
Usually, the array of chars is called a ‘string’,
whereas an array of ints or floats is called simply an
array.
Remember that all elements of any given array must
be of the same type. i.e. we cannot have an array of
10 numbers, of which 5 are ints and 5 are floats.
General form is
int examplearray[100]; /* This declares an array */
Array Initialisation
Let us see how to initialise an array.
int num[6] = { 2, 4, 12, 5, 45, 5 };
int n[ ] = { 2, 4,12, 5, 45, 5 };
float press[ ] = { 12.3, 34.2, -23.4, -11.3 }
Note the following points carefully:
Till the array elements are not given specific values, they are supposed
to contain garbage values.
If the array is initialised where it is declared, mentioning the dimension
of the array is optional.
M I T – Knowledge Centre
A simple program using Array to find
average marks obtained by a class of 30
students in a test
Void main()
{
float avg, sum = 0;
int i;
int marks[30]; /*array declaration*/
2 – Dimensional Array
3 - Dimensional Array
M I T – Knowledge Centre
2 – Dimensional Array
Initialising a 2 – Dimensional Array
int stud [4] [2] = {
{ 1234, 56 },
{ 1212, 33 },
{ 1434, 80 },
{ 1312, 78 }
};
Or
int stud [4] [3] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78};
M I T – Knowledge Centre
Continue…
It is important to remember that while initialising an array it
is necessary to mention the second (column) dimension,
whereas the first dimension (row) is optional.
Thus the declarations:
int arr[2][3] = {12, 34, 23, 45, 56, 45 };
int arr[ ][3] = {12, 34, 23, 45, 56, 45 };
are perfectly acceptable.
Whereas,
int arr[2][ ] = {12, 34, 23, 45, 56, 45 };
int arr[ ][ ] = {12, 34, 23, 45, 56, 45 };
Would never works.
M I T – Knowledge Centre
M I T – Knowledge Centre