Chapter No 6 To 8 XII
Chapter No 6 To 8 XII
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
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.
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.
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.
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.
Example
char name[15];
gets(name);
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.
Example
char x=’A’;
putchar(x);
putchar(‘Z’);
puts()
Example
char name[]={“Bahria College”};
gets(name);
scanf() Function
The scanf() function stands for “scan formatted”. This function is a generalized function which is used
to input all types of data.
A control string enclosed in double quotes followed by a comma that contains the format specifiers
indicating how the data input will be interpreted.
Example
int x;
scanf(“%d”,&x);
(& is called address operator that invokes the address of variable)
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.
A list of variable.
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)
Other than standard data format specifiers these format specifiers can also be used with printf()
function.
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.
%.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)
Syntax:
if (condition)
Statement;
[else
Statement;]
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.
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);]
}
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)
{
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()
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:
(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)
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
Syntax
while( expression )
statement
or { block of statements}
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.
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.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int x=1;
while(x<=10)
{
printf(“%d\n”,x);
x++;
}
printf(“End of 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}
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.
#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()
Syntax:
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.
#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()
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.
#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()