gpt4 book ai didi

c - 当我们尝试打印 c 中未初始化的变量(可能是全局变量或局部变量)时,会发生什么?

转载 作者:行者123 更新时间:2023-11-30 19:30:26 25 4
gpt4 key购买 nike

当我们尝试打印 c 中未初始化的变量(可能是全局变量或局部变量)时,会发生什么?在 C 中,变量也由编译器自动初始化为默认值吗?这是我的代码

#include<stdio.h>
int b;
int main()
{
float a;
printf("%f %d",a,b);
}

我得到的输出为

0.000000 0

请问谁能解释一下吗?

最佳答案

具有静态存储持续时间的全局变量是隐式初始化的。

除非显式完成,否则不会初始化具有自动存储持续时间的局部变量。但是,如果局部变量具有静态存储类,则会对其进行初始化。

引用C11 ,

If an object that has automatic storage duration is not initialized explicitly,its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive orunsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

— if it is a union, the first named member is initialized (recursively) according to these rules.

关于c - 当我们尝试打印 c 中未初始化的变量(可能是全局变量或局部变量)时,会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51025187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com