0% found this document useful (0 votes)
10 views16 pages

Part 3

The document provides an overview of comments, library functions, input and output functions, and control flow in C programming. It explains the purpose of comments, lists common library functions, and describes input/output functions like getchar(), scanf(), and printf(). Additionally, it covers decision-making statements such as if-else and switch statements, as well as looping constructs like while, do-while, and for statements.

Uploaded by

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

Part 3

The document provides an overview of comments, library functions, input and output functions, and control flow in C programming. It explains the purpose of comments, lists common library functions, and describes input/output functions like getchar(), scanf(), and printf(). Additionally, it covers decision-making statements such as if-else and switch statements, as well as looping constructs like while, do-while, and for statements.

Uploaded by

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

Comments:

 Comments are non executable statements.


 They are used to increase the understand ability of a program.
 A comment may start anywhere in the line and whatever follows fill the end of the line is ignored.
 The double slash comment is considered as a single line comment.
Eg: // This is my first c program.
 Note that there is no closing symbol.
 If we want to make more than one line as comment then the comment lines should be placed within the
symbols / *. . . . . . . */
 Hence /*. . . . . . . .*/ these are considered as multi line comments.
 However remember we can’t insert a double slash style comment within the text of a program line.
Eg: for (i=1, // program counter;) – not valid

Library Functions:
 C-language provides a number of library functions that carry out various commonly used operations.
 These functions have a predefined task.
 Some of the commonly used library functions are listed below.

Function Return Type Purpose


abs(i) int return the absolute value of i.
ceil (d) double Roundup to the next integer value
cos (d) double Returns the cosine of d
cosh (d) double Return the hyperbolic cosine of ’d’.
exp (d) double Raise e to the power of d (e^d).
floor (d) double Round down to the next integer value
log (d) double Return the natural logarithm of ’d’.
pow (d1,d2) double Return of d1 raised to the power of d2{d1^d2}.
getchar() int Enter a character from standard input device.
putchar() int send a character to standard output device.
sqrt(d) double Return the square-root of ’d’.
toascii(c) int convert value of argument of ASCII
tolower(c) int convert letter to lowercase
toupper (c) int convert letter to uppercase
INPUT AND OUTPUT
Introduction
 Input & Output statements are used to read and write the data in C-program.
 Reading, processing and writing of data are three essential functions of a computer program.
 Most of the programs receive data from standard input device like keyboard, and process the data and
displays the result on the standard output device like “Monitor”.
 ‘C’ language supports a number of library functions used for input and output of data.
 All these library functions are available in a header file called stdio.h.

Input Functions
 The commonly used input functions are
o getchar().
o scanf().
o gets().
getchar ():
 Single characters can be entered in to the computer by using getchar() function.
 This function doesn’t require any arguments.
 The general format of getchar() function is as follows.
 Syntax: char-variable-name = getchar();
Eg: ch = getchar();
 Here ch is a variable name and is a variable of character data type.
scanf ();
 Input data can be entered in to the computer from a standard input device by using scan f function.
 This function can be used to enter any combination of numerical values, single characters and strings.
 The general format function of scan f is as follows.
scanf(control string, arg1, arg2, . . . . . . . . . . . . argn);
 Where control string {also called format string or format specifiers} refers to the format of data being
received.
 The control string consists of individual group of characters with one character for each input data item.
Each character for each input data item. Each character group begin with percent sign {%}.
 arg1, arg2, . . . . . . . argn specifies the list of arguments (or) variables each preceded with an address
operator (&).
 The number of format specifiers must be equivalent to number of arguments. Here, the order of the
arguments is important.
 Some of the format specifiers for various data types are listed below.
Type of value Format Specifiers
char c
int d
float f
float {Scientific Notation} e
short h
octal value o
string s
long ld
unsigned int u
double lf
hexa decimal value x

Eg: int a;
float b;
char ch;
scanf (“%f %d %c”, &b, &a,&ch);
gets ():
 gets() is an input function used to accept a string.
 It contains single argument. The argument must be a data item that represents a string.
Eg: char line [100];
gets (line);

Output functions:
 The commonly used output functions are
o putchar()
o printf()
o puts()
putchar ():
 Single characters can be displayed on the standard output device {monitor} by using the ‘c’ library function
put char.
 This function is complementary to the input function getchar(). It transmits a single character to the output
device.
 The character being transmitted will be of character type variable.
Syntax: putchar (char – variable);
Eg: char c;
putchar (c);

printf();
 Output data can be written from the computer on to a standard output device using the library function printf
().
 This function can be used to output any combination of numerical values, single characters and strings.
 The general format for print ‘f’ statement is as follows.
printf(control string arg1, arg2 . . . . . . . argn).
 Where control string refers to a string that contains formatting information and arg1, arg2 . . . . . . . . argn are
arguments that represent the individual output data items.
 It contrast, to scanf(), the arguments in a printf function do not represent memory address and therefore they
are not preceded by &.
Eg: int a;
float f;
char h;
printf (“%c %f d”, ch, f, a);
 The printf () statement can also be used to display a message
Syntax: printf (“message”);
Eg: print f ( “welcome to c programming”);

puts ();
 puts () function is used to print a strings on standard output device.
 It accepts a single argument.
 The argument must be a data item that represents a string.
 Eg: char line[80];
puts (line);
CONTROL FLOW
Decision Making Statements
Introduction
 A ‘C’ program is a set of statements which are normally executed sequentially in the order in which they
appear.
 But in practice we have a number of situations where we can change the order of execution based on
certain conditions.
 The above situation involves a kind of decision making to check whether a particular statement should be
executed are not depending on the result of the condition.
 When a program breaks the sequential flow and jumps in to another part of the code, then it is called
“branching”.
 When the branching is based on a condition, it is known as conditional branching.
 It branching takes place without any decision, it is known as unconditional branching.
 ‘C’ language supports the following statements, which are known as “control” (or) “conditional” (or)
“Decision making statements”.
 If Statement
 Switch Statement
 Conditional Operator

If Statement:
 The If statement is a powerful decision making statement.
 It is used to control the flow of execution of statements.
 It is basically a two-way decision statement

Entry

True

False

Two-way branching
 It evaluates the test expression first and checks whether the condition is true (or) false. It has two paths, one
for true condition and other for false condition.
 The If statement may be implemented in different forms.
Simple If statement:
 The general form of if statement is as follows.
If (test expression)
{
Statement – block;
}
Statement -x;
The statement block may be a single statement (or) group of statements.
 If the test expression is true, the statement block will be executed otherwise the block will be skipped and
executes statement ‘X’.

Entry

Test True Statement


Expression Block
?

False

Statement-x

Note:
 The default scope of if statement is the immediate next statement i.e., if braces are not mentioned only
the immediate next statement of if will have the effect.
If-else statement:
 The general form for if-else statement is as follows.
if (test expression)
{
true-block statement;
}
else
{
false-block statement;
}
Statement-x;
 If the text expression is true, the true block statements will be executed otherwise false block statement will
be executed.
 In either case only one block will be executed but not both.

Entry
False
Test True
Expression
?

False block True block


Statement Statement

statement-x

Nested if-else statement:


 When a series of decisions are involved, we may have to use more than one if-else statement, which is
also known as nested if-else. The general form is as follows.
 If (test condition-1)
{
If (test condition-2)
{
Statement -1;
}
else
{
Statement -2;
}
}
else
{
Statement -3;
}
Statement –x;
 If the condition 1, is false, statement-3 will be executed. Otherwise condition 2 is checked. If it is true,
statement-1 will be executed otherwise statement-2 will be executed. Finally the control transfers to the
statement –x.

Entry
Test True
Cond-1
?

Test True
Cond- 2
False
False

Statement-3 Statement-2 Statement -3

statement –x
Else-if ladder:
 This is the another way of multipath decision making in contains a chain of ifs in which the statement
associated with each else is an it. The general form is as follows.
If (condition -1)
Statement-1;
else if (condition -2)
Statement-2;
else if (condition-3)
Statement -3;
else
default – statement;
Statement –x;
 The above construct is known as else if ladder. The conditions are evaluated from top, to bottom. If condition
is true, the statement associated with it is executed and the control transfer to statement-x. When all conditions
becomes false, default statement will be executed.

True Cond-1 False


?

Statement-1 True Cond-2 False

Statement-2
True cond-3 False

Statement-3 Default
Statement

Statement-x
Switch Statement
Introduction
 The draw backs of “nested if else” are:
o Care needs to be exercised to place else to the corresponding if and
o Care needs to be exercised to place closing brace to the corresponding opening brace as the
number of conditions increases.
 From the above, we can conclude that as the number of conditions increases, the complexity of the program
also increases. If statement can’t handle this type of situation perfectly.
 To overcome the above problem, ‘C’ provides another built-in multi way decision making statement known
as “switch”.
 The switch statement tests the value of the given variable (or) expression against a list of case values. When a
match is found, a block of statements associated with that particular case is executed.
 The general form of Switch statement is as follows.
switch (expression)
{
case value -1: block -1;
break;
case value -2: block -2;
break;
: :
: :

case value –n: block-n;


break;
default: default- block;
}
Statement-x;
 In above syntax the expression is an integer expression (or) character.
 value-1, value-2, . . . . . . . value-n are constants {int (or) char} known as “case labels”, each of these values
should be “unique”.
 Block-1, block-2, . . . . .block-n are statement lists and may contain zero (or) more statements.
 There is no need to have to put braces around the block of statements but colon is mandatory after the case
label.
 In case of switch, the value of expression is compared against the values value-1, value-2,…value-n.
 If a case is found the corresponding block of statements will be executed.
 When a match is found, the switch statement executes the matched case as well as all the cases below it. In
order to overcome this problem break statement is used.
 The break statement causes an exit from the switch statement and transfers the control to the next statement
following the switch.
 The default is an optional case. If present, it should be the last case of the switch. The default block
statements will be executed when the expression doesn’t match with any case value.
 The selection process of switch statement is as follows.
Decision Making and Looping Statements
Introduction
 There may be a situation where we should perform same task repeatedly for a number of times.
 The process of repeatedly executing a group of statements is known as looping.
 In case of looping a sequence of statements are executed until the condition for termination of the loop is
satisfied.
 A program loop consists of two segments namely “body of the loop” and “control statement”.
 The control statement tests the condition and then executes the statements contained in the body of the loop.
 Depending on the position of the control statement in the loop, a control structure may be classified as Entry
controlled loop (or) Exit controlled loop.
 In the Entry controlled loop the condition is tested before the start of execution. If the condition is false the
body of the loop is not executed.
 In the case of Exit controlled loop the test is performed at the end of the body of the loop.
 Therefore, the body of the statements executed unconditionally for the first time.

 A Looping process in general includes the following steps.


o Initialization of the counter.
o Execution of the statements in the loop.
o Test for a specified condition.
o Increment/decrement the counter.
 ‘C’ language provides three constructs for performing looping operations. They are
o while statements
o do.. while statement.
o for statement.

while Statement:
 While is an entry-controlled loop statement.
 It is the simplest of all looping structures.
 The basic format of while statement is as follows.
Initialization;
while (test condition)
{
body of the loop;
}
 After initialization of the control variable, 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 it is true, the body of the loop is
executed once again.
 The above process repeats until the test condition fails and the control is transferred to out of the loop.

do –while Statement:
 It is an exit controlled looping statement the basic format of do…. While statement is as follows.
Initialization;
do
{
body of the loop;
}while(condition);
 The process of execution of do-while statement is similar to that of while statement the difference between
while & do-while is the place where the test condition occurs.
 In case of do-while statement, the program proceeds to evaluate the body of the loop first. At the end of the
loop, the test condition is evaluated.
 If the condition is true, the body of the loop is once again executed. The above process repeats until the test
condition fails and then control goes out of the do-while constructs.
for Statement:
 while and do-while are indefinite looping statements i.e., these statements are used when the numbers of
iterations are not known in advance where as for is a definite loop i.e., for statement is used when the number
of iterations are known is advance.
 It is another entry controlled loop that provides a more concise looping structure.
for (initialization; test condition; increment / decrement)
{
body of the loop.
}
The execution of for statement is as follows.
 Initialization of counter variable is done first.
 The value of counter variable is tested using test condition. If the condition is true the body of the
loop is executed otherwise the loop is terminated.
 When the body of the loop is executed, the control is transferred back to the for statement after
evaluating the last statement.
 Now, the counter variable is increment/ decremented and then the new value is again tested using test
condition. If the condition is true the above process continues otherwise the loop control goes out of the
structure.
Break Statement
 The break statement is frequently used to terminate the processing of a particular case within a switch
statement. Lack of an enclosing iterative or switch statement generates an error.
 Within nested statements, the break statement terminates only the do, for, switch, or while statement that
immediately encloses it.
 We can use a return or goto statement to transfer control elsewhere out of the nested structure.
 The below example illustrates the break statement:
#include <stdio.h>
void main()
{
char c;
for(;;)
{
printf("\nPress any key, Q to quit: " );
// Convert to character value
scanf_s("%c", &c);
if (c == 'Q')
break;
}
} // Loop exits only when 'Q' is pressed
Continue Statement
 Continue statement is a jump statement.
 The continue statement can be used only inside for loop, while loop and do-while loop.
 Execution of these statement does not cause an exit from the loop but it suspend the execution of the
loop for that iteration and transfer control back to the loop for the next iteration.
 For Example , a continue statement in a for statement causes the first expression of the for statement to
be evaluated. Then the compiler reevaluates the conditional expression and, depending on the result,
either terminates or iterates the statement body.
 The below example illustrates the continue statement:
#include <stdio.h>
void main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
continue;
printf(“%d”,i);
}
getch();
}
Goto and labels:
 ‘C’ language supports the goto statement to branch unconditionally from one part of the program to another
part.
 It is in general used to alter the normal sequence of a program execution.
 The general format of goto statement is as follows.
goto label ;
Where label is an identifier that is used to label the target statements to which the control will be
transferred.
 Control may be transferred to any other statement with in the program i.e., the label can be either before
(or) after the goto statement.
 The target statement must be labeled and the label must be followed by a ‘colon’ as follows.
Label : target statement ;

label-1: statement; goto label-2;


---------------
---------------
goto label-1; label-2: statement;

Backward goto Forward goto

You might also like