You are on page 1of 40

Chapter–10 Input / Output Function

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;
}

KIPS OBJECTIVE TYPE SERIES 203


Chapter–10 Input / Output Function

TOPICAL MULTIPLE CHOICE QUESTIONS


PRINT AND SCANF FUNCTION
1. The function printf and scanf can be accessed by including library __________.
(a) conio.h (b) stdio.h
(c) math.h (d) string.h
2. printf function takes as arguments a ___________ and an optional list of variables.
(a) Constant (b) Values
(c) Expression (d) Format string
3. Which of the following function is used to input data in the program.
(a) printf (b) scanf
(c) clrscr (d) putc
4. The number of variables can be used in printf function.
(a) One (b) Two
(c) Three (d) Many
5. The symbol & before the variable name represent.
(a) Value (b) Address
(c) Data type (d) None of these
6. The scanf function is defined in.
(a) conio.h (b) math.h
(c) stdio.h (d) All of above
FORMAT SPECIFIER
7. The format specifier for integer is __________.
(a) %f (b) %d
(c) %e (d) %9
8. Which of the following format specifier is used for float data type.
(a) %f (b) %d
(c) %g (d) %c
9. Format specifier is used in ___________.
(a) Loop (b) Constant
(c) Format string (d) None of these
10. The format specifier used for string data type is __________.
(a) %s (b) %c
(c) %d (d) %f
11. The format specifier %c is used for __________.
(a) String (b) Integer
(c) Character string (d) Character
12. The format specifier %s is used for___________.
(a) Character (b) String
(c) Integer (d) None of these
FIELD-WIDTH SPECIFIER
13. The number of columns used to display a value on screen is referred.
(a) Escape sequence (b) Array
(c) Structure (d) Field-width
14. If float a  .6789 and format specifier is %4.2f the output will be.
(a) 0.67 (b) .67
(c) 78 (d) 0.68
15. The escape sequence for new line is
(a) \r (b) \t
(c) \m (d) \n
KIPS OBJECTIVE TYPE SERIES 204
Chapter–10 Input / Output Function
16. The escape sequence used to erase a character left to the cursor position is.
(a) \e (b) \b
(c) /b (d) \t
17. The escape sequence for form feed is_________.
(a) /f (b) \fo
(c) \f (d) \d
18. \t escape sequence is used for________.
(a) New line (b) Form feed
(c) Tab (d) Space
CHARACTER INPUT
19. Which of the following function is used for character input.
(a) getch (b) getche
(c) scanf (d) All
20. getch() function is a part of __________ library.
(a) conio.h (b) stdio.h
(c) math.h (d) None of these
21. The functions that are very handy in character manipulation.
(a) printf (b) getch
(c) getche (d) Both b & c
22. The __________ function does not echo the character typed.
(a) getch (b) getche
(c) scanf (d) getc
KIPS EXERCISE
1. __________ function is used for formatted output.
(a) scanf (b) getch
(c) printf (d) None of these
2. The symbol for address of operator is __________.
(a) $ (b) &
(c) % (d) #
3. The escape sequence __________ represents the carriage return.
(a) \a (b) \t
(c) \n (d) \r
4. Escape sequence always begins with a __________.
(a) / (b) \
(c) % (d) f
5. The ASCII code for escape key is __________.
(a) 32 (b) 8
(c) 27 (d) 6
6. The format specifier is started with __________ symbol.
(a) \ (b) &
(c) | (d) %
7. The general form of format specifier for a floating point value is.
(a) %m.nf (b) m.n
(c) %f m.n (d) %d
8. Which of the following is not a valid escape sequence.
(a) \t (b) \n
(c) \” (d) None of these
KIPS OBJECTIVE TYPE SERIES 205
Chapter–10 Input / Output Function
9. getch() stand for.
(a) get input (b) get characters
(c) get character (d) All of above
10. Which of the following function do not accept any argument.
(a) clrscr() (b) getch()
(c) getche() (d) All of above
MULTIPLE CHOICE QUESTIONS
(From Textbook Exercise)
1. The function getche() is defined in:
(a) stdio.h (b) string.h
(c) math.h (d) conio.h
2. The escape sequence for backslash is:
(a) \ (b) \b
(c) \\ (d) \t
3. The format specifier %u is used for:
(a) Integer (b) Unsigned short
(c) Unsigned float (d) Unsigned long int
4. The manipulation two very small numbers result may be too small, this
phenomenon is called ________
(a) Arithmetic overflows (b) Arithmetic underflow
(c) Truncation (d) Round off
5. The symbol ‘=’, represents:
(a) Comparison operator (b) Assignment operator
(c) Equal-to operator (d) None of these
6. Which of the following operators has lowest precedence?
(a) ! (b) +
(c) = (d) = =
7. Relational operators are used to:
(a) Establish a relationship among variables (b) Compare two values
(c) Construct compound condition (d) Perform arithmetic operations
8. C is a strongly typed language, this means that:
(a) Every program must be compiled before execution.
(b) Every variable must be declared before it is being used.
(c) The variable declaration also defines the variable.
(d) Sufficient data types are available to manipulate each type of data.
9. The logical not operator, denoted by! is a:
(a) Ternary operator (b) Unary operator
(c) Binary operator (d) Bitwise operator
10. a   b is equivalent to:
(a) b  a (b) a  b
(c) a  a  b (d) b  b  a

KIPS OBJECTIVE TYPE SERIES 206


Chapter–10 Input / Output Function

MULTIPLE CHOICE QUESTIONS


