在c中声明没有任何数据类型的变量 [英] declaring variables without any data type in c

查看:31
本文介绍了在c中声明没有任何数据类型的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个c程序中

a=8;
main()
{
    printf("%d", a);
}   

已声明变量 a 没有任何数据类型,但该程序仍能成功编译并提供所需的输出.
输出::

variable a has been declared without any data type and still this program compiles successfully and gives the desired output.
output ::

8  

ideone 上查看.
但是,当我在 main 中声明相同的变量时,它会出现编译错误.

see it on ideone.
but, when i declared the same variable inside main, it gives compilation error.

main()
{
     a=8;
    printf("%d", a);
}  

输出::

prog.c:2: warning: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:3: error: ‘a’ undeclared (first use in this function)
prog.c:3: error: (Each undeclared identifier is reported only once
prog.c:3: error: for each function it appears in.)
prog.c:4: warning: implicit declaration of function ‘printf’
prog.c:4: warning: incompatible implicit declaration of built-in function ‘printf’  

请参阅此处.

第一个程序如何工作,但第二个程序如何?

推荐答案

您在这里看到的是 "隐式整数规则" 在起作用.简单地说,规则是:

What you see here is the "Implicit Int Rule" at work. Simply put the rule says:

没有明确类型名称的声明的变量被假定为int类型."

"A variable declared without an explicit type name is assumed to be of type int."

请注意,此规则在 c99 标准中已被撤销[Ref 1].但是,根据您的编译器及其设置,第一个示例可能会在编译时发出警告,或者将无法编译(具有严格的编译设置)

Note that this rule was revoked in the c99 Standard[Ref 1].However, depending on your compiler and its settings, the first example might compile with a warning, or will fail to compile(with strict compilation settings)

如果您按照 c99 标准使用严格的设置编译第一个示例,编译器会告诉您根本原因.

If you compile your first example with strict settings adhering to c99 Standard the compiler will tell you the root cause.

检查此处.

check here.

cc1:警告被视为错误
prog.c:1: 错误:数据定义没有类型或存储类
prog.c:1: 错误:在‘a’的声明中类型默认为‘int’
prog.c:3: 错误:返回类型默认为‘int’
prog.c:在函数‘main’中:
prog.c:4: 错误:函数‘printf’的隐式声明
prog.c:4: 错误:内置函数‘printf’的隐式声明不兼容

cc1: warnings being treated as errors
prog.c:1: error: data definition has no type or storage class
prog.c:1: error: type defaults to ‘int’ in declaration of ‘a’
prog.c:3: error: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:4: error: implicit declaration of function ‘printf’
prog.c:4: error: incompatible implicit declaration of built-in function ‘printf’

为什么第一个例子有效而第二个例子无效?

注意规则中对变量声明"一词的强调.

Note the emphasis on the words "variable declared" in the rule.

在第一个示例中,由于语句在全局范围内,因此被视为隐式声明Implicit Int Rule 得到应用于它.

In first example, since the statement is at the global scope it is treated as an Implicit declaration, and the Implicit Int Rule gets applied to it.

在第二个示例中,语句充当赋值而不是声明.由于没有声明,Implicit int 规则在这里不适用.在没有任何类型的情况下,编译器无法确定 a 的类型,因此会报告错误.

In Second example, the statement acts as an Assignment and not a Declaration. Since there is no declaration the Implicit int rule does not apply here. In the absence of any type the compiler cannot determine what is the type of a and hence reports the error.

[参考 1]

C99 标准:前言
第 5 段:

此版本取代了之前的版本,ISO/IEC 9899:1990,经修正和修正通过 ISO/IEC 9899/COR1:1994ISO/IEC 9899/COR2:1995ISO/IEC 9899/AMD1:1995.
与上一版相比的主要变化包括:
.....
.....
— 删除隐式 int
.....
.....

This edition replaces the previous edition, ISO/IEC 9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/COR2:1995, and ISO/IEC 9899/AMD1:1995.
Major changes from the previous edition include:
.....
.....
— remove implicit int
.....
.....

这篇关于在c中声明没有任何数据类型的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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