Short Notes: Formatted Input-Output | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


FORMATTED INPUT-OUTPUT
Data can be entered & displayed in a particular format. Through format specifications, better 
presentation of results can be obtained.
Variations in Output for integer & floats:
# include <stdio.h> 
int main()
{
printf("Case 1:%6d\n",9876);
/* Prints the number right justified within 6 columns */ 
printf("Case 2:%3d\n",9876);
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not 
right justified */
printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */ 
printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */ 
printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation (scientific notation) */ 
return 0;
}
Output
Case 1: 9876 
Case 2:9876 
Case 3:987.65 
Case 4:988
Case 5:9.876543e+002
Variations in Input for integer and floats:
#include <stdio.h> 
int main()
{
int a,b; 
float c,d;
printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below */
Page 2


FORMATTED INPUT-OUTPUT
Data can be entered & displayed in a particular format. Through format specifications, better 
presentation of results can be obtained.
Variations in Output for integer & floats:
# include <stdio.h> 
int main()
{
printf("Case 1:%6d\n",9876);
/* Prints the number right justified within 6 columns */ 
printf("Case 2:%3d\n",9876);
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not 
right justified */
printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */ 
printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */ 
printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation (scientific notation) */ 
return 0;
}
Output
Case 1: 9876 
Case 2:9876 
Case 3:987.65 
Case 4:988
Case 5:9.876543e+002
Variations in Input for integer and floats:
#include <stdio.h> 
int main()
{
int a,b; 
float c,d;
printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below */
scanf("%d%d", &a, &b);
printf("Enter intger and floating point numbers: ");
/*Integer and floating point number can be taken at once from user as below */ 
scanf("%d%f', &a, &c); 
return 0;
}
Similarly, any number of inputs can be taken at once from user.
EXERCISE:
1 . To print out a and b given below, which of the following printf() statement will you use?
# include <stdio.h> 
float a=3.14; 
double b=3.14;
A. printf("%f %lf", a, b);
B. printf("%Lf %f", a, b);
C. printf("%Lf %Lf", a, b);
D. printf("%f %Lf", a, b);
2. To scan a and b given below, which of the following scanf() statement will you use?
# include <stdio.h> 
float a; 
double b;
A. scanf("%f %f", &a, &b);
B. scanf("%Lf %Lf", &a, &b);
C. scanf("%f %Lf", &a, &b);
D. scanf("%f %lf", &a, &b);
3. For a typical program, the input is taken using.
A. scanf
B. Files
C. Command-line
D. None of the mentioned
Page 3


FORMATTED INPUT-OUTPUT
Data can be entered & displayed in a particular format. Through format specifications, better 
presentation of results can be obtained.
Variations in Output for integer & floats:
# include <stdio.h> 
int main()
{
printf("Case 1:%6d\n",9876);
/* Prints the number right justified within 6 columns */ 
printf("Case 2:%3d\n",9876);
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not 
right justified */
printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */ 
printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */ 
printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation (scientific notation) */ 
return 0;
}
Output
Case 1: 9876 
Case 2:9876 
Case 3:987.65 
Case 4:988
Case 5:9.876543e+002
Variations in Input for integer and floats:
#include <stdio.h> 
int main()
{
int a,b; 
float c,d;
printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below */
scanf("%d%d", &a, &b);
printf("Enter intger and floating point numbers: ");
/*Integer and floating point number can be taken at once from user as below */ 
scanf("%d%f', &a, &c); 
return 0;
}
Similarly, any number of inputs can be taken at once from user.
EXERCISE:
1 . To print out a and b given below, which of the following printf() statement will you use?
# include <stdio.h> 
float a=3.14; 
double b=3.14;
A. printf("%f %lf", a, b);
B. printf("%Lf %f", a, b);
C. printf("%Lf %Lf", a, b);
D. printf("%f %Lf", a, b);
2. To scan a and b given below, which of the following scanf() statement will you use?
# include <stdio.h> 
float a; 
double b;
A. scanf("%f %f", &a, &b);
B. scanf("%Lf %Lf", &a, &b);
C. scanf("%f %Lf", &a, &b);
D. scanf("%f %lf", &a, &b);
3. For a typical program, the input is taken using.
A. scanf
B. Files
C. Command-line
D. None of the mentioned
4. What is the output of this C code?
#include <stdio.h> 
int main()
{ int i = 10, j = 2;
printf("%d\n", printf("%d %d " , i, j));
}
A. Compile time error
B. 10 2 4
C. 10 2 2
D. 10 2 5
5. What is the output of this C code?
#include <stdio.h> 
int main()
{
int i = 10, j = 3; 
printf("%d %d %d", i, j);
}
A. Compile time error
B. 10 3
C. 10 3 some garbage value
D. Undefined behavior
6. What is the output of this C code?
#include <stdio.h> 
int main()
{ int i = 10, j = 3, k = 3; 
printf("%d %d " , i, j, k);
}
A. Compile time error
B. 10 3 3
C. 10 3
D. 10 3 somegarbage value
Page 4


FORMATTED INPUT-OUTPUT
Data can be entered & displayed in a particular format. Through format specifications, better 
presentation of results can be obtained.
Variations in Output for integer & floats:
# include <stdio.h> 
int main()
{
printf("Case 1:%6d\n",9876);
/* Prints the number right justified within 6 columns */ 
printf("Case 2:%3d\n",9876);
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not 
right justified */
printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */ 
printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */ 
printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation (scientific notation) */ 
return 0;
}
Output
Case 1: 9876 
Case 2:9876 
Case 3:987.65 
Case 4:988
Case 5:9.876543e+002
Variations in Input for integer and floats:
#include <stdio.h> 
int main()
{
int a,b; 
float c,d;
printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below */
scanf("%d%d", &a, &b);
printf("Enter intger and floating point numbers: ");
/*Integer and floating point number can be taken at once from user as below */ 
scanf("%d%f', &a, &c); 
return 0;
}
Similarly, any number of inputs can be taken at once from user.
EXERCISE:
1 . To print out a and b given below, which of the following printf() statement will you use?
# include <stdio.h> 
float a=3.14; 
double b=3.14;
A. printf("%f %lf", a, b);
B. printf("%Lf %f", a, b);
C. printf("%Lf %Lf", a, b);
D. printf("%f %Lf", a, b);
2. To scan a and b given below, which of the following scanf() statement will you use?
# include <stdio.h> 
float a; 
double b;
A. scanf("%f %f", &a, &b);
B. scanf("%Lf %Lf", &a, &b);
C. scanf("%f %Lf", &a, &b);
D. scanf("%f %lf", &a, &b);
3. For a typical program, the input is taken using.
A. scanf
B. Files
C. Command-line
D. None of the mentioned
4. What is the output of this C code?
#include <stdio.h> 
int main()
{ int i = 10, j = 2;
printf("%d\n", printf("%d %d " , i, j));
}
A. Compile time error
B. 10 2 4
C. 10 2 2
D. 10 2 5
5. What is the output of this C code?
#include <stdio.h> 
int main()
{
int i = 10, j = 3; 
printf("%d %d %d", i, j);
}
A. Compile time error
B. 10 3
C. 10 3 some garbage value
D. Undefined behavior
6. What is the output of this C code?
#include <stdio.h> 
int main()
{ int i = 10, j = 3, k = 3; 
printf("%d %d " , i, j, k);
}
A. Compile time error
B. 10 3 3
C. 10 3
D. 10 3 somegarbage value
7. The syntax to print a % using printf statement can be done by.
A. %
B. %
C.
D. %%
8. What is the output of this C code?
#include <stdio.h> 
int main()
{ int n;
scanf("%d", n); 
printf("%d\n", n); 
return 0;
}
A. Compilation error
B. Undefined behavior
C. Whatever user types
D. Depends on the standard
9. What is the output of this C code?
#include <stdio.h> 
int main()
{ short int i; 
scanf("%hd", &i); 
printf("%hd", i); 
return 0;
}
A. Compilation error
B. Undefined behavior
C. Whatever user types
D. None of the mentioned
10. In a call to printf() function the format specifier %b can be used to print binary equivalent of 
an integer.
A. True
B. False
Page 5


FORMATTED INPUT-OUTPUT
Data can be entered & displayed in a particular format. Through format specifications, better 
presentation of results can be obtained.
Variations in Output for integer & floats:
# include <stdio.h> 
int main()
{
printf("Case 1:%6d\n",9876);
/* Prints the number right justified within 6 columns */ 
printf("Case 2:%3d\n",9876);
/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not 
right justified */
printf("Case 3:%.2f\n",987.6543);
/* Prints the number rounded to two decimal places */ 
printf("Case 4:%.f\n",987.6543);
/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */ 
printf("Case 5:%e\n",987.6543);
/* Prints the number in exponential notation (scientific notation) */ 
return 0;
}
Output
Case 1: 9876 
Case 2:9876 
Case 3:987.65 
Case 4:988
Case 5:9.876543e+002
Variations in Input for integer and floats:
#include <stdio.h> 
int main()
{
int a,b; 
float c,d;
printf("Enter two intgers: ");
/*Two integers can be taken from user at once as below */
scanf("%d%d", &a, &b);
printf("Enter intger and floating point numbers: ");
/*Integer and floating point number can be taken at once from user as below */ 
scanf("%d%f', &a, &c); 
return 0;
}
Similarly, any number of inputs can be taken at once from user.
EXERCISE:
1 . To print out a and b given below, which of the following printf() statement will you use?
# include <stdio.h> 
float a=3.14; 
double b=3.14;
A. printf("%f %lf", a, b);
B. printf("%Lf %f", a, b);
C. printf("%Lf %Lf", a, b);
D. printf("%f %Lf", a, b);
2. To scan a and b given below, which of the following scanf() statement will you use?
# include <stdio.h> 
float a; 
double b;
A. scanf("%f %f", &a, &b);
B. scanf("%Lf %Lf", &a, &b);
C. scanf("%f %Lf", &a, &b);
D. scanf("%f %lf", &a, &b);
3. For a typical program, the input is taken using.
A. scanf
B. Files
C. Command-line
D. None of the mentioned
4. What is the output of this C code?
#include <stdio.h> 
int main()
{ int i = 10, j = 2;
printf("%d\n", printf("%d %d " , i, j));
}
A. Compile time error
B. 10 2 4
C. 10 2 2
D. 10 2 5
5. What is the output of this C code?
#include <stdio.h> 
int main()
{
int i = 10, j = 3; 
printf("%d %d %d", i, j);
}
A. Compile time error
B. 10 3
C. 10 3 some garbage value
D. Undefined behavior
6. What is the output of this C code?
#include <stdio.h> 
int main()
{ int i = 10, j = 3, k = 3; 
printf("%d %d " , i, j, k);
}
A. Compile time error
B. 10 3 3
C. 10 3
D. 10 3 somegarbage value
7. The syntax to print a % using printf statement can be done by.
A. %
B. %
C.
D. %%
8. What is the output of this C code?
#include <stdio.h> 
int main()
{ int n;
scanf("%d", n); 
printf("%d\n", n); 
return 0;
}
A. Compilation error
B. Undefined behavior
C. Whatever user types
D. Depends on the standard
9. What is the output of this C code?
#include <stdio.h> 
int main()
{ short int i; 
scanf("%hd", &i); 
printf("%hd", i); 
return 0;
}
A. Compilation error
B. Undefined behavior
C. Whatever user types
D. None of the mentioned
10. In a call to printf() function the format specifier %b can be used to print binary equivalent of 
an integer.
A. True
B. False
# include <stdio.h> 
int main()
{
char ch; 
int i;
scanf("%c", &i); 
scanf("%d", &ch); 
printf("%c %d", ch, i); 
return 0;
}
A. Error: suspicious char to in conversion in scanf()
B. Error: we may not get input for second scanf() statement
C. No error
D. None of above
11. Point out the error in the program?
12. Which of the following is NOT a delimiter for an input in scanf?
A. Enter
B. Space
C. Tab
D. None of the mentioned
Read More
90 docs

Top Courses for Computer Science Engineering (CSE)

Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

ppt

,

practice quizzes

,

shortcuts and tricks

,

Important questions

,

Summary

,

Extra Questions

,

Objective type Questions

,

video lectures

,

Short Notes: Formatted Input-Output | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

pdf

,

MCQs

,

Semester Notes

,

Short Notes: Formatted Input-Output | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Free

,

study material

,

Short Notes: Formatted Input-Output | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Sample Paper

,

Viva Questions

,

mock tests for examination

,

Previous Year Questions with Solutions

,

past year papers

;