(From Past Papers 2011-2017)
(Lahore + Gujranwala Board)
1. Format specifier is started with symbol. (LHR 2011)
(a) ? (b) %
(c) & (d) @
2. Format specifier for char data type is: (LHR 2013)
(a) %s (b) %c
(c) %d (d) %f
3. The printf is a __________ function: (LHR 2013)
(a) Built in (b) User define
(c) Local (d) All of these
4. The format specifier %u is used for: (LHR 2014)
(a) Integer (b) Unsigned short
(c) Unsigned float (d) Unsigned long
5. Which escape sequence can used to begin a new line in C: (LHR 2014,16) (GRW 2016)
(a) /a (b) /b
(c) \n (d) /m
6. An ampersand (&) before the name of a variable denotes: (GRW 2013,14) (LHR 2015)
(a) Actual value (b) Variable name
(c) Address (d) Data type
7. Which of the following function is used to input data in C-programs:
(GRW 2014) (LHR 2015)
(a) scanf (b) printf
(c) get (d) sqrt
8. The functions used for input and output are stored in header file: (LHR 2015)
(a) conio.h (b) math.h
(c) stdio.h (d) output.h
9. The format specified used for character type data is: (LHR 2015)
(a) %c (b) %d
(c) %e (d) %f
10. Which format specifier is used for integer data type? (GRW 2011)
(a) %d (b) %c
(c) %s (d) %f
11. How many variables can be used in print() function? (GRW 2013)
(a) One (b) Two
(c) Three (d) Many
12. The function that is used to display output on screen is called: (LHR 2017)
(a) scanf (b) pow
(c) display (d) printf
13. Which of the following is not valid escape code? (GRW 2017)
(a) \t (b) \v
(c) \y (d) \f

KIPS OBJECTIVE TYPE SERIES 207


Chapter–10 Input / Output Function

MULTIPLE C HOICE QUESTIONS


(From Past Papers 2011-2017)
(Faisalabad + Sargodha + Rawalpindi Boards)
1. The ampersand used in scanf() function is called. (SGD 2013)
(a) Address indicator (b) Cell address
(c) Control (d) Program
2. The function used to display output on screen is called: (SGD 2013)
(a) printf (b) scanf()
(c) getch() (d) gets ()
3. Which of the following format specifier is used for character data type: (SGD 2014)
(a) %S (b) %c
(c) %f (d) %d
4. The ASCII code for escape key is: (SGD 2014)
(a) 27 (b) 28
(c) 29 (d) 30
5. The function getch() is defined in: (RWP 2013)
(a) stdio.h (b) conio.h
(c) string.h (d) math.h
6. Which escape sequence is used to begin a new line in C? (SGD 2017) (RWP 2014)
(a) /a (b) /b
(c) /m (d) \n
7. Which of the following function is used to output data in C programs? (FSD 2016)
(a) printf (b) Getch
(c) Getche (d) Scanf
8. The function getche ( ) is defined in: (SGD 2016)
(a) Stdio. h (b) Math. h
(c) String. h (d) Conio. h
MULTIPLE CHOICE QUESTIONS
(From Past Papers 2011-2017)
(D.G Khan + Bahawalpur + Multan + Sahiwal Board)
1. The format specifier %U is used for: (D.G.K 2013)
(a) Integer (b) Unsigned short
(c) Unsigned float (d) Unsigned long int
2. Which of the following things are determined by format specifier: (D.G.K 2014)
(a) Data Type (b) Field Width
(c) Format of the Value (d) All of above
3. The function getch () is defined in: (D.G.K 2014)
(a) stdio.h (b) conio.h
(c) string.h (d) math.h
4. Which of the following format specifies is used for character data type: (BWP 2013)
(a) %c (b) %f
(c) %d (d) %x
5. Which of the following function is used to input numeric data in (BWP 2015, 17)
C-Language program:
(a) scanf () (b) printf ()
(c) get () (d) getch ()
KIPS OBJECTIVE TYPE SERIES 208
Chapter–10 Input / Output Function
6. Which programming structure makes a comparison: (BWP 2015)
(a) Relation (b) Decision
(c) Repetition (d) Iteration
7. What will be the output of the following point f(“%.2f”, 5.5555); (MTN 2013)
(a) 5.555 (b) 5.55
(c) 5.56 (d) 5.00
8. _________ is not a valid escape code. (MTN 2014,17)
(a) \t (b) \r
(c) \y (d) \f
9. The escape sequence for back slash is: (MTN 2014,17)
(a) \ (b) \b
(c) \\ (d) \t
10. The printf is a: (MTN 2014)
(a) Built in function (b) User defined function
(c) Keyword (d) None of these
11. scanf is an __________ function. (SWL 2013)
(a) Data (b) Information
(c) Input (d) Output
12. The escape sequence used to move cursor one character back is: (MTN 2016)
(a) \b (b) \t
(c) \n (d) \r
13. Which of the given is not a valid escape sequence: (BWP 2016)
(a) \ t (b) \ r
(c) \ y (d) \ f
14. Which of the following character is used to mark the end of the string: (BWP 2016)
(a) \ 0 (b) / 0
(c) \ a (d) \ n
15. The escape sequence for carriage return is (SWL 2017)
(a) \a (b) \c
(c) \r (d) \f

KIPS OBJECTIVE TYPE SERIES 209


Chapter–10 Input / Output Function

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

KIPS OBJECTIVE TYPE SERIES 210


Chapter–10 Input / Output Function

