Printf() Quiz in C Set-4

Printf() Quiz in C Set-4 | This quiz is on String length using printf() and printf() inside printf() in C Programming language. The printf() function in C programming language is used for output formatting. It is used to display information required by the user and also prints the value of the variables. It formats the output, like the width of the output, the sign of the output e.t.c

In C, there are many predefined library functions are available for input and output operations. These functions are collectively called standard input-output library functions, and all these functions are available in stdio.h header file.

The “stdio.h” is an abbreviation of the standard input output header file. So, in C language, any program which uses input or output operations must contain the statement #include<stdio.h>

Note:- We can write printf() inside printf(). Execution will be inside to the outside of printf(). First inside printf() will executed and then outside printf(). The printing order will be right to left. Recommended Reading:- Output Formatting Using Printf() Function in C Language. Now let us find the output of the programs given in the Quiz on printf() function in C set-4

Printf() Quiz in C

Q1) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int a = printf("Hello");
  printf("\t %d",a);
  return 0;
}

a) Hello 6
b) error
c) Hello 5
d) Garbage value


Ans:- c) Hello 5

The printf() returns the length(integer) of string. In string ‘\0’ is not counted as a character. Here “Hello” has 5 characters so printf display Hello to the screen and returns 5 to the variable a.


Q2) Find the output of the given C program.

#include<stdio.h>
int main()
{
  int n=9988;
  printf("%d", printf("%d", printf("%d", n) ) );
  return 0;
}

a) 998899889988
b) 9988
c) 998841
d) Compile time error


Ans:- c) 998841

Innermost printf(“%d”, 9988) display 9988 returns the number of digits (in this case 4 due to 9988) to the second inner printf. Now,
printf(“%d”, printf(“%d”, 4));
Second inner printf display 4 and return number of digits (in this case 1 for 4) to the outer printf. Now,
printf(“%d”, 1);
The final output is:- 998841


Q3) Can we find string length without using a loop or strlen()?

a) No
b) Yes


Ans:- b) Yes

Using printf() function we can find the length of any string without using loop or strlen().


Q4) Find the output of the given C program.

#include<stdio.h>
int main()
{
  printf("I miss you", printf("buddy "));
  return 0;
}

a) I miss you
b) buddy
c) buddy I miss you
d) I miss you buddy


Ans:- c) buddy I miss you

buddy I miss you; The innermost print() function, printf(“buddy “) will execute and display “buddy “. Now,
printf(“I miss you”, 5);
The next printf() will execute and print “I miss you”.


Q5) Find the output of the given C program.

#include<stdio.h>
int main()
{
  printf("\t Spiderman-%d",printf("Coming"));
  return 0;
}

a) Coming
b) Coming Spiderman-5
c) Coming Spiderman-7
d) Coming Spiderman-6


Ans:- d) Coming Spiderman-6

printf(“Coming”) display Coming and it has 6 charaters. Outer printf() display this 6 using conversion specifier %d.


Q6) Find the output of the given C program.

#include<stdio.h>
int main()
{
  printf("John Wick-%d",printf("The Lion King\t"));
  return 0;
}

a) The Lion King John Wick-11
b) The Lion King John Wick-14
c) The Lion King John Wick-15
d) The Lion King John Wick-13


Ans:- b) The Lion King John Wick-14

printf(“The Lion King\t”) display “The Lion King” and it has 14 characters, blank space is a special character and ‘\t’ is an escape character.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *