11

Why does the following code compile? Which section of the language allows URLs to be added in C and C++ code?

int main()
{
     http://www.stackoverflow.com
     return 0;
}

Thanks in advance, Castro.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 2
    I went ahead and properly formatted your question using the code formatting, instead of block quote - that makes it pretty obvious, as Daniel said. – Cascabel Mar 21 '11 at 05:22
  • http://stackoverflow.com/questions/75538/hidden-features-of-c/78840#78840 – Inverse Mar 26 '11 at 06:53

3 Answers3

21

If you compiled with warnings, you would notice:

warning: label ‘http’ defined but not used

That should be indicative enough of the problem here.

The http: text is treated as a label.

Followed by // negating the remaining text as a comment, ignoring it.

http://www.stackoverflow.com

Even the SO syntax colour schemes indicated as above show this to be true, as the section after the http, is treated as a comment (grayed out).

hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
3

It's because the compiler treats http: as a label and // whatever as a comment. This is perfectly legal code.

Unless you use goto http; somewhere however, it'll be completely useless code.

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

In your code http is just a label and //www.stackoverflow.com is a comment.

Also note that

int main()
{
     http://www.stackoverflow.com
}

or

int main()
{
 http://www.stackoverflow.com
 http://www.facebook.com
 return 0;
}

won't compile.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345