C Questions and Answers – scanf – 1

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “scanf – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. The syntax of the scanf() is scanf(“control string “, arg1,arg2,arg3,….,argn); the prototype of control string is ____________
a) [=%[width][modifiers]type=]
b) [=%[modifiers][width]type=]
c) [=%[width] [modifiers]]
d) [width][modifiers]
View Answer

Answer: a
Explanation: scanf() starts with the symbol % followed by the width, modifier, type of the argument.

2. What is the use of symbol * in the control string as shown [=%[*][width] [modifiers] type=]?
a) * is optional and used when the data should be read from the stream but ignored
b) * is not optional, used to read data from the stream but it is not ignored
c) * is not optional, it is used to read data stream but ignored
d) * is optional and used to read data from stream but it is not ignored
View Answer

Answer: a
Explanation: * is an optional argument, it indicates that data should be read from the steam but ignored (not stored in a memory location)
advertisement
advertisement

3. What action is carried out by scanf if a user enters any blank spaces, tabs, and newlines?
a) consider as input
b) ignores it
c) produces error
d) nothing can be said
View Answer

Answer: b
Explanation: The scanf() function ignores any blank spaces, tabs, and newlines entered by the user. This scanf() function just returns the number of input fields successfully scanned and stored.

4. What error will generate if the read and write parameters are not separated by commas?
a) run-time error
b) compile error
c) logical error
d) no error
View Answer

Answer: b
Explanation: A compile error will be generated if the read and write parameters are not separated by commas.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

5. What will be the output of the following C code?

char str[] =”Good”;
scanf(%s”, str);

a) compile error
b) run-time error
c) good
d) logical error
View Answer

Answer: c
Explanation: String can be read from the stream without the use of address of operator (&).
advertisement

6. What will be the output of the following C code?

advertisement
scanf(%d %d %d”,&n1,&n2);

a) read data for two
b) generate error
c) read data for three
d) nothing can be said
View Answer

Answer: b
Explanation: The following scanf() statement will generate an error as no variable address is given for the third conversion specification.

7. What form the data must be entered for the given C code?

scanf(%d / %d”, &n1,&n2);

a) 6  9
b) 6/9
c) compile error
d) run-time error
View Answer

Answer: b
Explanation: The slash in the control String are neither white space characters nor a part of conversion specification, so the user must enter data of the form 6/9.

8. A fatal error will be generated if the format string is ended with a white space character.
a) true
b) false
View Answer

Answer: a
Explanation: An error will be generated if the format string %s is ended with white space character.

9. Explain the format string "%5d%s %c"
a) five characters as a decimal integer, then reads the remaining as a string and then scans the first non-whitespace character
b) compile error
c) run-time error
d) read first five characters as a decimal and ignore the rest
View Answer

Answer: a
Explanation: The above format string reads the first five characters as a decimal integer, then reads the remaining as a string until a space, newline or tab is found, then reads the first non-whitespace character.

10. _____ is an optional argument that gives the maximum number of characters to be read.
a) modifiers
b) width
c) precision
d) length
View Answer

Answer: b
Explanation: Width is the argument that gives the maximum number of characters to be read. Few characters will be read if the scanf function encounters white space and it will stop processing further.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.