You are on page 1of 11

INPUT/OUTPUT

Q1. What is an output statement? Explain the printf() function of C language.


Ans.
Output Statements:
• Output statements (functions) are used to get data from the program.
• Output produced by monitor is called standard output.
Example:
printf( ) Function:
• It is used to display messages.
• It can be pronounced as print-eff function.
• It can be accessed by including standard input / output header file stdio.h in the
program.
• It takes formatted string and list of variables as an argument.
• The values of variables are displayed according the specified format in the string.
• We can use printf ( ) function in two ways.
1. To print a constant message
Syntax: printf ( “ Message ” );
Example:
printf( “Hello World “); // output is Hello World
2. To print values of Variables:
Syntax: printf(“Format specifiers Control string” , var-1, var-2,……..);
Control string consists of text, format specifiers and escape sequence and is
enclosed in double quotation marks.
Example:
A=2;
printf(“%d”, A); // output is 2
%d is format specifier for integer variable A
printf(“%f”, A); // output is 2.000000
%f is format specifier for float value.
% is format symbol.
[Ch#10] Input/Output 270 Computer Science Part-II
Q2. What are format specifier? Also write down the associated data types.
Ans.
Format Specifiers:
• A format specifier represents data type field width and format of a value of a variable
displayed on the screen.
• A format specifier always begins with the symbol %.
• Format specifiers are used for both input and output statements.
• The general syntax of format specifies is.
% Flag Field_Width Precision Conversion_Character
• % It specifies the beginning of the format specifier.
• Flag
o − or + symbol can be used as flag. Its use is optional.
o + is used to display a + sign with positive numbers.
o − is used to display output left justified. BY default the output is right justified.
• Field width A number is used as field width. It specifies the minimum number
of columns used for a value.
• Precision It is a number used for real values. A precision n specifies that n
decimal places will be shown in output.
• Conversion Character It is used to convert output value in a specified format.
Different conversion characters are used for different type of data. Its use in format
specifier is compulsory.
• In format specifier the use of % and conversion character is compulsory. The use of
flag, field width and precision is optional.
• For different types different format specifiers are used.
Different format specifiers are:
Format Specifier Data Type
a) %d int, short
b) %f float
c) %lf double
d) %e float, double (exponential notation)
e) %g floating point (%f or %e whichever is shorter)
f) %c char
g) %s character string
h) %u unsigned int, unsigned short
i) %x unsigned hexadecimal integer
j) %i integers
[Ch#10] Input/Output 271 Computer Science Part-II
k) %ld long integers
Program:
Write a program to declare and initialize data into an integer type variable x and
print the value of x in:
o Decimal format.
o The ASCII character of the integer value.
o The Hexadecimal value of the integer value.
o The Octal value of the integer value.
#include<stdio.h>
void main ( )
{
int x = 97;
printf(“The decimal value of x = %d\n”,x);
printf(“The ASCII value of x = %c\n”,x);
printf(“The Hexadecimal value of x = %X\n”,x);
printf(“The octal value of x = %o\n”,x);
}
Output:
The decimal value of x = 97
The ASCII value of x = a
The Hexadecimal value of x = 61
The octal value of x = 141
Q3. What are field width specifiers? Explain with the help of examples.
Ans.
Field Width Specifier:
• It is defined as the number of columns used to display a value on the screen.
• Its use is optional.
• If the value requires more columns, then the field is expanded.
[Ch#10] Input/Output 272 Computer Science Part-II
• If the value is smaller than the specified number of columns then the extra spaces are
padded before or after the value.
• The space will be printed as an extra character in output.
Example:
Integers:
Program Statement with field width Output ( � means spaces)
specifier
Printf ( “%d”,786); 786
Printf ( “%4d”,786); � 786
printf ( “%5d”,786); � �786
printf ( “%4d”,-786); -786
printf ( “%5d”,-786); � -786
printf ( “%1d”,786); 786 (expanded if width is small)
printf ( “%2d”,-786); -786 (expanded if width is small)
Float:
• In floating point we must specify the total field width and the desired decimal places
(precision).
• The total field should be large enough to accommodate all digits before and after the
decimal point.
• Total field should include a space for a decimal point as well as for the minus sign.
• Zero is always printed before a number in which integral part is not mentioned
(means zero e.g. .12) or less than 1.
• The general form for the floating point will be %m.nf
• m represents the total field width and n represents the desired number of decimal
places.
Program Statement with field width specifier Output (� means spaces)
printf ( “%6.2f”,-25.41); -25.41
printf ( “%5.2f”,3.14159); � 3.14
printf ( “%6.2f”,.123); � �0.12
printf ( “%5.1f”,3.14159); � �3.1
printf ( “%8.5f”,3.14159); �3.14159
printf ( “%4.2f”,-0.007); -0.01
printf ( “%8.5f”,-0.007); -0.00700
printf ( “%.4f”,-3.14159); -3.1416
printf ( “%5.3f”,3.14159); 3.141
[Ch#10] Input/Output 273 Computer Science Part-II
printf ( “%4.2f”,.6789); 0.69
printf ( “%8.3f”,-.007); � �-0.007
printf ( “%.3f”,-.007); -0.007
Program:
Write a program to declare and initialize data to a variable × of float type and
print its value on the screen using the control characters %f, %e, %E, %g and %G.
#include<stdio.h>
void main( )
{
float x = 6714.987697f;
printf(“Value of x using control character f = %f\n”,x);
printf(“Value of x using control character e = %e\n”,x);
printf(“Value of x using control character E = %E\n”,x);
printf(“Value of x using control character g = %g\n”,x);
printf(“Value of x using control character G = %G\n”,x);
}
Output:
Value of x using control character f = 6714.987793
Value of x using control character e = 6.714988e+03
Value of x using control character E = 6.714988E+03
Value of x using control character g = 6714.99
Value of x using control character G = 6714.99
Q4. What are escape sequences?
Ans.
Escape Sequences:
• These are characters which are used to control output on the output devices.
• These characters are not printed on the output devices.
• An escape sequence is a combination of a backslash and a single character.
• Backslash \ is called control character.
• And next single character after backslash is called code character.
[Ch#10] Input/Output 274 Computer Science Part-II
• These are used in control string.
• These characters cause a different meaning from the normal interpretation.
• Different Escape Sequences are:
Escape Sequence Purpose
a) \n New line
b) \t Tab
c) \b Back space
d) \r Carriage return (Enter key)
e) \f Form feed
f) \’ Single quote
g) \” Double quote
h) \\ Backslash
i) \xdd ASCII code in hexadecimal notation (each d represent a digit)
j) \ddd ASCII code in octal notation (each d represent a digit)
k) \? Question mark
Examples:
Program Statement with escape sequence Output
printf ( “Name\troll No\tMarks”); Name Roll No Marks
printf(“Amir \n ali”); Amir
ali
printf(“Amir \”Ali\””); Amir “Ali”
printf(“Amir \’Ali\’”); Amir ‘Ali’
printf(“Amir \\Ali\\”); Amir \Ali\
Q5. What is an input statement? Also discuss its different types.
Ans.
Input Statements:
• Input statements (functions) are used to provide data to the program.
• The input given to the computer through keyboard is called standard input.
• There are two types of input
1. Design time input
[Ch#10] Input/Output 275 Computer Science Part-II
2. Run time input
Design Time Input:
• It is also called programmer’s input or simple input.
• It is given by the programmer at the time of writing programs (coding).
• It remains constant during the execution of the program.
Example:
int x=5,y=9; //programmer assigns values to the variables in the program
float a=10.2;
Run Time Input:
• It is given at the time of execution of the program (after compilation).
• It is called user input.
• Examples are:
• scanf() Function // to get value of variable
• getch() Function // to get a single character from keyboard
• getche() Function // to get a single character from keyboard
scanf() Function:
• It is used to get values of the variables (numeric and string).
• It can be pronounced as scan-eff function.
• It can be accessed by including standard input / output header file stdio.h in the
program (means stdio.h is a prototype for scanf( ) function).
• It takes formatted control string and list of variables as an argument to hold the value
of variables.
• scanf() requires the address of the variable to store the value into it.
• Ampersand sign (&) is used before the variable name as an address of operator.
• If & sign is omitted the scanf function will not locate the variable in memory.
Syntax:
scanf(“List of Format specifiers”, &var-1, &var-2, &var-3, ……..);
Examples:
Input of a single variable
int a;
[Ch#10] Input/Output 276 Computer Science Part-II
scanf(“%d”, &a); // To get the value of a at run time
%d is format specifier for integer variable a
% is format symbol.
Input of more than one variable
int a,b;
float c;
scanf(“%d%d”, &a,&b);// To get the value of a and b at run time
scanf(“%f”, &c); // To get the value of c at run time
scanf(“%d%f%d”, &a,&c,&b); // To get the value of a, c and b
Program:
Write a program to input the marks obtained by a student in three subjects.
Calculate the total marks and their average and print the results on the screen.
#include<stdio.h>
void main( )
{
float total, sl,s2,s3,avg;
printf(“Enter marks of first subject?”);
scanf(“%f”, &s1);
printf(“Enter marks of second subject?”);
scanf(“%f”, &s2);
printf(“Enter marks of third subject?”);
scanf(“%f”, &s3);
total = s1+s2+s3;
avg = total/3;
printf(“\nTotal marks = %f”,total);
printf(“\nAverage marks = %f”,avg);
}
Output:
Enter marks of first subject ? 92
Enter marks of second subject ? 95
[Ch#10] Input/Output 277 Computer Science Part-II
Enter marks of third subject ? 98
Total marks = 285.000000
Average marks = 95.000000
Q6. What are getch() and getche() functions?
Ans.
getch() Function:
• It is used to get a single character as input from the keyboard during the execution of
the program.
• When a character key is pressed, the entered character is not displayed on the screen.
• The prototype of getch( ) function is in conio.h (console input/output) header file.
• It does not accept any argument.
• There is no requirement of pressing enter key after typing the character.
Syntax:
getch( ); or variable_name = getch( );
variable name is optional.
getche() Function:
• It is similar to getch() function and stands for get character with echo.
• It is also used to get a single character as input from the keyboard during the program
execution.
• Difference between getch( ) and getche( ) function is when a character key is pressed,
the entered character is displayed on the screen.
• The prototype of getche( ) function is conio.h (console input/output) header file.
• It does not accept any argument.
• There is no requirement of pressing enter key after typing the character.
Syntax: getche( ); or variable = getche( ); Variable is optional.
Program:
Write a program in C language that takes a number of three digits from user and
prints it in reverse order.
#include<stdio.h>
#include<conio.h>
void main( )
[Ch#10] Input/Output 278 Computer Science Part-II
{
int n,x,y,r;
clrscr( );
printf(“Enter any number of three digits\t”);
scanf(“%d”,&n);
x=n/100;
n=n%100;
y=n/10;
n=n%10;
r=n*100 + y*10 + x;
printf(“Reverse of number is %d”,r);
getch( );
Output:
Enter any number of three digits 584
Reverse of number is 485
Program:
Write a program in C language that takes roll number, name and address of a
student and display it on screen.
#include<stdio.h>
#include<conio.h>
void main( )
{
int rollno;
char name[30], address[50];
clrscr( );
printf(“Enter Your Roll Number\t”);
scanf(“%d”, &rollno);
printf(“Enter Your Name\t”);
scanf(“%s”, &name);
printf(“Enter Your Address\t”);
scanf(“%s”, &address);
printf(“Your Roll Number is %d\n”, rollno);
printf(“Your Name is %s\n”,name);
[Ch#10] Input/Output 279 Computer Science Part-II
printf(“Your Address is %s\n”, address);
getch( );
}

You might also like