This program tries to print Hello World, but something not quite right comes out.


#include <stdio.h>

int main()

{

    printf("%s\n" 

            "Hello World");

    return (0);

}



Source code

Hint 1: "%s\n" is closely related to "Hello World"

Hint 2: The relationship is not what it first appears.

Hint 3: %s is not related to "Hello World"

Hint 4: Hint 3 is not a contradiction. There's a reason that the quotes disappeared.

Hint 5: The program prints garbage followed by "Hello World".

Answer: The problem is that there is no comma between "%s\n" and "Hello World":

printf("%s\n" "Hello World");

This is the same as:

printf("%s\n" "Hello World");
or
printf("%s\nHello World");

So printf sees the %s, goes to the second parameter (there is none, so it makes one up) and prints it. It then prints the rest of the format.

Main gallery