没有指定类型时,C编译器不引发错误 [英] C Compiler not throwing error when no type is specified

查看:400
本文介绍了没有指定类型时,C编译器不引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个计划之下不引发错误:

  dfljshfksdhfl;
#包括LT&;&stdio.h中GT;
诠释主(){
        返回0;
}

GCC 也只是抛出一个警告:


  

test.c的:1:1:警告:数据定义没有类型或存储类[默认启用]



解决方案

这是因为即使隐含int是的不再是一部分由于C99 C标准的一些编译器仍然支持它,主要是prevent打破了很多老code的。所以这行:

  dfljshfksdhfl;

最后等于为:

  INT dfljshfksdhfl;

默认情况下为我们带来了更加翔实的警告:

 警告:类型说明符缺少,默认为'廉政'[-Wimplicit-INT]
dfljshfksdhfl;
^ ~~~~~~~~~~~~

我们可以使用 -pedantic-错误标志,把它变成一个错误,但奇怪的是,这并不为,所以我们不得不诉诸 -Werror 工作,并把所有的警告走入误区,这实际上是一个很好的习惯进入。由于remyabel指出了我们还可以使用 = -Werror隐含-INT

Why this below program not throwing an error:

dfljshfksdhfl;
#include <stdio.h>
int main () {
        return 0;
}

gcc would just throw a warning:

test.c:1:1: warning: data definition has no type or storage class [enabled by default]

解决方案

This is because even though implicit int is no longer part of the C standard since C99 some compilers still support it, mainly to prevent breaking a lot of old code. So this line:

dfljshfksdhfl;

ends up being equivalent to:

int dfljshfksdhfl;

clang gives us a much more informative warning by default:

warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
dfljshfksdhfl;
^~~~~~~~~~~~~

We can use the -pedantic-errors flag to turn this into an error, although oddly enough this does not work for clang and so we have to resort to -Werror and turn all warnings into errors, which is actually a good habit to get into. As remyabel points out for clang we can also use -Werror=implicit-int.

这篇关于没有指定类型时,C编译器不引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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