C Programming Questions and Answers – Formatted Input – 1

«
»
This section on C test focuses on “Formatted Input”. One shall practice these test questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C test questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C test questions on “Formatted Input” along with answers, explanations and/or solutions:

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

advertisement
  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?

  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?

advertisement
  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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter