在 1 行中声明多个变量时在 C 中赋值
Value assignment in C while declaring multiple variable in 1 line
我正在玩单行声明和多个变量的初始化,并注意到 C 中的一些不寻常的东西
#include <stdio.h>
int main() {
int a, c, d, e = 0;
printf("a = %d, c = %d, d = %d, e = %d", a, c, d, e);
}
所以上面程序的输出是
a = 2961408, c = 0, d = 4200720, e = 0
容易,因为 e
被赋值 0
,其余的都可能是垃圾值 ,但 c 的值为 0。我将代码更改为
#include <stdio.h>
int main() {
int a, b, c, d, e = 0;
printf("a = %d, b = %d, c = %d, d = %d, e = %d", a, b, c, d, e);
}
这次输出是:
a = 2297856, b = 0, c = 4200736, d = 4200848, e = 0
再次将第二个变量初始化为 0
[此处 b
,之前 c
] 以及 e
,其余均为垃圾。
对于 3
、4
、5
、6
、7
等变量,每次第二次都注意到同样的事情声明中的变量被初始化为值 0
以及最后一个变量。
这不是最后一个变量初始化的情况,但是如果我在第二个变量之后分配值 0
的任何变量。我的第二个变量得到 0
作为它的初始化值。是我的编译器有问题还是 C 背后有一些逻辑?
#include <stdio.h>
int main() {
int a, b, c, d, e, g = 10, f;
printf("a = %d, b = %d, c = %d, d = %d, e = %d, g = %d, f = %d", a, b, c, d, e, g, f);
}
输出:
a = 3702784, b = 0, c = 4200752, d = 4200864, e = 6422400, g = 10, f = 4200752
[P.S。我的 C 编译器是 gcc (MinGW.org GCC Build-2) 9.2.0 ]
Is it something wrong with my compiler
没有
or is there some logic behind this in C?
没有
读取未初始化的本地值会给您不确定的值。这就是所谓的 undefined behavior - 您首先需要了解的是它的含义。 What is undefined behavior and how does it work? 正如我在那里写的那样:
It is important to investigate the cause of the undefined behavior - not so much the symptoms. Beginners often ask why a certain behavior occurred when they did something undefined, but there is often not much to learn from investigating the outcome. Time is better spent on learning what caused the undefined behavior.
现在这是未定义行为的原因实际上是因为您没有获取那些未初始化的局部变量的地址,请参阅此答案:(Why) is using an uninitialized variable undefined behavior?所以程序可能会崩溃。
撇开这个问题不谈,在许多系统上,不确定的值表现为打印恰好存储在 RAM 中某些随机位置的任何内容。 RAM 内存的工作方式,我们从不“删除”或“擦除”其中的任何内容。我们只是跟踪使用了哪些位置,然后当它们不再使用时,存储在那里的任何东西都会保留下来。
但是没有任何保证,因此优化编译器可能会打印一些随机 CPU 寄存器的内容。同样,从中学习没有任何意义。您不妨掷一些骰子,然后尝试找出您获得某些值的原因。
一些糟糕的编译器使用“调试构建”,其中整个 RAM 设置为零,因此在这样的系统上你会得到所有的零。这很糟糕,因为它会隐藏错误,直到您从调试切换到发布。
无论如何,从中学到的唯一重要的事情是 gcc/mingw 会抱怨未初始化的变量,以防您使用 -Wall
。作为初学者,养成编译的习惯:
gcc -std=c11 -pedantic-errors -Werror -Wall -Wextra
我正在玩单行声明和多个变量的初始化,并注意到 C 中的一些不寻常的东西
#include <stdio.h>
int main() {
int a, c, d, e = 0;
printf("a = %d, c = %d, d = %d, e = %d", a, c, d, e);
}
所以上面程序的输出是
a = 2961408, c = 0, d = 4200720, e = 0
容易,因为 e
被赋值 0
,其余的都可能是垃圾值 ,但 c 的值为 0。我将代码更改为
#include <stdio.h>
int main() {
int a, b, c, d, e = 0;
printf("a = %d, b = %d, c = %d, d = %d, e = %d", a, b, c, d, e);
}
这次输出是:
a = 2297856, b = 0, c = 4200736, d = 4200848, e = 0
再次将第二个变量初始化为 0
[此处 b
,之前 c
] 以及 e
,其余均为垃圾。
对于 3
、4
、5
、6
、7
等变量,每次第二次都注意到同样的事情声明中的变量被初始化为值 0
以及最后一个变量。
这不是最后一个变量初始化的情况,但是如果我在第二个变量之后分配值 0
的任何变量。我的第二个变量得到 0
作为它的初始化值。是我的编译器有问题还是 C 背后有一些逻辑?
#include <stdio.h>
int main() {
int a, b, c, d, e, g = 10, f;
printf("a = %d, b = %d, c = %d, d = %d, e = %d, g = %d, f = %d", a, b, c, d, e, g, f);
}
输出:
a = 3702784, b = 0, c = 4200752, d = 4200864, e = 6422400, g = 10, f = 4200752
[P.S。我的 C 编译器是 gcc (MinGW.org GCC Build-2) 9.2.0 ]
Is it something wrong with my compiler
没有
or is there some logic behind this in C?
没有
读取未初始化的本地值会给您不确定的值。这就是所谓的 undefined behavior - 您首先需要了解的是它的含义。 What is undefined behavior and how does it work? 正如我在那里写的那样:
It is important to investigate the cause of the undefined behavior - not so much the symptoms. Beginners often ask why a certain behavior occurred when they did something undefined, but there is often not much to learn from investigating the outcome. Time is better spent on learning what caused the undefined behavior.
现在这是未定义行为的原因实际上是因为您没有获取那些未初始化的局部变量的地址,请参阅此答案:(Why) is using an uninitialized variable undefined behavior?所以程序可能会崩溃。
撇开这个问题不谈,在许多系统上,不确定的值表现为打印恰好存储在 RAM 中某些随机位置的任何内容。 RAM 内存的工作方式,我们从不“删除”或“擦除”其中的任何内容。我们只是跟踪使用了哪些位置,然后当它们不再使用时,存储在那里的任何东西都会保留下来。
但是没有任何保证,因此优化编译器可能会打印一些随机 CPU 寄存器的内容。同样,从中学习没有任何意义。您不妨掷一些骰子,然后尝试找出您获得某些值的原因。
一些糟糕的编译器使用“调试构建”,其中整个 RAM 设置为零,因此在这样的系统上你会得到所有的零。这很糟糕,因为它会隐藏错误,直到您从调试切换到发布。
无论如何,从中学到的唯一重要的事情是 gcc/mingw 会抱怨未初始化的变量,以防您使用 -Wall
。作为初学者,养成编译的习惯:
gcc -std=c11 -pedantic-errors -Werror -Wall -Wextra