Formatted Input and Output in C Programming Language | Learn C | Village Programmer




This is the sixth article of the series Learning C Programming Language

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:
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:
Enter two integers: 10
5
Enter integer and floating point number :
10 23.65

Similarly, any number of inputs can be taken at once from user.

🤼‍♂️Train Your Brain (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

11. Point out the error in the program?
#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

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

Solutions : 

1. A. printf("%f %lf", a, b); 
2. D. scanf("%f %lf", &a, &b);
3. B. scanf
4. D. 10 2 5 // printf() returns number of characters printed. Here 5 (1,0,space,2,space)
5. C. 10 3 some garbage value 
6. C. 10 3 
7. D. %%
8. C. Whatever user types
9. C. Whatever user types 
10. B. False
11. B. Error: we may not get input for second scanf() statement
12. D. None of the mentioned


That's All. 😅

Thanks for reading. 🙏
If you liked it feel free to share with your dear ones.💗
And tell me what do you think in the comment box.💬
Stay tuned with Village Programmer.


Post a Comment

Previous Post Next Post