printing char array

my print output is "helloworld" instead of "hello" , why ?

1
2
3
4
5
6
7
8
9
  int main()
{
        char a[5] = "hello";
        char b[5] = "world" ;
        char p[5];
        char *c ;
        char *k = "ragav";
         printf("%s\n",a);
}
Nope. Your print output is "hello"

printf("%s\n",a) See? You only output "a", dont see the b anywhere.

printf("%s%s\n",a,b)

Edit: Also, Change the arrays to [6] not 5. Because you have to leave place for a "\0".
Last edited on
Actually, since hello and world are stored just one after the other with no space for the null terminator, the behavior is undefined.
Your program could reformat your hard drive, send a rocket into space(provided you have the necessary hardware options) or simply print "helloworld" due to stack overflowing.
Imagen if his code was the first thing to land on pluto.
Topic archived. No new replies allowed.