You are on page 1of 24

C

Programming
Shamit Sarkhel
Data Types
• Primitive or Basic • Derived
– char – Using modifiers
– int • short
– float • long
– double • signed
• unsigned
– Array
• User defined or Abstract
• struct (structure) Used for declaring
• union variables / constants
• enum (enumerator) (memory locations)
Primitive or Basic
– char size 1 byte - 08 bits  range -128 to 127
• signed char  range -128 to 127
• unsigned char  range +000 to 256
– int  size 2 bytes - 16 bits  range -32768 to 32767
• signed int  range -32768 to 32767
• short int  range -32768 to 32767
• signed short int  range -32768 to 32767
• unsigned int  range 0 to 65535
• unsigned short int  range 0 to 65535
• long int  range -2147483648 to 2147483647
Size
4 bytes
• signed long int  range -2147483648 to 2147483647
• unsigned long int  range 0 to 4294967295
Primitive or Basic
– float size 4 bytes - 32 bits
range 3.4E-38 to 3.4E+38

– double  size 8 bytes - 64 bits


range 1.7E-308 to 1.7E+308

– long double  size 10 bytes - 80 bits


range 3.4E-4932 to 1.1E+4932
Keywords in C
• A keyword is a reserved word.
• It cannot be used as a variable or constant name
• There are only 32 keywords in the C language
which are as follows :
• auto, break, case, char, const, continue, default,
do, double, else, enum, extern, float, for, goto,
if, int, long, register, return, short, signed, sizeof,
static, struct, switch, typedef, union, unsigned,
void, volatile and while
Memory Declaration for I/O in C
• Variables • Constants
– Named memory location. – Named memory location.
– Value may be assigned during – Value must be assigned during
declaration, i.e., initialization is declaration, i.e., initialization is
optional. mandatory.
– Value is not fixed and can be – Value is fixed and cannot be
changed during execution. changed during execution.
– Value assigned using literals – Value assigned using literals
such as ‘A’, 10, 45.78, “ram” such as ‘A’, 10, 45.78, “ram”
etc. etc.
– Example : int n1, n2=19; – Example : int n1, n2=19;
– float salary, price=789.98f; – float salary, price=789.98f;
– char alpha, ans=‘y’; – char alpha, ans=‘y’;
error in n1, salary and alpha declaration
#include Preprocessor Directive
• It is used to paste code of given file into current file.
• It is used for including system-defined and user-defined
header files.
• If included file is not found, compiler renders error.
• By the use of #include directive, we provide
information to the preprocessor where to look for the
header files.
• There are two variants to use #include directive.
• #include <filename> and #include "filename"
• The #include <filename> tells the compiler to look for
the directory where system header files are held.
• The #include "filename" tells the compiler to look in
the current directory from where program is running.
Input in C
• The scanf ( ) function is used for input.
• It is defined in the header file – stdio.h which must be
included in the source code using #include statement.
• It reads the data input from the console.
• Syntax : scanf ( “data format string", list of memory locations );
• Example :
# include <stdio.h>
void main ( )
{
int i; float f; char c; double d; char name[20];
scanf(“%d %f %c %lf %s” , &i , &f , &c , &d , name);
}
Output in C
• The printf ( ) function is used for output.
• It is defined in the header file – stdio.h which must be
included in the source code using #include statement.
• It displays/outputs the data onto the console.
• Syntax : printf ( “data format string", list of memory locations );
• Example :
# include <stdio.h>
void main ( ) {
int roll; char name[20];
scanf(“%d %s” , &I , name);
printf(“Roll : %d \t Name : %s” , roll , name);
}
Processing Statement
• Decision Structure • Case Structure
– Simple “if” – “switch … case”
– Nested “if” • “break” Statement
– Complex (Ladder) “if”
• “continue” Statement
• “goto” Statement
• Iteration Structure
– “while” loop
– “do while” loop
– “for” loop
Decision Structure : Simple “if”
if ( boolean expression ) •else (false) part is
{ optional.
true case statements; •if (true) will be
………………………………..
executed if the boolean
} expression evaluates as
else true.
{
false case statements;
•else (false) part, if
……………………………….. present, will be executed
} if the boolean
expression evaluates to
false
Decision Structure : Simple “if”
if ( a > b )
{
printf ( “\n %d is greater than %d” , a , b );
}
else
{
printf ( “\n %d is greater than %d” , b , a );
}
Decision Structure : Simple “if”
if ( a > b )
printf ( “\n %d is greater than %d” , a , b );
else
printf ( “\n %d is greater than %d” , b , a );
Note :
1) \n is used for new line feed, i.e., printing from
next line.
2) { } are not required if block contains only one
statement.
Decision Structure : Nested “if”
if ( boolean expression ) if ( a > b ){  main if
if ( a > c ) {  nested if
{ printf(“%d”, a);
if ( boolean expression ) }
……………………………….. else {  nested else
printf(“%d”, c);
} }
else }
{ else{  main else
if ( b > c ){  nested if
if ( boolean expression )
printf(“%d”, b);
……………………………….. }
} else {  nested else
printf(“%d”, c);
}
}
Decision Structure : Complex “if”
if ( boolean expression ) if ( ( a > b ) && ( a > c ) ) {
printf ( “%d” , a);
{ ……… } }
else if ( boolean expression ) else if ( ( b > a ) && ( b > c ) ) {
{ ……… } printf ( “%d” , b);
else if ( boolean expression ) }
else if ( ( c > a ) && ( c > b ) ) {
{ ……… } printf ( “%d” , c);
else if ( boolean expression ) }
{ ……… } else if ( ( a == b ) && ( a == c ) ) {
………………………….. printf ( “All are equal” );
}
………………………….. else {
else printf ( “Any two are equal
{ ……… } and the third is smaller or
greater”);
}
Execution Control Transfer: “goto”
• The goto statement is known as jump statement
in C.
• It goto is used to transfer the program control to
a predefined label.
• The goto statement can be used to repeat some
part of the code for a particular condition.
• It can also be used instead of “break” to come
out of the multiple loops which can't be done by
using a single break statement.
• Use of goto must be avoided since it makes the
program complicated and less readable.
Execution Control Transfer: “goto”
• Syntax:
label:
<code block>;
goto label;
• Example :

