C Programming Questions and Answers – Formatted Input – 1

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

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

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int n;
  5.         scanf("%d", n);
  6.         printf("%d\n", n);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Whatever user types
d) Depends on the standard
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *n;
  5.         scanf("%s", n);
  6.         return 0;
  7.     }

a) Compilation error
b) Undefined behavior
c) Nothing
d) None of the mentioned
View Answer

Answer: b
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char n[] = "hello\nworld!";
  5.         char s[13];
  6.         sscanf(n, "%s", s);
  7.         printf("%s\n", s);
  8.         return 0;
  9.     }

a) hellonworld!
b)

hello
world!
advertisement

c) hello
d) hello world!
View Answer

Answer: c
Explanation: The array n contains a string which has a newline character in between the strings “hello” and “world”. A newline character is considered as a whitespace character for inputs for the scanf(), sscanf() and fscanf() functions. So, the sscanf() function will only copy upto the string “hello” into the array s. Hence, the output of the printf() function be only the string “hello”.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Whatever user types
d) None of the mentioned
View Answer

Answer: c
Explanation: None.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
View Answer

Answer: b
Explanation: None.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
View Answer

Answer: b
Explanation: None.

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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%h*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Somegarbage value
d) Depends on the standard.
View Answer

Answer: a
Explanation: None.

8. Which of the following is NOT a delimiter for an input in scanf?
a) Enter
b) Space
c) Tab
d) None of the mentioned
View Answer

Answer: d
Explanation: None.

9. If the conversion characters of int d, i, o, u and x are preceded by h, it indicates?
a) A pointer to int
b) A pointer to short
c) A pointer to long
d) A pointer to char
View Answer

Answer: b
Explanation: None.

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.

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.