URLs in C

By Susam Pal on 03 Jun 2011

Here is an interesting C puzzle I created recently. It is one of those silly puzzles that may bring a smile on your face if you aren't able to figure it out immediately.

#include <stdio.h>

int main()
    printf("hello, world\n");
    return 0;

This code compiles and runs successfully even though the C99 standard doesn't mention anywhere that URLs are valid syntactic elements in C. How does this code work then?

Update on 02 Oct 2011: The puzzle has been solved in the comments section. If you want to think about the problem before you see the solutions, this is a good time to pause and think about it. There are spoilers ahead.

The code works fine because http: is a label and // following it begins a comment. In case, you are wondering if // is indeed a valid comment in C, yes, it is, since C99. Download the C99 standard and go to section 6.4.9 (Comments) and read the second point which mentions this:

Except within a character constant, a string literal, or a comment, the characters // introduce a comment that includes all multibyte characters up to, but not including, the next new-line character.The contents of such a comment are examined only to identify multibyte characters and to find the terminating new-line character.