Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does inserting URLs in C and C++ code work?

Tags:

c++

c

url

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.

like image 291
Renato de Castro Avatar asked Mar 21 '11 05:03

Renato de Castro


2 Answers

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).

like image 64
deceleratedcaviar Avatar answered Sep 27 '22 21:09

deceleratedcaviar


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.

like image 21
Mark B Avatar answered Sep 27 '22 22:09

Mark B