645 views
2 votes
2 votes
// C/C++ program to demonstrate return value
// of printf()

#include <stdio.h>
int main()
{
    long int n = 123456789;

    printf("While printing ");
    printf(", the value returned by printf() is : %d",
                                    printf("%ld", n));

    return 0;
}

i think this code needs to print “while printing, the value reuturned by printf() is : 9 123456789”

but this prints “While printing 123456789, the value returned by printf() is : 9”

why this is happens ?

2 Answers

3 votes
3 votes

It first prints the first line “While printing” .

Then in the same line it first executing the inner most printf “printf("%ld", n).

So it prints the number “123456789”. 

Then the statement “, the value returned by printf() is :”  is printed.

We know printf return no of character it is printed and the inner printf returned 9 as no of character which print at the end due to the %d .

So the output is 

While printing 123456789, the value returned by printf() is : 9”.

 

1 votes
1 votes

There are 2 printf() functions in this code fragment:

inner printf()
outer printf()
Firstly, the inner printf() will execute and will print 123456789 and then the outer printf() will execute.

It is to be noted that the printf() returns the number of characters that it prints.
Thus, by executing the inner printf(), we have got 9 (because  123456789 are 9 characters), Now, when the outer printf() is executed, 9 is printed.

the inner call happned first it will print 9 number of character.

This answer is as per myknowledge , kindly Correct me if I am wrong.

Also Practice – C Programming GATE Questions

 

Related questions

0 votes
0 votes
1 answer
1
Isha Karn asked Dec 8, 2014
13,079 views
int x=5; void f() { x = x+50; } void g(h()) { int x=10; h(); print(x); } void main() { g(f()); print(x); }1) what is output if code uses deep binding?2)what is the output...
2 votes
2 votes
1 answer
2
Akhil01 asked Jul 17, 2016
452 views
#include int main() { int *p, a; p = &a; scanf("%d",p); printf("a = %d *p = %d", a, *p); }a = 32766 *p = 32766a = 32767 *p = 32767a = -1219137971 *p = -1219137971No Outpu...
3 votes
3 votes
2 answers
3
sabir asked Nov 24, 2015
872 views
Main () { int a [3] [4] = $\begin{pmatrix} 1&2&3&4 \\ 5&6&7&8 \\ 9&10&11&12 \\ \end{pmatrix}$ printf ("\n% u% u% u", a[0]+1, * (a[0] + 1), *(*(a + 0)+1)); }What is the o...
0 votes
0 votes
1 answer
4
shivani2010 asked Apr 21, 2016
289 views
#include<stdio.h int main() { char arr[10]; memset(arr,0,sizeof(arr)); gets(arr); printf("\n The buffer entered is [%s]\n",arr); return 0; }