Why it prints 3 instead of 6 and also with space? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why it prints 3 instead of 6 and also with space?

void main() { printf("ABC %d",printf("ABC")); }

27th Aug 2019, 3:50 AM
Chetan Mude
Chetan Mude - avatar
2 Answers
+ 2
Chetan Mude for inner printf , you are asking to store value of function return as %d of outer printf... hence, first inner printf ABC is printed and that function returns size of characters which is 3... then outer printf prints abc and that size we are not capturing anywhere to print.. it's like below example: int add(int a,int b) {return a+b;} int main () { int x = add(3,4); add(5,7); return 0; } here , first function call result is stored in variable x but second function call is returning result but we are not storing it... hih
27th Aug 2019, 5:13 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
The inner printf() function is executed first, printing “ABC”. Moreover it returns the length of the string to the outer printf() function, which is 3. Then the outer printf() function outputs “ABC 3”.
27th Aug 2019, 4:25 AM
Michael
Michael - avatar