0% found this document useful (0 votes)
9 views

Module_2____2

The document discusses various components of C programming, including source code files, header files, object files, and binary executable files. It also covers basic data types, conditional branching statements, loops, and control statements such as break and continue. Additionally, it provides examples of programs demonstrating these concepts, including character conversion, electricity billing, and a simple calculator.

Uploaded by

hemanthsjbitcse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module_2____2

The document discusses various components of C programming, including source code files, header files, object files, and binary executable files. It also covers basic data types, conditional branching statements, loops, and control statements such as break and continue. Additionally, it provides examples of programs demonstrating these concepts, including character conversion, electricity billing, and a simple calculator.

Uploaded by

hemanthsjbitcse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Problem Solving Using C

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.

Binary Executable Files


The binary executable file is generated by the linker. The linker links the various object files to produce a binary file that can be
directly executed. On Windows operating system, the executable files have a ' exe' extension.
BASIC DATA TYPES IN C
CONDITIONAL BRANCHING STATEMENTS
The conditional branching statements help to jump from one part of the program to another depending on whether a particular condition is
satisfied or not. These decision control statements include:
◦ if statement
◦ if-else statement
◦ if-else-if statement
◦ Nested if
◦ switch statement

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 Enter any character: ");

scanf ("%c", &ch);

if(ch >='A' && ch<='Z')

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

Enter any character: a

The entered character is in lower case. In upper case it is: A


Nested if
If else if
• An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paisa per unit: for the
next 100 units 90 paisa per unit: beyond 300 units Rs 1 per unit. All users are charged a minimum of Rs.100 as meter
charge. If the total amount is more than Rs 400, then an additional surcharge of 15% of total amount is charged. Write
a program to read the name of the user, number of units consumed and print out the charges.

#include <Stdio.h>
#include < math.h>
Void Main()
{
float amount, units;
char name [20];

Printf (" Enter the Units and Name : \n");


Scanf(“%f%s", &units, name );
if (units >0 && units <= 200)
{
amount = Units * 0•8 + 100;
}

else if(units > 200 && Units < = 300)


{
amounts = (200*0.8) + ((Units- 200) *0.9) + 100;
}

Else

amount = (200*0.8) + (100*0.9) + (Units -300)*1) + 100;

if(amount > 400)


{
amount = amount * 0•15 ;
}
Printf(" Name: %s \t and Units: %f \t amount: %f \n",name, Units, amount);

}
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

The while is an entry-controlled loop statement.


The test-condition is evaluated and if the condition is true, then the
body of the loop is executed. After execution of the body, the test-condition
is once again evaluated and if it is true, the body is executed once again.
This process of repeated execution of the body continues until the test-
condition finally becomes false and the control is transferred out of
the loop
On exit, the program continues with the statement immediately
after the body of the loop.
The Do 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

The execution of the for statement is as follows:


1. Initialization of the control variables is done first, using assignment statements such as i = 1 andcount = 0. The
variables i and count are known as loop-control variables
2. The value of the control variable is tested using the test-condition. The test-condition is a relational expression,
such as i < 10 that determines when the loop will exit. If the condition is true, the body of the loop is executed;
otherwise the loop is terminated and the execution continues with the statement that immediately follows the
loop.
3. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the
last statement in the loop. Now, the control variable is incremented using an assignment statement such as i =
i+1 and the new value of the control variable is again tested to see whether it satisfies the loop condition. If the
condition is satisfied, the body of the loop is again executed. This process continues till the value of the control
variable fails to satisfy the test-condition.
Write a program to calculate the average of first n numbers.

#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:

• ❑ can only appear in the body of a loop.

• ❑ 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;

for (i = 0; i < 10; i++) {


if (i == 4) {
continue;
}
printf("%d\n", i);
} //This example skips the value of 4:
• goto statement:

• transfer control to a specified label.

• Label can be any valid variable name that is followed by a colon (:)

• Note that label can be placed anywhere in the program either


before or after the goto statement.

• ❑ Whenever the goto statement is encountered the control is


immediately transferred to the statements following the label.

• ❑ If the label is placed after the goto statement then it is called a


forward jump and in case it is located before the goto statement, it is
said to be a backward jump.

You might also like