在变量自己的声明中将变量分配给自身

发布于 2024-09-09 03:26:22 字数 353 浏览 5 评论 0原文

#include <stdio.h>
int main() {
    int c = c;
    printf("c is %i\n", c);
    return 0;
}

我正在定义一个名为 c 的整数变量,并将其值分配给自身。但这怎么能编译呢? c 还没有初始化,那么如何将它的值赋给自己呢?当我运行该程序时,我得到c is 0

我假设编译器正在生成为 c 变量分配空间的汇编代码(当编译器遇到 int c 语句时)。然后,它获取未初始化空间中的任何垃圾值并将其分配回c。这是正在发生的事情吗?

#include <stdio.h>
int main() {
    int c = c;
    printf("c is %i\n", c);
    return 0;
}

I'm defining an integer variable called c, and I'm assigning its value to itself. But how can this even compile? c hasn't been initialized, so how can its value be assigned to itself? When I run the program, I get c is 0.

I am assuming that the compiler is generating assembly code that is assigning space for the the c variable (when the compiler encounters the int c statement). Then it takes whatever junk value is in that un-initialized space and assigns it back to c. Is this what's happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终弃我 2024-09-16 03:26:22

我记得在之前的回答中引用过这一点,但现在找不到。

C++03 §3.3.1/1:

名称的声明点紧接在其完整声明符(第 8 条)之后和其初始值设定项(如果有)之前,...

因此变量 c 即使在初始化程序部分之前也是可用的。

编辑:抱歉,您专门询问了有关 C 的问题;虽然我确信那里有一条等效的线。詹姆斯·麦克内利斯发现了它:

C99 §6.2.1/7:任何不是结构、联合或枚举标记的标识符“其范围在其声明符完成后开始”。声明符后面跟着初始化程序。

I remember quoting this in a previous answer but I can't find it at the moment.

C++03 §3.3.1/1:

The point of declaration for a name is immediately after its complete declarator (clause 8) and before its initializer (if any), ...

Therefore the variable c is usable even before the initializer part.

Edit: Sorry, you asked about C specifically; though I'm sure there is an equivalent line in there. James McNellis found it:

C99 §6.2.1/7: Any identifier that is not a structure, union, or enumeration tag "has scope that begins just after the completion of its declarator." The declarator is followed by the initializer.

嗫嚅 2024-09-16 03:26:22

你的猜测完全正确。 int c 将变量的空间压入堆栈,然后读取该变量并将其重写为 c = c 部分(尽管编译器可能会对其进行优化) 。您的编译器将值推送为 0,但并不保证始终如此。

Your guess is exactly right. int c pushes space onto the stack for the variable, which is then read from and re-written to for the c = c part (although the compiler may optimize that out). Your compiler is pushing the value on as 0, but this isn't guaranteed to always be the case.

夜血缘 2024-09-16 03:26:22

使用未初始化的值是未定义的行为(§C99 J.2“具有自动存储持续时间的对象的值在其被使用时被使用”
不确定”)。所以从 鼻恶魔 到 c = 任何事情都可能发生0,到玩 Nethack

It's undefined behavior to use an uninitialized value (§C99 J.2 "The value of an object with automatic storage duration is used while it is
indeterminate"). So anything can happen from nasal demons to c = 0, to playing Nethack.

旧竹 2024-09-16 03:26:22

C 规范不保证变量将被初始化为 0、0.0 或“”或“”。

这是编译器的一个功能,您永远不必强迫它发生。

我总是将我的 IDE/编译器设置为对此发出警告。

The C specification don't assure that variables will be initialized to 0, 0.0 nor "" or ''.

That is a feature of compilers and never you must thrust that will happen.

I always set my IDE/Compiler to warning about that.

夏末染殇 2024-09-16 03:26:22

c已经初始化了!

虽然这是一行代码,但实际上是先初始化c,然后给c赋值。你很幸运,编译器为你将 c 初始化为零。

c has been initialized!

Although this is one line of code, it is in fact initializing c first, then assigning c to it. You are just lucky that the compiler is initializing c to zero for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文