0% found this document useful (0 votes)
19 views15 pages

Chapter No 6 To 8 XII

Chapter 6 discusses input and output functions in C, categorizing them into formatted and unformatted functions, with examples of each. It also covers selection control structures like 'if' and 'switch' statements, explaining their syntax and usage. Additionally, it introduces iteration control structures, detailing 'while', 'for', and 'do while' loops, along with the 'break' and 'continue' statements.

Uploaded by

yshahid752
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)
19 views15 pages

Chapter No 6 To 8 XII

Chapter 6 discusses input and output functions in C, categorizing them into formatted and unformatted functions, with examples of each. It also covers selection control structures like 'if' and 'switch' statements, explaining their syntax and usage. Additionally, it introduces iteration control structures, detailing 'while', 'for', and 'do while' loops, along with the 'break' and 'continue' statements.

Uploaded by

yshahid752
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/ 15

Chapter No.

INPUT AND OUTPUT FUNCTIONS


Input and output functions are used to provide communication between the user and the program.

Input and Output functions are classified into two categories:

1. Formatted IO Functions:
i. scanf(): Input function for all data types
ii. printf(): Output function for all data types
2. Unformatted IO Functions:
i. getch(): Input function for [char] data type
ii. getche(): Input function for [char] data type
iii. getchar(): Input function for [char] data type
iv. putch(): Output function for [char] data type
v. putchar(): Output function for [char] data type
vi. gets(): Input function for [string] data
vii. puts(): Output function for [string] data

UNFORMATTED INPUT FUNCTIONS

getchar() Function

The function getchar() stands for “get character” and inputs a single character from the keyboard.
When called, the function waits for a key to be pressed. User can input the character as either a char
or int types because getchar() treat characters as integers and accepts the ASCII value of character.

Syntax: char var=getchar();

Example:
int x;
x=getchar();

char ch;
ch=getchar();

getch()

getch() stands for “get character”. This function is used to take a single character without waiting for
the confirmation via ENTER KEY. The character that provided by the user is not appeared on screen,
means input character without echo.

Syntax: char var=getch();

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 1
Example
char ch;
ch=getch();

getche()

getche() stands for “get character with echo”. This function is used to take a single character without
waiting for the confirmation via ENTER KEY. The character that provided by the user is appeared on
screen, means input character with echo.

Syntax: char var=getche();

Example
char ch;
ch=getche();

gets()

The function gets() stands for “get string”. It reads the string from the keyboard, adds the null character
‘\0’ to it, and assigns it o be required variable which comes as an argument of the function.

Syntax: gets(character array);

Example
char name[15];
gets(name);

UNFORMATTED OUTPUT FUNCTIONS

putchar() and putch()

The functions putchar() and putch() stands for “put character” and displays only a single char value on
the screen. The data can be provided as char variable or as char constant. The working of both
functions is identical.

Syntax: putchar(char var or char constant);

Example
char x=’A’;
putchar(x);
putchar(‘Z’);

puts()

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 2
The function puts() stands for “put string” and displays a string on monitor. String is provided as
argument.

Syntax: puts(char array or string constant);

Example
char name[]={“Bahria College”};
gets(name);

FORMATTED INPUT FUNCTION

scanf() Function

The scanf() function stands for “scan formatted”. This function is a generalized function which is used
to input all types of data.

scanf() function consists of two parts:

A control string enclosed in double quotes followed by a comma that contains the format specifiers
indicating how the data input will be interpreted.

A list of variable addresses.

Syntax: scanf(“Control String”, Address of variable);

Example
int x;
scanf(“%d”,&x);
(& is called address operator that invokes the address of variable)

FORMATTED OUTPUT FUNCTION

printf()

The function printf() stands for “print formatted”. This function is a generalized output function that can
be used to output any combination of numerical values, single characters and strings. In addition
printf() can format how the data appears.

Function printf() can perform the following formatting capabilities:

 Rounding floating value.


 Right and left justification out outputs.
 Displaying all types of data with fixed-size field widths and precisions.

printf() function consists of two parts:

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 3
A control string enclosed in double quotes followed by a comma that contains the format specifiers
indicating how the data output will be interpreted.

A list of variable.

Syntax: printf(“control string”[, variable list]);

Example:
int age=17;
printf(“The age of Salman is %d:”,age);
Output: The age of Salman is 17 (%d will be replaced by the variable age)

