Programming in ‘C’ and
Data Structure
Dr. Anand Kumar
M.S. Engineering College
Bangalore
The 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
Fairly concise
C History
Developed between 1969
and 1973 along with Unix
Due mostly to Dennis
Ritchie
Designed for systems
programming
– Operating systems
– Utility programs
– Compilers
– Filters
Evolved from B, which
evolved from BCPL
Hello World in C
Program mostly a
collection of functions
#include <stdio.h> “main” function special:
the entry point
“void” qualifier indicates
void main() function does not return
anything
{
printf(“Hello, world!\n”);
}
I/O performed by a library
function: not included in
the language
Creating Programs
C libaray
Edit hello.c compile hello.o Link hello
Source File Object File
Executable
(High-Level Languages) (Machine Languages)
Basic Programming Concepts
• Comment To explain what the program is for and why it
is the way it is.
• Preprocessing Done before the program is compiled.
• To include header files containing information about C libraries
• Statement A line of code that tells computer to perform
some action. Always ended with a semicolon(;).
• Invoking functions
• Function A module often consisting of a set of
statements.
• Two functions in the hello world program: main, printf
CHARACTER SET
The characters used to form word,
number, expressions.
1. Letter – (A-Z, a-z)
2. Digits – (0-9)
3. Special Characters – (,.;: etc)
4. White Space – (Blank Tab, Tab etc)
5. Trigraph Characters- (??= - #) for the
keyboard which does not support some
characters
C Tokens
Individual words and punctuation marks
are called Tokens.
1. Keywords- fixed meaning and cannot be
changed. Ex – int , if etc.
2. Identifiers – variable names defined by user.
3. Constants – 10,-20
4. Strings – “MCA”
5. Operators - *,+
6. Special symbols- { } [ ]
Identifiers Rule
First character must be an alphabet
(or Underscore)
Must consist of only letter, digits or
Underscore
Cannot use Keyword or white space.
Constants
Numeric
– Integer (10, 56789U- unsigned Integer)
– Real (10.06, 2.15e2 here e2 means 10 pow 2)
Character
– Single character –(‘a’)
– String – (“abc”, “???.....!”,etc )
– Backslash character constant – used in output
function. Ex- “\n”, “\t” (Page 29)
Variables
• Variable Name for a memory object. Variable
names must start with letters and contain
letters, digits, and underscores.
• a, b, i, j, counter, number_of_points, c1234, …
• Type What the variable represents. Could be of
integer, floating point number, character, string,
and many others.
• int, float, double, char, …
• Declaration Tells compiler about variables and
their type.
• int i,j;
• float sum;
Variables
The following variable types can be
signed or unsigned:
signed char (8 bits) –128 to +127
signed short (16 bits) –32768 to +32767
signed int (16 bits) –32768 to +32767
signed long (32 bits) –2147483648 to +2147483648
unsigned char (8 bits) 0 to + 255
unsigned short (16 bits) 0 to + 65535
unsigned int (16 bits) 0 to + 65535
unsigned long (32 bits) 0 to + 4294967295
Decision Making and Branching
If….
If…else
Nested if …else
If…else if….else
If…else if….else (else if ladder)
The conditions are evaluated from top to
down.
As soon as true condition is found the
statements associated with it executed
and the control is transferred to out of the
ladder.
When all the conditions are false, the final
else containing the default statement is
executed.
Cont…
If (Cond 1)
stmt1
else if(cond2)
stmt2
else if (cond3)
stmt3
else
default stmt..
Example
The marks obtained by a student in 5 different subjects are
input through the keyboard. The student gets a division as
per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the
student.
Sol-1
if ( per >= 60 )
printf ( "First division ") ;
else
{
if ( per >= 50 )
printf ( "Second division" ) ;
else
{
if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "Fail" ) ;
}
}
Sol-2
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
if ( per < 40 )
printf ( "Fail" ) ;
Sol-3 based on else if ladder
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
Dangling else
In nested if statements, when a single
“else clause” occurs, the situation happens
to be dangling else!
For example:
if (condition)
if (condition)
if (condition)
else
printf("dangling else!\n");
Decisions Using switch
The control statement that allows us to
make a decision from the number of
choices is called a switch, or more
correctly a switch-case-default, since
these three keywords go together to make
up the control statement.
Nested switch is also permitted.
switch ( expression )
{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
Example
main()
{ The
int i = 2 ; outpu
switch ( i ) t of
{ this
case 1 : progr
printf ( "I am in case 1 \n" ) ; am
break ; would
case 2 : be:
I am in case 2
printf ( "I am in case 2 \n" ) ;
break ;
case 3 :
printf ( "I am in case 3 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
}
Example
main( )
{
The output of this
int i = 2 ; program would be:
I am in case 2
switch ( i ) I am in case 3
I am in default
{
case 1 :
printf ( "I am in case 1 \n" ) ;
case 2 :
printf ( "I am in case 2 \n" ) ;
case 3 :
printf ( "I am in case 3 \n" ) ;
default :
printf ( "I am in default \n" ) ;
}
}
Looping(Iterative Statements)
This involves repeating some portion of
the program either a specified number of
times or until a particular condition is
being satisfied. This repetitive operation is
done through program.
They are:
– (a) Using a for statement
– (b) Using a while statement
– (c) Using a do-while statement
Types of loop
Counter controlled loop- known how many
times.
Sentinel controlled loop- does not know
how many times. (Ex- digits manipulation)
for loop
for(initialization; conditional_expression; update_expression)
{
statements;
……………………..
……………………..
}
while loop
initialise loop counter ;
while ( conditional expression )
{
statements;
………………
increment / decrement loop counter;
}
do……………while()
do … while() loop is used when the body
of the loop is executed at least once.
Firstly body of the loop is executed, and
then condition is checked.
do
{
statements;
}
while ( conditional expression );
Difference while and do…while
while() do….while()
Condition is executed first then if true, Body of the loop is executed first and
body of the loop is executed. then condition is checked. If condition
is true, loop is repeated.
Syntax: Syntax:
Loop initialization; do
While(conditional expression) {
{ statements;
statements; }
} While(conditional expression);
Loop may be executed zero times Minimum one time loop is executed
Pre test looping Post test looping
Example : any Example : any
goto statement
The goto statement transfers control to a
label.
goto statement provides unconditional
jump to a defined label.
Goto statement is usually used with if
conditional statement.
Syntax :
goto label_name;
label_name:
goto statement
goto down;
--------
--------
--------
down:
Statement;
It skips statements and Called forward jump
goto statement
(unconditional & jumping statement)
above:
---------
---------
---------
---------
goto above;
Statement;
It creates loop called backward jump
break
(unconditional & jumping statement)
break statement is used to jump out of a
loop instantly.
When break is encountered inside any
loop, control automatically passes to the
first statement after the loop.
A break is usually associated with an
if.
Cont..
In case of nested loop, break comes out from the loop
containing it.
while(……..)
{
while(…….)
{
if (condition)
break;
}
---
}
continue statement
(unconditional & jumping statement)
Continue statement breaks the current
iteration. In other words, the continue
statement breaks the current execution of
the loop.
Skipping a part of a loop
continue;
While(condition)
{
-----------;
if (condition)
continue;
---------;
---------;
}
Cont…
Do
{
------------;
if(condition)
continue;
------------;
------------;
}
While(Condition);
Cont..
For(….;……..;…….)
{
---------------;
---------------;
if (condition)
continue;
----------------;
----------------;
}
Ternary (conditional)Operator
Ternary Operator is a conditional operator
which takes three argument.
Ternary (conditional)Operator
exp1 ? exp2 : exp3;
cond. true false
example : x = (a>b) ? a : b;
=> if (a>b)
x=a;
else
x=b;