TOPICAL SHORT QUESTIONS


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 Write a program that calculate and print the area of a square.
Ans: Program
#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();
}
Output:
Area of square is =30
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 Write a program that print the sum of two floating point numbers.
Ans: Program
#include <stdio.h>
#include <conio.h>
void main()
{
float a, b, c;
clrscr();
a = 10.30;
b = 20.40;
KIPS OBJECTIVE TYPE SERIES 211
Chapter–10 Input / Output Function
c = a + b;
printf(" %f + %f = %f", a, b, c);
getch();
}
Output:
10.30 + 20.40 = 30.70
Q.6 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. A
field width specifier describe the number of columns that should be used to print a
value.
Q.7 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.8 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.nf where
m represents the total field width and n represents the desired number of decimal places.
Q.9 Give some examples for field width for floating point number:
Ans: The following table show how different floating values are displayed using different
format specifiers:
Value Format Specifier Displayed Output
3.14159 %5.2f □3.14
.6789 %4.2 0.69
-0.007 %4.2f -0.01
3.14159 %5.1f □□3.1
Q.10 What is Escape sequence?
Ans: 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 new line and \t mean tab.

KIPS OBJECTIVE TYPE SERIES 212


Chapter–10 Input / Output Function
Q.11 Write a program that display the following output using escape sequences characters.
Rollno Name Marks
--------------------------------------------------------------
10 Asim 56
11 Naveed 95
Ans: Program
#include<stdio.h>
#include<conio.h>
void main()
{
printf("Rollno \t \t Name \t \t \t Marks \n");
printf("10 \t \t Asim \t\ t \t 56 \n");
printf(“…………………………”);
printf("11 \t \t Abid \t\ t \t 95 \n");
getch();
}
Q.12 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.13 Write a program to convert the distance in kilometers into meters.
Ans: Program:
#include<stdio.h>
#include<conio.h>
void main()
{
double km, m;
clrscr();
printf("Enter the distance into kilometers : ");
scanf(" %lf ", &km);
m = km * 1000;
printf("Distance in meters = %lf ", m);
getch();
}
Q.14 Give some examples for field width for integer number:
Ans: The following table show how different integer values are displayed using different
format specifiers:

Value Format specifier Displayed output


786 %4d □786
786 %1d 786
786 %6d □□□786
-786 %2d -786

KIPS OBJECTIVE TYPE SERIES 213


Chapter–10 Input / Output Function

TOPICAL SHORT QUESTIONS


Q.1 What is printf() function. (LHR 2013,16) (RWP 2014) (MTN 2016)
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 Write a program that calculate and print the area of a square.
Ans: Program
#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();
}
Output:
Area of square is =30
Q.3 What is format specifier?
(LHR 2014) (GRW 2016) (FSD 2016) (SGD 2016) (RWP 2017) (D.G.K 2013)
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?
(LHR 2014) (D.G.K 2013) (MTN 2016) (BWP 2016)
Ans:
Symbol Data type
%d int, short
%f float
%lf double
%e float, double (Exponential Notation)

KIPS OBJECTIVE TYPE SERIES 214


Chapter–10 Input / Output Function
Q.5 Write a program that print the sum of two floating point numbers.
Ans: Program:
#include <stdio.h>
#include <conio.h>
void main()
{
float a, b, c;
clrscr();
a = 10.30;
b = 20.40;
c = a + b;
printf(" %f + %f = %f", a, b, c);
getch();
}
Output:
10.30 + 20.40 = 30.70
Q.6 What is meant by field width specifier. (LHR 2015, BWP 2013)
Ans: Field width specifier
The number of columns used to display a value on screen is referred to as field width. A
field width specifier describe the number of columns that should be used to print a value.
Q.7 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.8 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.nf where
m represents the total field width and n represents the desired number of decimal places.
Q.9 Give some examples for field width for floating point number: (GRW 2011)
Ans: The following table show how different floating values are displayed using different
format specifiers:
Value Format Specifier Displayed Output
3.14159 %5.2f □3.14
.6789 %4.2 0.69
-0.007 %4.2f -0.01
3.14159 %5.1f □□3.1
Q.10 What is Escape sequence? (LHR 2011) (GRW 2013,14) (RWP 2014,17) (FSD 2017) (D.G.K 2013,17)
Ans: 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 new line and \t mean tab.

KIPS OBJECTIVE TYPE SERIES 215


Chapter–10 Input / Output Function
Q.11 Write a program that display the following output using escape sequences characters.
Rollno Name Marks
--------------------------------------------------------------
10 Asim 56
11 Naveed 95
Ans: Program
#include<stdio.h>
#include<conio.h>
void main()
{
printf("Rollno \t \t Name \t \t \t Marks \n");
printf("10 \t \t Asim \t\ t \t 56 \n");
printf(“…………………………”);
printf("11 \t \t Abid \t\ t \t 95 \n");
getch();
}
Q.12 What is scanf() function. (LHR 2014) (GRW 2014) (RWP 2017)
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.13 Write a program to convert the distance in kilometers into meters.
Ans: Program
#include<stdio.h>
#include<conio.h>
void main()
{
double km, m;
clrscr();
printf("Enter the distance into kilometers : ");
scanf(" %lf ", &km);
m = km * 1000;
printf("Distance in meters = %lf ", m);
getch();
}
Q.14 Give some examples for field width for integer number: (GRW 2011)
Ans: The following table show how different integer values are displayed using different
format specifiers:
Value Format specifier Displayed output
786 %4d □786
786 %1d 786
786 %6d □□□786
-786 %2d -786

KIPS OBJECTIVE TYPE SERIES 216


Chapter–10 Input / Output Function

KIPS SHORT QUESTIONS


