Professional Documents
Culture Documents
KEY POINTS
INPUT/OUTPUT FUNCTION/STATEMENT
(i) Input and output are usually an important part of any program. To be more interactive a
program needs to be able to accept data and display result
(ii) In C the input and output operations are performed by two standard functions these are
printf() and scanf(). These functions can be accessed by including the standard library
stdio.h in the program.
(iii) The printf function is used to print the output of the program in specified format.
PRINTF FUNCTION
The printf function is used to print the output of the program in specified format. It takes
arguments a format string and an optional list of variables. The values of variables are
displayed according to format specifier in the format string. The printf function can be
access in a program by including stdio.h header file.
FORMAT SPECIFIER
Format specifier specify the format in which the value of a variable should be display on the
screen. Format specifiers are specified in the format string and always start with % symbol.
For example:
Format specifier of int data type is %d and float data type is %f
Field-width specifier
The number of columns used to display a value on screen is referred to as field width.
Afield width specifierdescribe the number of columns that should be used to print a value.
Formatting integers
To specify the field width of an integer value just add a number between % and d of the
%d format specifier in the printf format string. This number specifies the field width.
For example
int area = 45;
printf("Area = %4d ", area);
Output
Area=45
Formatting floating point numbers
The format specification of floating point number we must specify both the total field
width and the number of decimal places.
The general form for the format specifier for a floating point value will be %m.nf where
m represents the total field width and n represents the desired number of decimal places.
ESCAPE SEQUENCE
The escape sequences are special characters used in format string of printf function in
combination with backslash (\). The backslash is called escape character, it causes an
escape from he normal interpretation of a string so that the next character is recognized as
having a special meaning.
For example escape sequence \n mean newline and \t meantab.
KIPS OBJECTIVE TYPE SERIES 202
Chapter–10 Input / Output Function
SCANF FUNCTION
Thescanf function takes as arguments a format string and a list of variables to hold the
input values.
The syntax of scanf function is
scanf(format string, &var1, &var2, &var3,.....);
Character input
In C, there are many functions to accept character input. The versatile scanf() can also be
used for this purpose. But scanf() requires pressing the return key at the end of input
value. In some cases, it is desirable to input characters without pressing the return key.
Getch function
The getch () function is very handy in character manipulation. In constract to the getch
function which does not echo the character typed. This function do not accept any
argument.
Getch () function take single character as input.
Getch () function do not echoes the character to the screen.
#include <stdio.h>
#include <conio.h>
void main()
{
int height, width, area;
clrscr();
height = 5;
width = 6;
area = height * width;
printf(" Area of square is = %d ", area);
getch();
}
Getche functions
Getche () function prompts the user to press a character and that character is printed on
the screen.
Getche () function take single character as input.
getche() function echoes the character to the screen.
It can be remembered by the use of character ‘e’ at the end of getche() function.
Getche () takes no arguments and require conio.h header file.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<“Want to continue (Press :(y/n)) : ”;
cin>>ch;
return 0;
}
ANSWER KEY
(Topical Multiple Choice Questions)
1 b 11 d 21 d
2 d 12 b 22 a
3 b 13 d
4 d 14 d
5 b 15 d
6 c 16 b
7 b 17 c
8 a 18 c
9 c 19 d
10 a 20 a
(From Textbook Exercise)
1 d 6 c
2 b 7 b
3 c 8 b
4 b 9 b
5 b 10 c
(KIPS Exercise)
1 c 6 d
2 b 7 a
3 d 8 d
4 b 9 c
5 c 10 d
(From Past Papers 2011-2015)
(Lahore + Gujranwala Board)
1 b 3 a 5 c 7 a 9 a 11 d 13 c
2 b 4 b 6 c 8 c 10 a 12 d
(From Past Papers 2011-2015)
(Faisalabad + Sargodha + Rawalpindi Boards)
1 a 3 b 5 b 7 a
2 a 4 a 6 d 8 d
(From Past Papers 2011-2015)
(D.G Khan + Bahawalpur + Multan + Sahiwal Board)
1 b 6 a 11 c
2 d 7 b 12 a
3 b 8 c 13 c
4 a 9 c 14 c
5 a 10 c 15 c
SHORT QUESTIONS
(From Text Book Exercise)
Q.1 What is printf() function.
Ans: printf
The printf function is used to print the output of the program in specified format. It takes
arguments a format string and an optional list of variables. The values of variables are
displayed according to format specifier in the format string. The printf function can be
access in a program by including stdio.h header file.
Q.2 What is scanf() function.
Ans: scanf() function
scanf function accept user input from the keyboard.The input is stored into a variable in a
specified format. The scanf function is versatile as it is equally good for numeric as well
as string input.The scanf function is defined in stdio.h header file.
Q.3 What is format specifier?
Ans: Format specifier
Format specifier specify the format in which the value of a variable should be display on the
screen. Format specifiers are specified in the format string and always start with % symbol.
For example:
Format specifier of int data type is %d and float data type is %f
Q.4 List four format specifiers used in C language?
Ans:
Symbol Data type
%d int, short
%f float
%lf double
%e float, double (Exponential Notation)
Q.5 What is meant by field width specifier.
Ans:
Field width specifier
The number of columns used to display a value on screen is referred to as field width.
Afield width specifierdescribe the number of columns that should be used to print a
value.
Q.6 How can you specify the field specifier of integer.
Ans: To specify the field width of an integer value just add a number between % and d of the
%d format specifier in the printf format string. This number specifies the field width.
For example
int area = 45;
printf("Area = %4d ", area);
Output:
Area=45
Q.7 How can you specify the field width for floating point number:
Ans: The format specification of floating point number we must specify both the total field
width and the number of decimal places.
The general form for the format specifier for a floating point value will be %m.nfwhere
m represents the total field width and n represents the desired number of decimal places.
void main()
{
int m, n;
printf( " Enter two numbers(separated by comma) : ");
scanf( "%d %d", &m, &n);
m = m +10;
n = 5 * m;
printf(" m = %d \t \t \t n = %d \n " , m, n);
}
Output: m = 20 n = 100
KIPS OBJECTIVE TYPE SERIES 229
Chapter–10 Input / Output Function
Q.13 Write the program in example 5 using scanf function.
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
charch;
printf("Please type a character : ");
scanf(" %c ", &ch);
printf("\n The ASCII code for \'%c\' is %d ", ch, ch);
}
Q.14 Show how the value -17.246 would be printed using the formats %8.4f, %.8.3f,
%8.2f, %8.1f, %8.0f, and %0.2f.
Ans: Format Output
%8.4f -17.2460
%.8.3f □-17.246
%8.2f □□-17.25
%8.1f □□□-17.2
%8.0f □□□□□-17.
%0.2f -17.24
Q.15 Assuming x (type double) is 21.335and y (type int) is 200, show the output of the
following statements (on paper). For clarity, use the symbol □ to denote a blank space.
Ans: printf(" x is %6.2f \t y is %4d \n", x, y);
printf(" y is %d \n", y);
printf(" x is %.1f \n", x);
Output
x is □21.34 y is □□200
y is 200
x is 21.3
Q.16 If a variables a, b and c are 307, 408.558 and -12.31, respectively, write a statement
that will display the following line: (for clarity, he symbol □ shows a blank space)
□□307□□□□□408.56□□□□-12.3
Ans: printf("%5d %11.2f %9.1f", a, b, c);
Q.17 Write a program that asks the user to enter the radius of a circle and then computes
and display the circle's area. usehe formula.
Ans: area = PI x radius x radius
where PI is constant value of 3.14159. (Not: Define a constant macro PI with #define
directive)
#include<stdio.h>
#include<conio.h>
#define PI 3.141
void main()
{
float radius, area;
printf("Enter the radius of a circle : ");
scanf(" %f ", &radius);
area = PI * radius * radius;
printf("Area of circle is = %f ", area);
getch();
}
KIPS OBJECTIVE TYPE SERIES 230
Chapter–10 Input / Output Function
Q.18 Write a program that store he values 'A', 'U', 3.456E10 and 50 in separate memory
cells. your program should be the first three values as input data, but use an
assignment statement to store the last value.
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
char ch1, ch2;
float f1;
int n1;
printf("Enter first value : ");
ch1 = getche();
printf("\nEnter second value : ");
ch2 = getche();
printf("\nEnter third value : ");
scanf(" %f ", &f1);
n1 = 50;
getch();
}
Q.19 Write a program that converts a temperature in degrees Fahrenheit to degrees
Celsius. For conversion, use the following formula
celsius = 5/9(Fahrenheit - 32)
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
float f, c;
printf("Enter temperature in Fahrenheit : ");
scanf(" %f ", &f);
c = 5.0/9.0 * ( f - 32 );
printf("Temperature in Celsius = %f ", c );
getch();
}
Output:
Enter temperature in Fahrenheit :50.36
Temperature in Celsius = 10.20
Q.20 Write program that takes a positive number with a fractional part and rounds it to
two decimal places. For example, 25.4851 would round to 25.49 and 62.4431 would
round to 32.44.
Ans: #include<stdio.h>
#include<conio.h>
void main()
{
float f1;
printf("Enter a positive value :");
scanf(" %f ", &f1);
printf("Rounded value in two decimal places is = %.2f ", f1);
getch();
}
KIPS OBJECTIVE TYPE SERIES 231
Chapter–10 Input / Output Function
SHORT QUESTIONS
(From Past Papers 2011-2017)
(Lahore & Gujranwala Board)
Q.1 Define escape sequence. (LHR 2011) (GRW 2013,14)
Ans: See Topical Q.10
Q.2 Define standard output. (LHR 2011) (GRW 2016)
Ans: See Kips Ex-Q.34
Q.3 Find the error in the (LHR 2011)
Main();
{
PRINTF(“Hello”);
}
Ans: See Kips Ex-Q.8
Q.4 What is control string in printf() function? (LHR 2013)
Ans: See Topical Q.1
Q.5 What is the use of scanf() function? Write syntax. (LHR 2014)
Ans: See Topical Q.12 & Kips Ex-Q.2
Q.6 What is format specifier? Write example (LHR 2014)
Ans: See Topical Q.3 & Q.4
Q.7 Trace the output (LHR 2014)
void main()
{
char w1, w2, w3;
w1=’A’;
w2=’B’;
w3=’C’;
printf(“%c %c w3”, w1,w2);
}
Ans: See Kips Ex-Q.9
Q.8 List some important functions for output. (LHR 2014) & (GRW 2014)
Ans: See Kips Ex-Q.10
Q.9 Trace the output
void main()
{
int a, b, temp;
a = 10;
b = 20;
temp = b;
b = a;
a = temp;
printf(“%d \n”, a);
printf(“%d \n”, b);
getch();
}
Ans: See Kips Ex-Q.11
SHORT QUESTIONS
(From Past Papers 2011-2017)
(Faisalabad + Sargodha + Rawalpindi Boards)
Q.1 Write C- statement output two values in two floating point variables a and b. (FSD 2013)
Ans: See Kips Ex-Q.20
Q.2 Trace output of the following code. (FSD 2013)
float f = 3.14159;
printf(“f = %4.2f”,f);
Ans: See Kips Ex-Q.21
Q.3 Find error in the following code (FSD 2013)
include<stdio.h>
main()
{
int I=1;
printf(“%d”,i);
}
Ans: See Kips Ex-Q.22
Q.4 What will the following code display? (RWP 2013)
printf(“555\t”);
Ans: See Kips Ex-Q.23
Q.5 Describe the use of ampersand (&) in scan function. (RWP 2013)
Ans: See Kips Ex-Q#5
Q.6 What is the purpose of Argument list in printf()? (RWP 2014)
Ans: See Topical Q#1
Q.7 What is escape sequence? (RWP 2014,17) (FSD 2017)
Ans: See Topical Q#10
Q.8 Find the error in the following code: (FSD 2016)
main ( );
{
printf (|Hellow);
}
Ans: See Kips Ex-Q.46
Q.9 Write down the use of format specifies om C – language. (FSD 2016)
Ans: See Topical Q#3
Q.10 List out the names of functions used for character input? (FSD 2016)
Ans: See Kips Ex-Q.34
Q.11 Why the ampersand (&) used in scanf is function? (SGD 2016)
Ans: See Kips Ex-Q.5
Q.12 What is the use of format specifier in C language? (SGD 2016)
Ans: See Topical Q.3
Q.13 Trace out errors in the following code: (SGD 2016)
float area , r
printf (“Enter Radius);
scanf(“Enter Radius);
Ans: See Kips Ex-Q.47