请提供需要翻译的“URL in C Puzzle”的具体内容。 我需要文本才能进行翻译。
URL in C Puzzle

原始链接: https://susam.net/url-in-c.html

这段C代码片段,似乎在`printf`语句中包含了一个URL(`https://susam.net/`),但令人惊讶的是,它能够成功编译和运行。其中的技巧在于C99标准对代码的解读方式。 `https://`部分被视为一个标签,随后的`//`则启动了一个注释。由于C99标准将`//`识别为有效的注释起始符(如标准6.4.9节中所定义),因此从它之后直到换行符的所有内容都会被编译器忽略。 因此,`printf`函数实际上只接收到`"hello, world\n"`作为其参数,从而解释了成功的执行和输出。URL本质上是一个无害的、被忽略的标签和注释的开始。

这个Hacker News讨论围绕着在代码注释中嵌入意外内容,特别是URL和shell命令。一位用户分享了一个同事意外将YouTube URL提交到Java代码库中的故事——这是一个有趣的发现,暴露了他们的观看习惯。 另一位用户故意在C/C++程序中使用这种技术,将简单的编译和运行命令嵌入到注释块(`#if`和`#endif`)中,设计为在他们的文本编辑器中按下特定键时执行。他们通过包含“exit”语句来确保代码不会作为命令运行。 最后一位评论者提倡只使用`/* */`样式的注释,以避免这种意外执行的可能性。该讨论强调了代码注释中可能存在的意外或有意隐藏的功能。
相关文章

原文
URL in C - Susam Pal

By Susam Pal on 03 Jun 2011

Here is a silly little C puzzle:

#include <stdio.h>

int main(void)
{
    https://susam.net/
    printf("hello, world\n");
    return 0;
}

This code compiles and runs successfully.

$ c99 hello.c && ./a.out
hello, world

However, the C99 standard draft does not mention anywhere that a URL is a valid syntactic element in C. How does this code work then?

Update on 04 Jun 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 https: 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 draft, 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.
联系我们 contact @ memedata.com