printf 为统一变量打印什么?

发布于 2024-11-06 20:05:24 字数 143 浏览 2 评论 0原文

代码应该打印什么? 0 或任何垃圾值还是取决于编译器?

#include <stdio.h>
int a;
int main() 
{ 
   printf("%d\n",a);
   return 0;
}

What should the code print? 0 or any garbage value or will it depend on the compiler?

#include <stdio.h>
int a;
int main() 
{ 
   printf("%d\n",a);
   return 0;
}

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

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

发布评论

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

评论(5

倥絔 2024-11-13 20:05:24

答案是 0。全局变量被初始化为零。

the answer is 0. Global variables are initialized to zero.

心意如水 2024-11-13 20:05:24

我想说你的代码可能会输出任何内容,或者只是任何事情都可能发生,因为你的代码根据 C99 调用未定义的行为。

您在范围内没有 printf 的原型。

J.2 未定义的行为

- 对于在使用函数原型定义函数的范围内调用没有函数原型的函数,原型以省略号结尾,或者提升后的参数类型与参数类型不兼容( 6.5.2.2)。

如果问题是关于全局变量的初始化,那么 a 将被初始化为 0,因为它具有静态存储持续时间。

I would say your code might output anything or simply anything can happen because your code invokes Undefined Behaviour as per C99.

You don't have a prototype for printf in scope.

J.2 Undefined behavior

— For call to a function without a function prototype in scope where the function is defined with a function prototype, either the prototype ends with an ellipsis or the types of the arguments after promotion are not compatible with the types of the parameters (6.5.2.2).

If the question is about initialization of global variables then a would be initialized to 0 because it has static storage duration.

故事↓在人 2024-11-13 20:05:24

我在 C99 标准,第 6.7.8.10 节,初始化中找到:

如果一个具有自动存储期限的对象没有显式初始化,那么它的值为
不定。如果具有静态存储持续时间的对象未显式初始化,
然后:
— 如果是指针类型,则初始化为空指针;
— 如果它具有算术类型,则将其初始化为(正或无符号)零;
— 如果是聚合,则根据这些规则(递归地)初始化每个成员;
— 如果是联合,则根据这些初始化(递归地)第一个命名成员
规则。

第 6.2.4.3 节定义:

一个对象,其标识符是通过外部或内部链接声明的,或者通过
存储类说明符 static 具有静态存储持续时间。它的生命周期是整个
程序的执行及其存储的值仅在程序之前初始化一次
启动。

换句话说,全局变量被初始化为 0。自动变量(即非静态局部变量)不会自动初始化。

I found on C99 standard, Section 6.7.8.10, Initialization:

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 or unsigned) 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.

Section 6.2.4.3 defines:

An object whose identifier is declared with external or internal linkage, or with the
storage-class specifier static has static storage duration. Its lifetime is the entire
execution of the program and its stored value is initialized only once, prior to program
startup.

In other words, globals are initialized as 0. Automatic variables (i.e. non-static locals) are not automatically initialized.

那一片橙海, 2024-11-13 20:05:24

没有自动变量[通常我们在大多数情况下在函数中使用]所有其他变量的值都被分配为0

without automatic variable [generally what we use in function in most cases] all other variable's value is assigned to 0

流云如水 2024-11-13 20:05:24

全局变量初始化为0。自动变量(即非静态局部变量)不会自动初始化。

Global variables are initialized as 0. Automatic variables (i.e. non-static locals) are not automatically initialized.

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