Format Specifiers used with prinf()

Other than standard data format specifiers these format specifiers can also be used with printf()
function.

%u Displays an Unsigned integer


%e Displays number is scientific notation (float)
%o Displays an octal number (Unsigned)
%x Displays a hexadecimal number (Unsigned)

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 4
PRECISION SPECIFIERS

Function printf() provides the ability to specify the precision with which data is printed. The default
conversion for %f is rounded to six decimal places. Programmer can vary this precision using a
decimal point and a precision specifier.

The format for precision specification is:

%.nf

Example:

float a=45.75;

printf(“%.2f”,a);
Output will be 45.75

printf(“%9.2”,a);
Output will be _ _ _ _ 45.75 (9-5=4 spaces in the beginning)

printf(“%-9.2”,a);
Output will be 45.75 _ _ _ _ (9-5=4 spaces in the end)

printf(“%9.0”,a);
Output will be _ _ _ _ _ _ _ 45 (9-2=7 spaces in the beginning)

printf(“%#9.0”,a);
Output will be _ _ _ _ _ _ _ 45. (9-2=7 spaces in the beginning, # will put a
decimal point in the end)

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 5
Chapter No. 7
Selection Control Structure
The if Selection Structure

The if statement is used in taking decision in C program.

Syntax:
if (condition)
Statement;
[else
Statement;]

How if statement works?

The if statement tests a particular condition. Whenever that condition evaluates as true, an action is
performed but if it is not true, then the action is skipped and control transfers the next step of the
program.

The else statement is optionally used with if structure. This statement is used to execute statement
when the condition evaluates as false.

Use of Curly brackets

The use of curly bracket is optional in the statement followed by if or else but incase of multiple
statement which have to be executed on certain criteria must be enclosed in curly bracket.

Example:

#include <stdio.h>
#include <conio.h>

void main(void)
{
int a;
scanf(“%d”,&a);
if (a%2==0)
{
printf(“Number is Even”);
} // end of if
else
{
printf(“Number is Odd”);
}// end of else
}// end of main()
Syed Werasat Hussain Hashmi
Sir Adamjee Institute Page 6
The switch structure

The switch statement is used to select the one of the several alternatives.

Syntax:
switch(int or char variable)
{
case constant 1:
statement(s);
break;
case constant 2:
statement(s);
break;
case constant 3:
statement(s);
break;
.
.
.
[default:
statement(s);]
}

How switch statement works?

The switch statement is especially useful the selection is based on the value of a single variable which
is called controlling variable or a simple expression called controlling expression. The type of variable
may be int or char but it may not be a float type.

The switch can only test for equality. A variable is tested against a list of integer or character constant.
When the value matches a case condition, the statement following the case is executed. The break
statement at the end of each case sends control to the end of the switch.

The default statement is option. The default statement(s) is performed if no matches are found.

Example:

#include <stdio.h>
#include <conio.h>

void main(void)
{

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 7
int code;
scanf(“%d”,&code);

switch(code)
{
case 200:
printf(“Welcome Mr Salman”);
break;
case 300:
printf(“Welcome Mr Aslam”);
break;
case 400:
printf(“Welcome Mr Navaid”);
break;
default:
printf(“Wrong Agent Code”);
break;
} // end of switch()
}// end of main()

Difference between switch and if

if statement Switch statement


1. if statement can evaluate any type 1. switch statement can only
of conditions. It may check equality, evaluates the equality check.
greater than, less than.
2. There is not restriction of data type 2. Only int and char data type variable
in conditional expression of if. can be used in switch statement for
evaluation.
3. In case of multiple condition the if 3. switch is more simpler as compare
structure has to be nested and it to if in case of nested conditions.
become too complex.
4. Example of if() 4. Example of switch()

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 8
Use of Conditional Operator

The conditional operators ? and : are sometimes called ternary operators since they take three
arguments. Conditional operator is used as foreshortened if-then-else. The general form is:

Expression 1 ? expression 2: expression 3;

Example: int hrent=(basic>5000) ? basic*65/100 : basic*50/100;

(If value of basic variable is greater than 5000 then 65% of basic variable’s value will be stored
in hrent variable otherwise 50% will be stored)

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 9
Chapter No. 8
Iteration Control Structure
What is Loop?

Loop is a programming statement used to execute statement(s) repeatedly. There are three types of
loops in C language:

1. while loop
2. do while loop
3. for loop

while Loop

A while loop executes the statement(s) as long as that expression is true.

Syntax
while( expression )
statement
or { block of statements}

Loops consist of two parts:

 One or more control expressions which control the execution of the loop.

 The body, which is the statement or set of statements which is executed over and over.

How while works?

A while loop starts out like an if statement: if the condition expressed by the expression is true, the
statement is executed. However, after executing the statement, the condition is tested again, and if it's
still true, the statement is executed again.

Example: Counting from 1 to 10

#include <stdio.h>
#include <conio.h>
void main(void)
{
int x=1;
while(x<=10)
{
printf(“%d\n”,x);
x++;
}
printf(“End of Loop”);

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 10
}// end of main()
The for loop

The for loop is used to repeat a statement or a block of statement for a specified number of times.

Syntax
for ([expression1];[expression 2]; [expression 3])
statement;
or {block of statements}

How for works?

The for loop statement includes the initialization of the counter, the condition, and the increment. The
three expression within the for loop must be separated by the semicolon.

The first expression initializes the starting value of variable that is used in counter expression. It is
optional and if it is ignored the existing value of variable will be considered as starting value of counter
variable.

The Second expression is used to set the condition for loop termination and iteration. If condition is
ignored the loop will become endless.

The third expression is used for counter expression. If the counter expression is ignored than it must
be placed within the body of the loop otherwise loop will become endless.

Example: Counting from 1 to 10

#include <stdio.h>
#include <conio.h>

void main(void)
{
int x;
for(x=1;x<=10;x++)
{
printf(“%d\n”,x);
}
printf(“End of Loop”);
}// end of main()

The do while loop

The do while loop is almost identical to while loop.

Syntax:

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 11
do
statement;
or {
Block of statements;
}
while(condition);

how do while works?

The only difference between the do while and while is that test condition is at the bottom of the loop.
This means that the program always executes the statement(s) at least once, even the condition fails
for the first time itself. Then depending on the value of test expression.

Example: Counting from 1 to 10

#include <stdio.h>
#include <conio.h>
void main(void)
{
int x=1;
do{
printf(“%d\n”,x);
x++;
} while(x<=10);

printf(“End of Loop”);
}// end of main()

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 12
The break and continue statements

break statement

The break statement is used to terminate the iteration of loop and exit from switch case block.

Example:

#include <stdio.h>
#include <conio.h>
void main(void)
{
int x;
for(x=1; ;x++)
{
printf(“%d\n”,x);
if (x==10)
break;
}
printf(“End of Loop”);
}// end of main()

continue statement

The continue statement is just opposite of the break statement. It takes the control to the beginning of
the loop, bypassing the statements inside the loop which have not yet been executed.

Example: Calculating some of all even numbers between 1 to 10

#include <stdio.h>
#include <conio.h>
void main(void)
{
int x,sum=0;
for(x=1; x<=10 ;x++)
{
if (x%2!=0)
continue; // skips rest part of loop
printf(“%d\n”,x);
sum+=x;
}
printf(“Sum of all even numbers between 1-10=%d”,sum);
}// end of main()

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 13
Difference between for and while loop

while loop for loop


1. A while loop executes the 1. The for loop is used to repeat a
statement(s) as long as that statement or a block of statement
expression is true for a specified number of times.
2. while loop would be the better 2. The for loop is suitable when
choice of the programmer if the the number of iteration is finite.
numbers if iteration is not known.
3. In while loop the control 3. In for loop there is nothing
expression is compulsory to be compulsory. It has three
mentioned otherwise a syntax expressions: initialization,
error will be generated. condition, counter but all are
optional.
4. Example of while loop 4 Example of for loop.

while loop do while loop


1. The while loop first evaluate the 1. The do while loop first executes
condition and then executes the the block and in the end evaluate
block. the condition.
2. If the condition is evaluated as 2. Regardless the given condition
false on the very first attempt, the is false or true, the do while loop
block will not be executed even for executes the block at least for one
a single time. time.
3. Example of while loop 3. Example of do while loop

Syed Werasat Hussain Hashmi


Sir Adamjee Institute Page 14
Syed Werasat Hussain Hashmi
Sir Adamjee Institute Page 15

You might also like