在 C 中赋值,同时在 1 行中声明多个变量 [英] Value assignment in C while declaring multiple variable in 1 line

查看:41
本文介绍了在 C 中赋值,同时在 1 行中声明多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩单行声明和多个变量的初始化,并注意到 C 中的一些不寻常的东西

I was playing with the single line declaration and initialization of multiple variables and notices something out of ordinary in 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.我把代码改成

Easy since e is assigned 0 and rest all may be garbage value but c has the value 0. I changed the code to

#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 和剩下的都是垃圾.

Again the second variable was initialized as 0 [Here b, previously c] along with e and rest all are garbage.

对于34567,也注意到了同样的事情 等变量,每次在声明中的第二个变量被初始化为 0 和最后一个变量时.

And the same thing was noticed for 3, 4, 5, 6, 7, and so on variables every time the second variable in the declaration is initialized the value 0 along with the last variable.

这不是最后一个变量初始化的情况,而是如果我在第二个变量之后赋值为 0 的任何变量.我的第二个变量获得 0 作为它的初始化值.是我的编译器有问题还是 C 语言背后有一些逻辑?

This is not the case of initialization of last variable but if I assign any variable after the second variable with value 0. My second variable get 0 as it's initialization value. Is it something wrong with my compiler or is there some logic behind this in 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

[附注我的 C 编译器是 gcc (MinGW.org GCC Build-2) 9.2.0 ]

[P.S. My C compiler is gcc (MinGW.org GCC Build-2) 9.2.0 ]

推荐答案

我的编译器有问题吗

Is it something wrong with my compiler

没有

或者在 C 语言背后有什么逻辑?

or is there some logic behind this in C?

没有

读取未初始化的本地值会给您不确定的值.这就是所谓的未定义行为 - 您首先需要了解它的含义.什么是未定义行为以及它是如何工作的?正如我在那里写的:

Reading uninitialized local values give you indeterminate values. This is so-called undefined behavior - the first thing you need to understand is what that means. What is undefined behavior and how does it work? As I wrote there:

调查未定义行为的原因很重要 - 而不是症状.初学者经常会问为什么当他们做了一些未定义的事情时会发生某种行为,但通常从调查结果中学到的东西不多.最好花时间了解导致未定义行为的原因.

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.

现在这个未定义行为的原因其实是因为你没有取那些未初始化的局部变量的地址,看这个答案:(为什么)正在使用未初始化的变量未定义行为?所以程序可能会崩溃.

Now the reason why this is undefined behavior is actually because you didn't take the address of those uninitialized local variables, see this answer: (Why) is using an uninitialized variable undefined behavior? So the program might as well just crash.

撇开这个问题不谈,在许多系统上,不确定值表现为打印存储在 RAM 中某些随机位置的任何内容.RAM 内存的工作方式,我们从不删除"或擦除"任何东西.我们只是跟踪使用了哪些位置,然后当它们不再使用时,存储在那里的任何内容都会保留.

That issue aside, on many systems indeterminate values manifest themselves as printing whatever happened to be stored at some random locations in RAM. The way RAM memory works, we never "delete" or "erase" anything from it. We just keep track of which locations that are used, then when they aren't used any longer, whatever happened to be stored there remains.

但是没有任何保证,因此优化编译器也可以打印一些随机 CPU 寄存器的内容.再一次,从中学习没有任何意义.您不妨掷一些骰子,然后尝试找出获得某些值的原因.

But there are no guarantees of anything so an optimizing compiler might just as well print the contents of some random CPU register. Again, there's nothing meaningful to learn from this. You might as well toss some dice and then try to find a reason why you got certain values.

一些糟糕的编译器使用调试版本"整个 RAM 设置为零,因此在此类系统上,您将获得全零.这很糟糕,因为它会隐藏错误,直到您从调试切换到发布.

Some bad compilers utilize a "debug build" where the whole RAM is set to zero, so on such systems you'll get all zeroes. This is bad since it hides away bugs until you switch from debug to release.

无论如何,唯一值得从中学习的是,如果您使用了 -Wall,gcc/mingw 会抱怨未初始化的变量.作为初学者,请养成编译的习惯:

Anyway, the only thing important to learn from this is gcc/mingw would have whined about uninitalized variables in case you used -Wall. As a beginner, make a habit of compiling with:

gcc -std=c11 -pedantic-errors -Werror -Wall -Wextra

这篇关于在 C 中赋值,同时在 1 行中声明多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