#include <stdio.h>
void main()
{
int num, I = 1;
printf("Enter the number whose table you want to print? : ");
scanf ( "%d ", &num );
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10) goto table;
}
Iteration Structure : “while”
Initialization of control variable;
while ( boolean expression using control variable )
{
loop body statements;
………………………………..
increment / decrement of control variable;
}
• The while block executes till the boolean expression (based on
control variable) evaluates to true. The boolean expression is
checked, each time the last line of the while block is executed.
• The control variable must be incremented or decremented so
that the boolean expression evaluates to false, otherwise loop
will become ‘infinite loop’ and block will be executed infinitely.
• Once the boolean expression evaluates to false, the loop
terminates and the line next to the loop body is executed.
Iteration Structure : “while”
num = 1; Output :
while ( num <= 10 ) 1
{ 2 Proper
printf ( “\n%d” , num ); 3 looping
num++; … structure
} …
10
printf ( “\nLoop terminated” );
Loop terminated
Output :
num = 1; 1
while ( num <= 10 ) 1
{ 1
printf ( “\n%d” , num ); Infinite 1
} loop ...
printf ( “\nLoop terminated” ); 1
1

Iteration Structure : “do while”
num = 1; Output :
do { 1
printf ( “\n%d” , num ); 2
num++; 3
} while ( num <= 10 ) ; …

10
printf ( “\nLoop terminated” );
Loop terminated

•Condition is evaluated after executing the loop body.


•Therefore, it is called “Post-Test” or “bottom checking” loop.
•So the loop body will be executed at least once, even if the
loop condition evaluates to false. This may give undesirable
result.
•Therefore, while loop is preferred. It is used mainly in menu
driven program
“while” and “do while”
num = 1; Output :
do {
printf ( “\n%d” , num ); 1
num++;
} while ( num > 10 ) ;
Loop terminated
printf ( “\nLoop terminated” );

num = 1; Output :
while ( num > 10 )
{
printf ( “\n%d” , num );
Loop terminated
num++;
}
printf ( “\nLoop terminated” );
Iteration Structure : “for”

1 2 4
for ( initialization ; expression checking ; increment/decrement )
{
loop body
} 3
Iteration Structure : “for”
for ( num = 1 ; num <= 10 ; num++ )
{
printf ( “\n%d” , num );
}
printf ( “\nLoop terminated” );

– Has three parts, separated using ‘ ; ’


– First part is initialization part. More than one such initialization
can be coded, each separated by ‘ , ’
–Second part is expression checking part that evaluates a boolean
expression.
–Third part is increment / decrement part. More than one such
part can be coded, each separated by ‘ , ’
–Thus for loop combines the three essential section of a loop into
one line statement.
Case Structure : “switch…case”
Initialization / input of case variable ;  value provided by user
switch ( case variable ) {
case <case variable value 1> : <statement block> ; break ;
case < case variable value 2> : <statement block> ; break ;
………………..
[ default : <statement block> ; [ break ; ] ]  this is like else case
}
• A switch statement tests the value of a variable and compares it with
multiple cases. Once the case match is found, a block of statements
associated with that particular case is executed.
• Each case in a block of a switch has a different number which is
referred as an identifier. The value provided by the user is compared
with all the cases inside the switch block until the match is found.
• If a case match is not found, then the default statement is executed,
and the control goes out of the switch block.
• The case variable can be integer expression or a character expression.

You might also like