Q.1 Write the syntax of printf() function. (LHR 2015)
Ans: The syntax of printf function is
printf(format string);
printf(format string, var1, var2, var3,......);
The format string is a character string and variables are optional .
Q.2 Write the syntax of scanf() function.
Ans: The scanf 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,.....);
Q.3 Write the difference between getch and getche function.
(GRW 2013) (SGD 2017) (BWP 2013) (SWL 2016)
Ans: The getch and getche functions are used for character input. The getch function does not echo
the typed character while the getche function echo the typed character to the screen. Both of
these function do not accept any argument and there is no need to press enter key after typing
the character. The header file conio.h is included in the program to use these functions.
Q.4 Write a program that displays the ASCII code of the character typed by the user.
Ans: Program
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter a character : ");
ch = getche();
printf("\nThe ASCII code of %c is = %d ", ch, ch);
}
Output:
Enter a character : a
The ASCII code of a is = 97
Q.5 What is the use of & in scanf() function. (RWP 2013) (GRW 2017)
Ans: Use of & in scanf() function
An ampersand character(&) is used before the variable name. In C & is actually the address
of operator. The address of operator (&) tells the scanf function the address of the
variable where the input is to store. If & is omitted the scanf will not be able to locate and
store the value into the variable in memory and the program will find a garbage value in
the variable.

KIPS OBJECTIVE TYPE SERIES 217


Chapter–10 Input / Output Function
Q.6 Give different types of escape sequence characters used in C language.
Ans: The list of escape sequences characters used in C are as follows
Escape sequence Purpose
\n New line
\t Tab
\b Back space
\r Carriage Return (Enter Key)
\f Form feed
\' Single Quote
\" Double Quote
\\ Backslash
\xdd ASCII code in hexadecimal notation (each digit represent a digit
\ddd ASCII code in octal notation (each d represents a digit)
Q.7 What is the purpose of getch()? (D.G.K 2013) (MTN 2013)
Ans: The getch function is very handy in character manipulation. In contract to the getch
function which does not echo the character typed. This function do not accept any
argument.
main();
{
PRINTF(“Hello”);
}
Ans: Error
Declaration terminated incorrectly
Q.8 Trace the output (LHR 2014)
void main()
{
char w1, w2, w3;
w1=’A’;
w2=’B’;
w3=’C’;
printf(“%c %c w3”, w1,w2);
}
Ans: A B w3
Q.9 List some important functions for output. (LHR 2014) & (GRW 2014)
Ans: Important output functions are.
printf
puts
Q.10 Trace the output
void main()
{
int a, b, temp;
a = 10;
b = 20;
temp = b;
b = a;
a = temp;
KIPS OBJECTIVE TYPE SERIES 218
Chapter–10 Input / Output Function
printf(“%d \n”, a);
printf(“%d \n”, b);
getch();
}
Ans: 20
10
Q.11 Correct the following statement (LHR 2015)
printf(Hello World);
Ans: printf(“Hello World”);
Q.12 printf(“C programming is \t easy”);
Ans: C programming is easy
Q.13 Write down output of the following code (LHR 2015)
int x;
printf(“%f”,x);
Ans: Garbage value
Q.14 Trace error in the following code: (LHR 2015)
include<stdio.h>
void main()
printf (“c-Language”);
}
Ans: Declaration syntax error
Unexpected }
Q.15 What will be the output of following code? (GRW 2011)
printf(“Book \t reading \n”);
printf(“is a \t good habit”);
Ans: Book reading
is a good habit
Q.16 Trace out the errors if any
Main()
{
int h, w, a;
h = 5; w=4,
a = h * w;
printf(“Area = “, a);
}
Ans: There are six errors
undefined symbol Int
statement missing ; etc
Q.17 What is the output of the following code: (GRW 2013)
float avg= 98.20;
printf(“Average = %5.2f”, avg);
Ans: Average = 98.20
Q.18 Write the output of the following statements (GRW 2014)
main()
{
printf(“ 555 \n”);
printf(55”);
}
Ans: 555
55
KIPS OBJECTIVE TYPE SERIES 219
Chapter–10 Input / Output Function
Q.19 Write C- statement output two values in two floating point (FSD 2013)
variables a and b.
Ans: printf(“%f %f”, a, b);
Q.20 Trace output of the following code. (FSD 2013)
float f = 3.14159;
printf(“f = %4.2f”,f);
Ans: f = 3.14
Q.21 Find error in the following code (FSD 2013)
include<stdio.h>
main()
{
int I=1;
printf(“%d”,i);
}
Ans: undefined symbol i
I is assigned a value that is never used
Q.22 What will the following code display? (RWP 2013)
printf(“555\t”);
Ans: 555
Q.23 Trace the output of the following code. (D.G.K 2013)
float f = 3.14159;
printf(“%f”, f);
Ans: 3.141590
Q.24 Trace the error in the following code. (D.G.K 2013)
int a=10;
printf(“%d’ , A);
Ans: There are six error
Unterminated string or character constant
Function call missing ) etc
Q.25 Trace the error in the following code. (BWP 2013)
void man()
{
Prin( “ C language is interesting”);
}
Ans: function prin should have a prototype
Q.26 Discuss the purpose of %c format specifier? (BWP 2013)
Ans: character
Q.27 Trace the errors in the following code. (MTN 2013)
void main();
{
printf(‘pak’);
}
Ans: Declaration terminated incorrectly
Character constant must be one or two character long.
Q.28 What will be the output of the following (MTN 2013)
printf(“55\t”);
printf(“555”);
Ans: 55 555

KIPS OBJECTIVE TYPE SERIES 220


