#include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { char hello[5]; hello [0] = 'H'; hello [1] = 'e'; hello [2] = 'l'; hello [3] = 'l'; hello [4] = 'o'; char world[5]; world [0] = 'W'; world [1] = 'o'; world [2] = 'r'; world [3] = 'l'; world [4] = 'd'; printf ("%s %s!\n", hello, world); return EXIT_SUCCESS; } When I run the above code I get:
Hello WorldHello! Can someone please explain why my output is either repeating words or getting weird numbers and letters being printed? Is it because I haven't included a '\0'?
printf()and told it that it was given two strings. Anything could have happened. You were lucky that what did happen was basically benign. You might care to meditate on what you see if you reverse the order of defininghelloandworld.