INT_MIN和编译器诊断 [英] INT_MIN and compiler diagnostic

查看:60
本文介绍了INT_MIN和编译器诊断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请参阅此测试程序:


1 #include< stdio.h>

2 #include< ; stdlib.h>

3 #include< limits.h>

4

5 int main(无效)

6 {

7 int y = -2147483648;

8 int x = INT_MIN;

9

10 printf(INT_MAX =%d INT_MIN =%d \ n,INT_MAX,

INT_MIN);

11 printf(" x =%dy = %d \ n",x,y);

12

13

14返回EXIT_SUCCESS;

15}


输出:

INT_MAX = 2147483647 INT_MIN = -2147483648

x = -2147483648 y = -2147483648


当我使用gcc编译它时,我在第7行得到诊断:


[pcg @ mylinux test] $ gcc -ansi -pedantic -Wall - o / tmp / x / tmp / xc

/ tmp / xc:函数`main'':

/ tmp / xc:7:警告:这个十进制常量是仅在ISO C90中无符号


然而,在我的系统中,INT_MIN确实是-2147483648,因为上面程序的输出建议为




是这种诊断有什么特别的原因[按照ANSI C]?


然而,第8行,逻辑上等同于第7行不会产生任何价值诊断。 INT_MIN在limits.h中定义为:


#define INT_MIN(-INT_MAX - 1)

#define INT_MAX 2147483647


谢谢,

pcg

解决方案

gcc -ansi -pedantic -Wall -o / tmp / x / tmp / xc

/ tmp / xc:在函数main中:

/ tmp / xc:7:警告:此十进制常量仅为无符号在ISO C90


但是,在我的系统中,INT_MIN确实是-2147483648,因为上面程序的输出建议为




这个诊断是否有任何具体原因[按照ANSI C]?


但是,第8行,逻辑上等同于第7行不是

产生任何诊断。 INT_MIN在limits.h中定义为:


#define INT_MIN(-INT_MAX - 1)

#define INT_MAX 2147483647


谢谢,

pcg

blockquote>

2月28日,10:07,p_cricket _... @ yahoo.co .in写道:


当我使用gcc编译它时,我在第7行得到诊断:


[pcg @ mylinux test]


gcc -ansi -pedantic -Wall -o / tmp / x / tmp / xc

/ tmp / xc:函数`main'' :

/ tmp / xc:7:警告:此十进制常数仅在ISO C90
中无符号



在我的(Sun sparc)上使用gcc与我得到完全相同的标志


xc:7:警告:十进制常量太大以至于无符号


程序输出是:


INT_MAX = 2147483647 INT_MIN = -2147483648

x = -2147483648 y = -2147483648


有趣的是它告诉我康斯坦t是未签名的,然后打印它

签名。


当你需要语言律师时,哪里是语言律师?!



Please see this test program:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4
5 int main (void)
6 {
7 int y = -2147483648;
8 int x = INT_MIN;
9
10 printf("INT_MAX = %d INT_MIN = %d\n", INT_MAX,
INT_MIN);
11 printf("x = %d y = %d\n", x, y);
12
13
14 return EXIT_SUCCESS;
15 }

Output:
INT_MAX = 2147483647 INT_MIN = -2147483648
x = -2147483648 y = -2147483648

When I compile this using gcc, I get a diagnostic on line 7:

[pcg@mylinux test]$gcc -ansi -pedantic -Wall -o /tmp/x /tmp/x.c
/tmp/x.c: In function `main'':
/tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90

However, on my system INT_MIN is indeed -2147483648 as
suggested by the output of above program.

Is there any specific reason for this diagnostic [as per ANSI C] ?

However, line 8, which is logically equivalent to line 7 does not
produce any diagnostic. INT_MIN is defined in limits.h as:

# define INT_MIN (-INT_MAX - 1)
# define INT_MAX 2147483647

Thanks,
pcg

解决方案

gcc -ansi -pedantic -Wall -o /tmp/x /tmp/x.c
/tmp/x.c: In function `main'':
/tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90

However, on my system INT_MIN is indeed -2147483648 as
suggested by the output of above program.

Is there any specific reason for this diagnostic [as per ANSI C] ?

However, line 8, which is logically equivalent to line 7 does not
produce any diagnostic. INT_MIN is defined in limits.h as:

# define INT_MIN (-INT_MAX - 1)
# define INT_MAX 2147483647

Thanks,
pcg


On 28 Feb, 10:07, p_cricket_...@yahoo.co.in wrote:

When I compile this using gcc, I get a diagnostic on line 7:

[pcg@mylinux test]


gcc -ansi -pedantic -Wall -o /tmp/x /tmp/x.c
/tmp/x.c: In function `main'':
/tmp/x.c:7: warning: this decimal constant is unsigned only in ISO C90

On mine (Sun sparc) using gcc with exactly the same flags I get

x.c:7: warning: decimal constant is so large that it is unsigned

The program output is:

INT_MAX = 2147483647 INT_MIN = -2147483648
x = -2147483648 y = -2147483648

So amusingly, it tells me the constant is unsigned, then prints it
signed.

Where''s a language lawyer when you need one?!


这篇关于INT_MIN和编译器诊断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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