+ 6

What's the output of code ?

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

27th Jan 2022, 9:11 AM
SIMRAN
SIMRAN - avatar
13 Antworten
+ 6
printf function returns the number of characters it printed on the console. edit: SIMRAN Try #include <stdio.h> int main() { printf("\n%d", printf("Sololearn")) ; return 0; } output: Sololearn 9 see output that, this first prints Sololearn on console because of printf("Sololearn"), and returns 9 as it printed 9 characters so %d cause to print 9
27th Jan 2022, 3:13 PM
Jayakrishna 🇮🇳
+ 4
Thats gonna return Hello5, Hello for the print 5 for the number of characters it printed, As for your question how it counts? Let me demonstrate you by a pseudo code, (Note:This my own understanding) Func printf(string) Int counter ->0; for char x : string { Print_to_stdout(x); counter++; } Return counter; Basically, it breaks the received String into char array, then prints one by one into the Standard Output stream, and every time it does that, it keeps track of number of time it printed through a counter variable
28th Jan 2022, 5:50 PM
Shantanu Bindhani
Shantanu Bindhani - avatar
+ 1
G'day SIMRAN as ANONYMOUS said, output is Hello5. Your code is counting the characters in "Hello" to be 5. Try a different length string & see what happens.
27th Jan 2022, 10:55 AM
HungryTradie
HungryTradie - avatar
+ 1
HungryTradie Can you please explain about it briefly ? How it's counting the characters 😅
27th Jan 2022, 2:49 PM
SIMRAN
SIMRAN - avatar
+ 1
Hello5 //word+no.of letter //as 20(can be either other no.to eg:19,21 or non eg:0) doesn't make any change
28th Jan 2022, 7:07 AM
Nabin Pariyar
0
Hello5
27th Jan 2022, 9:25 AM
JOKER
JOKER - avatar
- 1
Yes jayakrishna, you are right
27th Jan 2022, 4:18 PM
Mit Darji
- 1
Ouput is Hello5
29th Jan 2022, 2:02 AM
Shreya Sinha
- 1
Hello5 the string length is 5 so it'll print 5 rather than 20
29th Jan 2022, 3:44 AM
Nathnael Tekabe
Nathnael Tekabe - avatar
- 2
Hello5
27th Jan 2022, 6:45 PM
Mohammad Jawwad
Mohammad Jawwad - avatar
- 2
Hello5
27th Jan 2022, 8:10 PM
Mit Darji
- 2
It outputs hello5
29th Jan 2022, 12:32 AM
Dennis Kimathi
Dennis Kimathi - avatar
- 2
Output hello 5
29th Jan 2022, 1:42 AM
Abhimanyu Kumar Gond
Abhimanyu Kumar Gond - avatar