in Programming in C
1,357 views
0 votes
0 votes
WHAT IS THE OUTPUT OF THE FOLLOWING CODE?

 

printf("%d",printf("ABC"));

IS IT ABC3
in Programming in C
1.4k views

11 Comments

$\text{YES, because inner printf prints ABC and returns how many characters it has successfully}$

$\text{ printed which gets printed by outside printf}$
2
2

if it is 

printf("%d",scanf("ABC"));

then what will be output?

0
0

@srestha I THINK IT WILL SHOW ERROR AS WE DONT KNOW WHAT WE ARE READING?

FROM MY VIEW

0
0

@srestha It should be undefined behavior maybe?

0
0

why undefined?

scanf: On success, the function returns the number of items successfully read.

So, will be return 1 

0
0

@srestha Well actually it does not return 1. Here we are not providing scanf() with any address to insert value into. No items are successfully read, Hence the value printed is -1.

RETURN VALUE

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.

1
1
Is it not reading "ABC" successfully?
0
0

@srestha IN SCANF WE DONT KNOW WHAT WE GOT THERE AS WE DONT HAVE ANY FORMAT SPECIFIERS LIKE %s OR %d .

SO HOW CAN WE SAY THAT WE ARE READING STRING OR CHARACTER

0
0

No,read the syntax of scanf(), it is

int scanf(const char *format, ...);

and it's first argument is of const char* type i.e scanf() expects 1st argument as of const char* and 1st argument provided is "ABC" which is of char* type though you should provide some format specifier like %s etc.

0
0
EOF is returned since there is a matching failure.
0
0

but %s also not giving correct output. why?

https://ideone.com/RAGKnS

1
1

1 Answer

3 votes
3 votes
Best answer
The return value of printf is the number of characters it printed.

printf("%d",printf("ABC"));

To execute the outer printf it needs to execute the inner printf to obtain the second parameter.

Hence output is ABC3
selected by

4 Comments

$\text{It will not print anything as we have not specified any format specifier}$
0
0
@!KARAN it prints return value of scanf
0
0
printf("%s",scanf("ABC"));
printf("%c",scanf("ABC"));

@srestha try this!!

0
0