Module I
Module I
1
Introduction
2
Constants, Variables and Keywords
The alphabets, numbers and special symbols when properly combined form
constants, variables and keywords.
Types of C Constants
The three primary constants and variable types in C are integer, float and
character.
Variable
3
Variable names are names given to locations in memory. These locations can
contain integer, real or character constants.The types of variables depend on the
types of constants that it can handle.For eg, an integer variable can hold only an
integer constant, a real variable can hold only a real constant and a character
variable can hold only a character constant.
Variable declaration
C compiler made it compulsory to declare the type of any variable name that you
wish to use in a program. This type declaration is done at the beginning of the
program. Following are the examples of type declaration statements:
• float bassal ;
• char code ;
C Keywords
Keywords are the words whose meaning has already been explained to the C
compiler.(or they have some predefined meaning).The keywords cannot be used
as variable names. The keywords are also called ‘Reserved words’.There are only
32 keywords available in C
4
C Instructions
2. Arithmetic Instruction
3. Control Instruction
Eg. C Program
main( )
5
{
num2=500;
9. Any variable used in the program must be declared before using it. For
example,
int p, n ;
6
float r, si ;
Note: The ampersand (&) before the variables in the scanf( ) function is a must.
& is an ‘Address of’ operator. It gives the location number used by the variable
in memory. When we say &a, we are telling scanf( ) at which memory location
should it store the value supplied by the user from the keyboard.
Control Instructions in C
The Sequence control instruction ensures that the instructions are executed in
the same order in which they appear in the program.
Decision and Case control instructions allow the computer to take a decision as
to which instruction is to be executed next.
The if statement
7
switch statement.
The if Statement
if ( condition )
statement ;
/* Demonstration of if statement */
main( )
int num ;
if ( num > 0 )
8
of statements after the if up to and not including the else is called an ‘if
block’. Similarly, the statements after the else form the ‘else block’.
if ( condition )
statement ;
else
statement ;
if - else Example
main( )
int num ;
if ( num > 0 )
else
Nested if-elses
if we write an entire if-else construct within either the body of the if statement or
the body of an else statement , it is called ‘nesting’of ifs.
9
main( )
int num ;
else
else
Logical Operators
1. && ( AND)
2. || ( OR)
3. ! (NOT)
( Don’t use the single symbol | and &. These single symbols also have a
meaning.)
10
The first two operators, && and ||, allow two or more conditions to be combined
in an if statement.The ! operator reverses the result of the expression it operates
on. For example, if the expression evaluates to a non-zero value, then applying !
operator to it results into a 0 and Vice versa.
Eg: ! ( y < 10 ) means “not y less than 10”. if y is less than 10, the expression will
be false. We can express the same condition as ( y >= 10 ).
The conditional operators ? and : are sometimes called ternary operators since
they take three arguments.In fact, they form a kind of foreshortened if-then-else.
Their general form is,
This means “if expression 1 is true, then the value returned will be expression 2,
otherwise the value returned will be expression 3”.
int x, y ;
y=(x>5?3:4);
if ( x > 5 )
y=3;
else
y=4;
11
Loop Control Structures
WHILE LOOP
statement1 ;
…….
Statement n;
The statements within the while loop would keep on getting executed till the
condition being tested remains true. When the condition becomes false, the
control passes to the first statement that follows the body of the while loop.
12
The condition being tested may use relational or logical operators as shown in the
following examples:
while ( i <= 10 )
Example1
i=1;
while ( i <= 5 )
i = i +1;
Programming in C
Programming in C
Programming in C
Programming in C
Programming in C
Example2
i=0;
while ( i <= 10 )
13
printf ( "%d\t”, i);
i = i +1;
0 1 2 3 4 5 6 7 8 9 10
Do while Loop
Using the do-while loop, we can repeat the execution of statements within the
loop .The do while loop is a post tested loop. That is the condition is tested after
executing the body of the loop. The do-while loop is mainly used in the case
where we need to execute the loop at least once.
Syntax
do
14
{
Statement 1;
…..
Statement n;
main()
int count=1;
do
printf(“hello”);
count++;
There is a minor difference between the working of while and do while loops. This
difference is the place where the condition is tested. The while tests the condition
before executing any of the statements within the while loop. As against this, the
do-while tests the condition after having executed the statements within the
loop. This means that do-while would execute its statements at least once, even if
the condition fails for the first time. The while, on the other hand will not execute
its statements if the condition fails for the first time.
15
int i=10;
While( i<=5)
{
printf(“loop
executed”);
}
printf(“program over”);
int i=10;
do
Here the output will be:
{
program over
printf(“loop
executed”);
}
While( i<=5);
printf(“program over”);
Here the output will be:
loop executed
program over
This means that the
body of the loop is
16
statement 1;
statement 2;
………
statement n;
When the for statement is executed for the first time, the value of count is set to
an initial value 1.Now the condition is tested and if it is satisfied, the body of the
loop is executed for the first time.Upon reaching the closing brace of for, control
is sent back to the for statement, where the value of count gets incremented by
1.Again the test is performed to check whether the new value of count satisfies
the condition.The process is repeated and when the condition becomes false,
control exits from the loop and is transferred to the statement (if any)
immediately after the body of for.
The for loop allows us to specify three things about a loop in a single line:
2. Testing the loop counter to determine whether its value has reached the no
of repetitions required.
3. Increasing the value of loop counter each time the program statements
within the loop has been executed.
17
Explanation for- with EXAMPLE
main()
int count;
printf(“hello”);
When the for statement is executed for the first time, the value of count is set to
an initial value 1.Now the condition count <= 3 is tested. Since count is 1 the
condition is satisfied and the body of the loop is executed for the first time.Upon
reaching the closing brace of for, control is sent back to the for statement, where
the value of count gets incremented by 1.Again the test is performed to check
whether the new value of count exceeds 3.If the value of count is still within the
range 1 to 3, the statements within the braces of for are executed again.The body
of the for loop continues to get executed till count doesn’t exceed the final value
3.When count reaches the value 4 the control exits from the loop and is
transferred to the statement (if any) immediately after the body of for.
18
• Eg:2
main( )
int i ;
for ( i = 1 ; i <= 10 ; i + + )
printf ( "%d\t", i ) ;
1 2 3 4 5 6 7 8 9 10
main( )
int num, i ;
while(1)
19
if ( num == 0 )
break ;
• The control statement that allows us to make a decision from the number
of choices is called a switch. Syntax and Eg are as follows:
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
20
do this ;
default :
do this ;
main( )
{ char c ;
Scanf(“%c”,&c);
switch ( c )
case ‘R’ :
case ‘G’ :
case ‘B’ :
default :
First, the expression following the keyword switch is evaluated.The value it gives
is then matched, one by one, against the constant values that follow the case
21
statements. When a match is found, the program executes the statements
following that case. If no match is found with any of the case statements, only
the statements following the default are executed.
The way if statements can be nested, similarly whiles and fors can also be
nested.
• eg,.
{ 1
for(j=1;j<=i;j++) 12
printf(“\n”); 1 2 3 4
} 12345
Working: in the outer for loop, set i=1 at the first time and the first statement
within this loop is another for loop. Now the inner for loop works completely
until its condition gets false. Then comes to the printf statement and then again
goes to the second execution of outer for loop. That is i=2. The above process is
repeated until the condition of outer for loop gets false .
22