Explain the output please....? | SoloLearn: Learn to code for FREE!

+8

Explain the output please....?

https://code.sololearn.com/c5kn96mglyYx/?ref=app

6/29/2020 4:41:56 PM

🐍Sizuna 🐉

11 Answers

New Answer

+8

Samanvitha P R yeah am getting same output

+8

Martin Taylor thanks for explanation i understood very well

+7

Samanvitha P R but why its printing @

+6

Samanvitha P R sorry i understood because size was less so its printing garbage now i increased the length then its printing properly but why %s not printing two times

+6

Dennis yes i understood but wht %s not printed two times

+6

Dennis ohh i understood thanks

+6

char x[]="%s Kritika"; printf (x,x); is the same as... printf("%s Kritika", "%s Kritika"); The first sting is the format specifier. The second string is an argument for the formatter to operate upon. The format specifier reads "print the string in the arguments, a space, then the word Kritika". Since the string in the arguments is "%s Kritika" it prints "%s Kritika" + " Kritika" which results in "%s Kritika Kritika".

+2

Because the first parameter of printf is a format specifier which replaces the formats "%s" in this case with the 2nd and so on parameters. ( even if you don't provide one, so be careful there ) The 2nd parameter and so on don't have this behaviour.

+1

Your character array is not big enough to include the null character so anything can happen when it tries to print it.