Module_2____2
Module_2____2
23CPI25A
MODULE 2
LOCHAN GOWDA M
Assistant Professor
Dept of CSE
FILES USED IN A C PROGRAM
Source Code Files:
The source code file contains the source code of the program. The file extension of any C source code file is .c'. This file contains
C source code that defines the main function and maybe other functions.
Header Files:
Another option is to make subroutines and store them in a different file known as header file
we see that using a header file produces the same results as copying the header file into each source file that needs it. Also when a
header file is included, the related declarations appear in only one place. If in future we need to modify the subroutines, we just
need to make the changes in one place, and programs that include the header file will automatically use the new version when
recompiled later
string.h: for string handling functions
Stdlib.h : for some miscellaneous functions
stdio.h : for standardized input and output functions
math.h : for mathematical functions
alloc.h: for dynamic memory allocation
Object Files
Object files are generated by the compiler as a result of processing the source code file. Object files contain compact binary
code of the function definitions. Linker uses these object . files to produce an executable file (.exe file) by combining the object
files together. Object files have a '.o' extension, although some operating systems including Windows and MS-DOS have a ' .obj'
extension for the object file.
if Statement
The if statement is the simplest form of decision control statements that is frequently used in decision-making.
The general form of a simple if statement is shown in Figure 10.2.
condition and statement should be put together as a single statement.
Write a program to determine the character entered by the user.
#include <stdio.h›
#include < ctype.h>
if-else Statement
• Write a program to enter any character. If the entered character is in lower case
then convert it into upper case and if it is a lower case character then convert it int
upper case
#include <stdio.h>
Void main ( )
char ch;
printf("\n The entered character is in uppe case. In lower case it is: %c", (ch+32));
else
printf("\n The entered character is in lowe case. In upper case it is: %c", (ch-32));
Output
#include <Stdio.h>
#include < math.h>
Void Main()
{
float amount, units;
char name [20];
Else
}
Simulation of a Simple Calculator.
#indude<stdio.h>
#include < math. h>
Case ‘/':
#include< Stdlib.h> If(n2 !=0)
void main() {
{ printf(" %lf / %lf = %lf \n”, n1, n2,
double n1, n2; n1/n2 );
char op; }
Printf(“ Enter the Operatan (+, -, *,/.,%) :\n"); else
Scanf(" %c", &op); {
print (" Enter the number: In"); printf(" Division is not possible");
scanf(' %lf%lf " &n1, &n2 ); break;
Switch(op) }
{ Default :
case ‘+’ :
Printf(“ Invalid Input\n);
printf(" %lf + %lf = %lf \n”, n1, n2, n1 +n2 );
}
break;
}
case ' -':
printf(" %lf - %lf = %lf \n”, n1, n2, n1 - n2 );
break;
case '*':
printf(" %lf * %lf = %lf \n”, n1, n2, n1 * n2 );
Sentinel Loops
•Based on the nature of control variable and the kind of value assigned to it for testing the
control expression, the loops may be classified into following two general categories:
1. Counter-controlled loops
2. Sentinel-controlled loops
• When we know in advance exactly how many times the loop will be executed, we use a
counter-controlled loop. We use a control variable known as counter. The counter must be
initialized, tested and updated properly for the desired loop operations. The number of times we want
to execute the loop may be a constant or a variable that is assigned a value. A counter-controlled
loop is sometimes called definite repetition loop.
• In a sentinel-controlled loop, a special value called a sentinel value is used to change the loop
control expression from true to false.
• For example, when reading data we may indicate the "end of data" by a special value, like -1 and 999.
• The control variable is called sentinel variable. A sentinel-controlled loop is often called
indefinite repetition loop because the number of repetitions is not known before the loop
begins executing.
THE WHILE STATEMENT
On reaching the do statement, the program proceeds to evaluate the body of the loop
first. At the end of the loop, the test-condition in the while statement is evaluated. If the
condition is true, the program continues to evaluate the body of the loop once again. This process
continues as long as the condition is true. When the condition becomes false, the loop will be
terminated and the control goes to the statement that appears immediately after the while
statement.
Since the test-condition is evaluated at the bottom of the loop, the do...while construct provides
an exit-controlled loop and therefore the body of the loop is always executed at least
once.
THE FOR STATEMENT
The for loop is another entry-controlled loop that provides a more concise loop control structure. Thegeneral
form of the for loop is
#include‹stdio.h>
int main()
{
Int n, i = 1, sum = 0;
float avg = 0.0;
printf("\n Enter the value of n: ");
scanf ("%d", &n) ;
Do
{
sum = sum + i;
i = i + 1;
}while(i<=n);
avg = (float) sum/n;
printf("\n The sum of first %d numbers = %d",n, sum);
printf("\n The average of first %d numbers =%. 2f", n, avg);
return 0;
}
}
}
Break and continue statements
• Break statement :
• ❑ used to terminate the execution of the nearest enclosing loop in which it
appears.
• ❑ the control passes to the statement that follows the loop in which the break
statement appears. Its syntax is quite simple, just type keyword break followed
with a semi-colon.
break;
• ❑ In switch statement if the break statement is missing then every case from the
matched case label to the end of the switch, including the default, is executed.
• continue statement:
• ❑ When the compiler encounters a continue statement then the rest of the statements in the loop are
skipped and the control is unconditionally transferred to the loop-continuation portion of the
nearest enclosing loop.
• ❑ Its syntax is quite simple, just type keyword continue followed with a semi-colon.
continue;
• ❑ If placed within a for loop, the continue statement causes a branch to the code that updates the loop
variable. For example,
• int i;
• Label can be any valid variable name that is followed by a colon (:)