Chapter–10 Input / Output Function
Q.29 Give single printf statement to print the following message. (MTN 2014)
No. of employee
No. of officers.
Ans: printf(“No. of employee \n No. of officers.);
Q.30 Trace the output of the following code (MTN 2014)
{
int x, y, z;
x = 3;
y = 2;
z = 4;
printf(“%d %d %d ”, x+y, y+5, x+z);
}
Ans: 5 7 7
Q.31 Write the output of the following code. (SWL 2013)
main()
{
printf(“Well come\n”);
printf(“Thanks”);
}
Ans: Output:
Well come
Thanks
Q.32 What will be the output of the following code; if user enters 3,2, &4? (SWL 2013)
Int x, y, z;
printf(“Enter three no “);
scanf(“%d %d %d”, &x, &y, &z);
y = x + z;
x = z + 1;
printf(“%d %d “, x, y);
Ans: 5 7
Q.33 Discuss the purpose of %c format specifier? (BWP 2013)
Ans: character
Q.34 Define standard input & output? (LHR 2011)
Ans: in C, the standard input/output library provides functions to perform input and output
operations. By standard input and output, we mean the keyboard and monitor
respectively. In C, these input/output operations are performed by two standard
input/output functions, these are printf() nad scanf(). These functions can be accessed by
including the standard input/output library (stdio.h) in the program.
Q.35 Trace output: (LHR 2016)
Int n=6;
N++;
Printf(“%d”,n);
Ans: Output = 7
KIPS OBJECTIVE TYPE SERIES 221
Chapter–10 Input / Output Function
Q.36 Trace error; (LHR 2016)
Int x=5;
Int y
y= x+3
printf (“%d”’, y);
Ans: Statement terminator missing i.e
int x=5;
int y;
y= x+3;
printf (“%d”, y);
Q.37 Trace error: (LHR 2016)
Include<STDIO.H>
VOID Main ( );
{
Printf (“Pakistan”);
}
Ans: #Include<stdio.h >
void main ( );
{
Printf (“Pakistan”);
}
Q.38 Trace output (LHR 2016)
Int a=512;
Printf(“a=%5d”.a);
Printf (“a=%1d”, a);
Ans: Output = 512
512
Q.39 Determine the output of the following code segments. (GRW 2016)
#include<stdio.h>
#include<conio.h>
void main ( )
{
float f =3.14159
printf (“f=%3.2”, f)
getch ( );
}
Ans: Output = 3.14
Q.40 Find the putput of the following code (GRW 2016)
#include<stdio.h>
#include<conio.h>
void main ( )
{
int a, b, c;
a=5; b=10
c= a+b;
printf (“then sum of a+b=%d”, c);
}
Ans: Output = the sum of a+b=15
KIPS OBJECTIVE TYPE SERIES 222
Chapter–10 Input / Output Function
Q.41 Find any two errors of following code: (LHR 2017)
int number = 6
number++;
print f(%d\n”,number);
Ans: int number = 6;
number ++;
printf(;%d\n”,number);
Q.42 Write down output of the following: (LHR 2017)
float f = 3.14159;
print f (“f=%4.2f”, f);
Ans: Output = 3.14
Q.43 Find error (LHR 2017)
{
Float area, r
Print f(“Enter radius”);
}
Ans: {
float area, r;
printf(“Enter radius”);
}
Q.44 What will be the output from the following code: (GRW 2017)
void main ( )
{ int a, b, temp;
a = 10;
b = 20;
temp = a;
a = b;
b = temp;
printf (“a = %d\t b=%d”, a, b);
}
Ans: Output = 10,10
Q.45 Trace errors from the following: (GRW 2017)
void main ( );
{ floati = 15.6;
printf (“%d”,i);
}
Ans: void main ( )
{ float i = 15.6;
printf (“%f”,i);
}
Q.46 Find the error in the following code: (FSD 2016)
main ( );
{
printf (|Hellow);
}
Ans: void main ( )
{
printf (Hellow);
}
KIPS OBJECTIVE TYPE SERIES 223
Chapter–10 Input / Output Function
Q.47 Trace out errors in the following code: (SGD 2016)
float area , r
printf (“Enter Radius);
scanf(“Enter Radius);
Ans: float area , r;
printf (“Enter Radius”);
scanf(“Enter Radius”);
Q.48 Find the errors in the following piece of code: (FSD 2017)
VOID MAIN ( )
{ int a =10 :
Printf ( “%f ” ,a) ;
Ans: void main ( )
{ int a =10 ;
Printf ( “%d ” ,a) ;
}
Q.49 Trace the errors from the following piece of code: (FSD 2017)
include < stdio.h >
void main [ ]
{ printf, ( ‘pakistan’):
Ans: #include < stdio.h >
void main ()
{ printf ( “pakistan”);
}
Q.50 Trace output: (FSD 2017)
void main ( )
{ int a, b:
a = 10:
b = 12:
int c= a+ b:
printf (“c = % 3 d” ,--c )”
Ans: Output=21
Q.51 Find the errors in the following code: (SGD 2017)
# includes (stdio.h)
# includes (conio.h)
void. Main (void)
Intx,y,z;
x=y=z=3;
x=4;
y=x;
z=2;
print f (“%d%d%d”, x,y,z);
Ans: # include <stdio.h>
# include <conio.h>
void main (void)
{
Int x,y,z;
x=y=z=3;
KIPS OBJECTIVE TYPE SERIES 224
Chapter–10 Input / Output Function
x=4;
y=x;
z=2;
Printf (“%d%d%d”, x,y,z);
}
Q.52 Find the errors in the following code: (SGD 2017)
main ();
{
Print f(Hello)
}
Ans: void main ()
{
Printf (“Hello”);
}
Q.53 Trace the output. (RWP 2017)
printf(“Book\t Reading \n”);
printf(“isa\t very good”);
printf(“Habbit”);
Ans: Output = Book Reading
isa very good
Habbit
Q.54 Trace the output. (RWP 2017)
float f=3.14159
Printf(“f=%5.1f”,f);
Ans: Output = 3.1
Q.55 Predict the output of the following: (SWL 2016)
float f= 3.14159
print f (“f=%4.2f”, f);
Ans: Output =3.14
Q.56 Predict the output of the following (SWL 2016)
int a = 9 ;
a=a%4;
print f (“\n%d is result”, a) ;
Ans: Output = 2

Q.57 Trace out errors in the following code: (SWL 2016)


float area , r,
clrscr( );
printf(“enter radius);
scanf(“%f”, r);
Ans: float area , r;
clrscr( );
printf(“enter radius”);
scanf(“%f”, r);
KIPS OBJECTIVE TYPE SERIES 225
Chapter–10 Input / Output Function
Q.58 Trace the errors in the following code: (MTN 2016)
# include (std 10.h)
void main (void);
{
x = 5;
y = 6;
z=x+y
printf (“%d”,z);
}
Ans: # include <stdio .h>
void main (void)
{
x = 5;
y = 6;
z = x + y;
printf (“%d”,z);
}
Q.59 What is the output of the following code? (MTN 2016)
Printf (“I\n Love\n programming”);
Ans: Output =
I
Love
Programming

Q.60 Find the output of following code segment: (MTN 2016)


int x = 10;
int y = 5;
int z = x + y;
printf (“%d %d %d”, x, y, z);
Ans: Output = 10 5 15
Q.61 Trace out errors in the following code: (BWP 2016)
Float r ;
Clrscr( )
print f (″ enter radius) ;
Scanf (″ % f ″ ; r ) ;
Ans: float r ;
Clrscr( );
printf (″ enter radius”) ;
Scanf (″ % f ″ , r ) ;
Q.62 Find errors: (SWL 2017)
Main [ ] ;
(
float n ;
print f (“% d”; n);
}
Ans: void main ()
{
float n ;
print f (“% d”,n);
}
KIPS OBJECTIVE TYPE SERIES 226
Chapter–10 Input / Output Function
Q.63 Show output: (SWL 2017)
int I = 515;
print f (“ i=%5d”,i);
print f (“i=%2d”, i);
Ans: Output = 512
512
Q.64 Write the output of the following code:- (MTN 2017)
Int x=9;
X= x%4;
Printf(“\n%d is result”,x);
Ans: Output = 2
Q.65 Trace the output of the following code. (MTN 2017)
{
Intx,y,z;
x=3;
y=2
z=4
print(‘%d%d%d,x+y,y+5,x+z);
}
Ans: Output = 5 7
Q.66 Write the output float f = 3. 14159; (D.G.K 2017)
Print f (“% 7.3 f”, f);
Ans: Output = 3.141
Q.67 Find the error void main ( ) (D.G.K 2017)
{
int a = 10 ;
Print f(“%s”, b); }
Ans: {
int a = 10 ;
Printf(“%d”, a); }
Q.68 Trace output of the following code: (BWP 2017)
inti = -786 ;
print f ( “ %6d” , i);
Ans: -786
Q.69 Find tout the output of following code: (BWP 2017)
int“ = 10 , b = 12;
sum “ a + b ;
print f (“%d:, sum);
Ans: Output = 22

KIPS OBJECTIVE TYPE SERIES 227


Chapter–10 Input / Output Function

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.

KIPS OBJECTIVE TYPE SERIES 228


Chapter–10 Input / Output Function
Q.8 Give some examples for field width for floating point number:
Ans: The following table show how different floating values are displayed using different
format specifiers:
Value Format Specifier Displayed Output
3.14159 %5.2f □3.14
.6789 %4.2 0.69
-0.007 %4.2f -0.01
3.14159 %5.1f □□3.1
Q.9 What is Escape sequence?
Ans: 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.
Q.10 What is the purpose of getch()?
Ans: 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.
Q.11 Give different types of escape sequence characters used in C language.
Ans: The list of escape sequences characters used in C are as follows
Escape sequence Purpose
\n New line
\t Tab
\b Back space
\r Carriage Return (Enter Key)
\f Form feed
\' Single Quote
\" Double Quote
\\ Backslash
\xdd ASCII code in hexadecimal notation (each digit represent a digit
\ddd ASCII code in octal notation (each d represents a digit)
Q.12 Show the output displayed by the following program when the data entered and 10 and 15.
Ans: Program
#include<stdio.h>

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

KIPS OBJECTIVE TYPE SERIES 232


Chapter–10 Input / Output Function
Q.10 Correct the following statement (LHR 2015)
printf(Hello World);
Ans: See Kips Ex-Q.12
Q.11 printf(“C programming is \t easy”);
Ans: See Kips Ex-Q.13
Q.12 Write down output of the following code (LHR 2015)
int x;
printf(“%f”,x);
Ans: See Kips Ex-Q.14
Q.13 Write down the syntax of scanf function (LHR 2015)
Ans: See Kips Ex-Q.1
Q.14 Define field width. (LHR 2015)
Ans: See Topical Q.6
Q.15 Trace error in the following code: (LHR 2015)
Include<stdio.h>
void main()
printf (“c-Language”);
}
Ans: See Kips Ex-Q.15
Q.16 List out different format specifiers for integer and floating-point values.
Ans: See Topical Q.14 & Q.9
Q.17 What will be the output of following code? (GRW 2011)
printf(“Book \t reading \n”);
printf(“is a \t good habit”);
Ans: See Kips Ex-Q.16
Q.18 What is formatted input/output. (GRW 2011)
Ans: See Kips Ex-Q.34
Q.19 Trace out the errors if any
main()
{
int h, w, a;
h = 5; w=4,
a = h * w;
printf(“Area = “, a);
}
Ans: See Kips Ex-Q.17
Q.20 What is the output of the following code: (GRW 2013)
float avg= 98.20;
printf(“Average = %5.2f”, avg);
Ans: See Kips Ex-Q.18
Q.21 What is the purpose of getch(). (GRW 2013)
Ans: See Kips Ex-Q# 3
Q.22 What is the use of scanf function. (GRW 2014)
Ans: See Topical Q#12

KIPS OBJECTIVE TYPE SERIES 233


Chapter–10 Input / Output Function
Q.23 Write the output of the following statements (GRW 2014)
main()
{
printf(“ 555 \n”);
printf(55”);
}
Ans: See Kips Ex-Q.19
Q.24 What is standard input? (LHR 2016,17)
Ans: See Kips Ex-Q.34
Q.25 Give one example of control string in printf function. (LHR 2016)
Ans: See Topical Q.1
Q.26 Trace output: (LHR 2016)
Int n=6;
N++;
Printf(“%d”,n);
Ans: See Kips Ex-Q.35
Q.27 Trace error; (LHR 2016)
Int x=5;
Int y
y= x+3
printf (“%d”’, y);
Ans: See Kips Ex-Q.36
Q.28 Trace error: (LHR 2016)
Include<STDIO.H>
VOID Main ( );
{
Printf (“Pakistan”);
}
Ans: See Kips Ex-Q.37
Q.29 Trace output (LHR 2016)
Int a=512;
Printf(“a=%5d”.a);
Printf (“a=%1d”, a);
Ans: See Kips Ex-Q.38
Q.30 Write the use of format specifiers in C-Language. (GRW 2016)
Ans: See Topical Q.3
Q.31 Determine the output of the following code segments. (GRW 2016)
#include<stdio.h>
#include<conio.h>
void main ( )
{
float f =3.14159
printf (“f=%3.2”, f)
getch ( );
}
Ans: See Kips Ex-Q.39
KIPS OBJECTIVE TYPE SERIES 234
Chapter–10 Input / Output Function
Q.32 Find the putput of the following code (GRW 2016)
#include<stdio.h>
#include<conio.h>
void main ( )
{
int a, b, c;
a=5; b=10
c= a+b;
printf (“then sum of a+b=%d”, c);
}
Ans: See Kips Ex-Q.40
Q.33 Find any two errors of following code: (LHR 2017)
int number = 6
number++;
print f(%d\n”,number);
Ans: See Kips Ex-Q.41
Q.34 Write down output of the following: (LHR 2017)
float f = 3.14159;
print f (“f=%4.2f”, f);
Ans: See Kips Ex-Q.42
Q.35 Find error: (LHR 2017)
{
float area, r
print f(“Enter radius”);
}
Ans: See Kips Ex-Q.43
Q.36 What will be the output from the following code: (GRW 2017)
void main ( )
{ int a, b, temp;
a = 10;
b = 20;
temp = a;
a = b;
b = temp;
printf (“a = %d\t b=%d”, a, b);
}
Ans: See Kips Ex-Q.44
Q.37 Why & operator is used in scanf ( ) function? (GRW 2017)
Ans: See Kips Ex-Q.5
Q.38 Trace errors from the following: (GRW 2017)
void main ( );
{ floati = 15.6;
printf (“%d”,i);
}
Ans: See Kips Ex-Q.45

KIPS OBJECTIVE TYPE SERIES 235


Chapter–10 Input / Output Function

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

KIPS OBJECTIVE TYPE SERIES 236


Chapter–10 Input / Output Function
Q.14 Find the errors in the following piece of code: (FSD 2017)
VOID MAIN ( )
{ int a =10 :
Printf ( “%f ” ,a) ;
Ans: See Kips Ex-Q.48
Q.15 Trace the errors from the following piece of code: (FSD 2017)
Include < stdio.h >
Void main [ ]
{ printf, ( ‘pakistan’):
Ans: See Kips Ex-Q.49
Q.16 Trace output: (FSD 2017)
void main ( )
{ int a, b:
a = 10:
b = 12:
int c= a+ b:
printf (“c = % 3 d” ,--c )”
Ans: See Kips Ex-Q.50
Q.17 Find the errors in the following code: (SGD 2017)
# includes (stdio.h)
# includes (conio.h)
Void. Main (void)
Intx,y,z;
x=y=z=3;
x=4;
y=x;
z=2;
Print f (“%d%d%d”, x,y,z);
Ans: See Kips Ex-Q.51
Q.18 Find the errors in the following code: (SGD 2017)
Main ();
{
Print f(Hello)
}
Ans: See Kips Ex-Q.52
Q.19 List some important functions for input and output (SGD 2017)
Ans: See Kips Ex-Q.34
Q.20 Describe “getche” function (SGD 2017)
Ans: See Kips Ex-Q.3
Q.21 Write the use of scanf function. (RWP 2017)
Ans: See Topical Q.12
Q.22 What is the use of format specifier in C-language? (RWP 2017)
Ans: See Topical Q.3
Q.23 Trace the output. (RWP 2017)
Printf(“Book\t Reading \n”);
Printf(“isa\t very good”);
Printf(“Habbit”);
KIPS OBJECTIVE TYPE SERIES 237
Chapter–10 Input / Output Function
Ans: See Kips Ex-Q.53
Q.24 Trace the output. (RWP 2017)
float f=3.14159
Printf(“f=%5.1f”,f);
Ans: See Kips Ex-Q.54
SHORT QUESTIONS
(From Past Papers 2011-2017)
(D.G Khan + Bahawalpur + Multan + Sahiwal Board)
Q.1 Trace the output of the following code. (D.G.K 2013)
float f = 3.14159;
printf(“%f”, f);
Ans: See Kips Ex-Q.24
Q.2 Define getch() function. (D.G.K 2013) (MTN 2017)
Ans: See Kips Ex-Q.7
Q.3 Trace the error in the following code. (D.G.K 2013)
int a=10;
printf(“%d’ , A);
Ans: See Kips Ex-Q.25
Q.4 What is escape sequence? Give example (D.G.K 2013,17)
Ans: See Topical Q.10
Q.5 What is format specifier? Give one example. (D.G.K 2013)
Ans: See Topical Q.3 and Q.4
Q.6 Compare the getch() and getch function(). (BWP 2013) (SWL 2017)
Ans: See Kips Ex-Q.7
Q.7 Trace the error in the following code. (BWP 2013)
void man()
{
Prin( “ C language is interesting”);
}
Ans: See Kips Ex-Q.26
Q.8 Discuss the purpose of %c format specifier? (BWP 2013)
Ans: See Kips Ex-Q.33
Q.9 What is the use of field width specifier? (BWP 2013)
Ans: See Topical Q.6
Q.10 Trace the errors in the following code. (MTN 2013)
void main();
{
printf(‘pak’);
}
Ans: See Kips Ex-Q.27
Q.11 What will be the output of the following (MTN 2013)
printf(“55\t”);
printf(“555”);
Ans: See Kips Ex-Q.28

KIPS OBJECTIVE TYPE SERIES 238


Chapter–10 Input / Output Function
Q.12 Give single printf statement to print the following message. (MTN 2014)
No. of employee
No. of officers.
Ans: See Kips Ex-Q.29
Q.13 Trace the output of the following code (MTN 2014)
{
int x, y, z;
x = 3;
y = 2;
z = 4;
printf(“%d %d %d ”, x+y, y+5, x+z);
}
Ans: See Kips Ex-Q.30
Q.14 What is the use of “ scanf()” function? Write its syntax. (SWL 2016)
Ans: See Topical Q.12, kips Ex-Q.2
Q.15 Describe the “getche()” function. (SWL 2016)
Ans: See Kips Ex-Q.3
Q.16 Predict the output of the following: (SWL 2016)
float f= 3.14159
print f (“f=%4.2f”, f);
Ans: See Kips Ex-Q.55
Q.17 Predict the output of the following (SWL 2016)
int a = 9 ;
a=a%4;
print f (“\n%d is result”, a) ;
Ans: See Kips Ex-Q.56
Q.18 Trace out errors in the following code: (SWL 2016)
float area , r,
clrscr( );
printf(“enter radius);
scanf(“%f”, r);
Ans: See Kips Ex-Q.57
Q.19 List out four types of format specifier. (MTN 2016)
Ans: See Topical Q.4
Q.20 What is the control string in printf function? (MTN 2016)
Ans: See Topical Q.1
Q.21 Trace the errors in the following code: (MTN 2016)
# include (std 10.h)
void main (void);
{
x = 5;
y = 6;
z=x+y
printf (“%d”,z);
}
Ans: See Kips Ex-Q.58

KIPS OBJECTIVE TYPE SERIES 239


Chapter–10 Input / Output Function
Q.22 What is the output of the following code? (MTN 2016)
Printf (“I\n Love\n programming”);
Ans: See Kips Ex-Q.59
Q.23 Find the output of following code segment: (MTN 2016)
int x = 10;
int y = 5;
int z = x + y;
printf (“%d %d %d”, x, y, z);
Ans: See Kips Ex-Q.60
Q.24 Write down the name of any four escape character provided by C. (BWP 2016)
Ans: See Topical Q.4
Q.25 Trace out errors in the following code: (BWP 2016)
Float r ;
Clrscr( )
print f (″ enter radius) ;
Scanf (″ % f ″ ; r ) ;
Ans: See Kips Ex-Q.61
Q.26 Find errors: (SWL 2017)
Main [ ] ;
(
float n ;
print f (“% d”; n);
}
Ans: See Kips Ex-Q.62
Q.27 Show output: (SWL 2017)
int I = 515;
print f (“ i=%5d”,i);
print f (“i=%2d”, i);
Ans: See Kips Ex-Q.63
Q.28 Write the output of the following code:- (MTN 2017)
int x=9;
X= x%4;
printf(“\n%d is result”,x);
Ans: See Kips Ex-Q.64
Q.29 Trace the output of the following code. (MTN 2017)
{
int x,y,z;
x=3;
y=2
z=4
print(‘%d%d%d,x+y,y+5,x+z);
}
Ans: See Kips Ex-Q.65
Q.30 Write the output float f = 3. 14159; (D.G.K 2017)
Print f (“% 7.3 f”, f);
Ans: See Kips Ex-Q.66

KIPS OBJECTIVE TYPE SERIES 240


Chapter–10 Input / Output Function
Q.31 Find the error void main ( ) (D.G.K 2017)
{
int a = 10 ;
Print f(“%s”, b); }
Ans: See Kips Ex-Q.67
Q.32 Trace output of the following code: (BWP 2017)
inti = -786 ;
print f ( “ %6d” , i);
Ans: See Kips Ex-Q.68
Q.33 Find tout the output of following code: (BWP 2017)
int“ = 10 , b = 12;
sum “ a + b ;
print f (“%d:, sum);
Ans: See Kips Ex-Q.69
Q.34 Write the output of the following code. (SWL 2013)
main()
{
printf(“Well come\n”);
printf(“Thanks”);
}
Ans: See Kips Ex-Q.31
Q.35 What will be the output of the following code; if user enters 3,2, &4? (SWL 2013)
Int x, y, z;
printf(“Enter three no “);
scanf(“%d %d %d”, &x, &y, &z);
y = x + z;
x = z + 1;
printf(“%d %d “, x, y);
Ans: See Kips Ex-Q.32

KIPS OBJECTIVE TYPE SERIES 241

You might also like