+ 3

Why does it output two array?

#include <stdio.h> using namespace std; int main() { int i,j,l; char a[14]={'c','e','f','h','i','k','n','o','w','x'}; char s[4]={'f','b','l','a'}; puts(s); return 0; } It outputs " fblacefhiknowx", and I know my array s doesnt have '\0' at the end, but why array a is output? https://code.sololearn.com/cEGd33GC79s6/?ref=app

15th May 2017, 8:06 AM
Alion Lee
Alion Lee - avatar
6 Respuestas
+ 3
solution of you problem. change char s[4] char s[5] or any number which is greater than 4. ex char s[10] Reason: the puts method print the element till it find \0. so after printing 'fbla' it is printing any Garbage value if you remove thr char a[14] it will give you some other Garbage value. on increasing the size from 4 to any number bigger than 4. is add \0 to the 5 place
15th May 2017, 9:28 AM
MR Programmer
MR Programmer - avatar
+ 3
K plz wait for a sec or two...
15th May 2017, 8:17 AM
Vishnu ks
Vishnu ks - avatar
+ 2
Did you check the answer properly. I don't think that will be the output Since you are doing puts for only one array only that array will be printed.
15th May 2017, 8:09 AM
Vishnu ks
Vishnu ks - avatar
+ 2
Yeah, I checked, but it printed two arrays. You can run this code. https://code.sololearn.com/cEGd33GC79s6/?ref=app
15th May 2017, 8:16 AM
Alion Lee
Alion Lee - avatar
+ 2
thx, bro 😄
15th May 2017, 11:58 AM
Alion Lee
Alion Lee - avatar