在C语言中,为什么在一个单独的声明中警告养一个全局变量的定义,但确定为一个局部变量? [英] In C,why is definition of a global variable in a separate statement raising warning,but is OK for a local variable?

查看:630
本文介绍了在C语言中,为什么在一个单独的声明中警告养一个全局变量的定义,但确定为一个局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code,为什么全局变量的定义中的x显示警告数据定义没有类型或存储类,但同样的事情,工作正常,局部变量Y?所有我为每个变量做的是先在一条语句中声明它们,然后在另一个statement.What定义它们是它工作正常之一,但显示了其他警告的区别?

 #包括LT&;&stdio.h中GT;      INT X;
      X = 303;     INT主要(无效)
      {
        诠释Ÿ;
        Y = 776;        输出(x的值为%d,和y为%d,X,Y);
      }


解决方案

您正在编译中C89模式。

  INT X;

X

的暂定定义

  X = 303;

然后跨preTED为变量 X 隐式类型 INT (<$的definiton C $ C> INT X = 303; )。在C99或更高版本中,code不会因为隐 INT 的规则被废除,没有隐 INT 的规则,第二行只能是PTED作为一个声明,这不是在文件范围允许跨$ p $。

-std = C89 -Wall -pedantic -Wextra 编译(并加入返回0; ),海湾合作委员会警告

  redef.c:4:1:警告:数据定义没有类型或存储类[默认启用]
redef.c:4:1:警告:类型默认为'诠释'中'X'[-Wimplicit-INT]的声明

In the following code, why does the definition of the global variable "x" show the warning "data definition has no type or storage class" but the same thing works fine for the local variable "y"?All I am doing for each variable is first declare them in one statement and then define them in another statement.What is the difference that it works fine for one but shows warning for the other?

    #include<stdio.h>

      int x;
      x=303;

     int main(void)
      {
        int y;
        y=776 ;

        printf("The value of x is %d,and of y is %d",x,y);
      }

解决方案

You're compiling in C89 mode.

  int x;

is a tentative definition of x.

  x=303;

is then interpreted as a definiton of the variable x with implicit type int (int x = 303;). Under C99 or later, that code would not compile since the "implicit int" rule was abolished and without the "implicit int" rule, the second line could only be interpreted as a statement, which is not allowed at file scope.

Compiling with -std=c89 -Wall -Wextra -pedantic (and adding a return 0; to main), gcc warns

redef.c:4:1: warning: data definition has no type or storage class [enabled by default]
redef.c:4:1: warning: type defaults to ‘int’ in declaration of ‘x’ [-Wimplicit-int]

这篇关于在C语言中,为什么在一个单独的声明中警告养一个全局变量的定义,但确定为一个局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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