C 当我在我的电脑和学校电脑上编译时,信息不一样

C 当我在我的电脑和学校电脑上编译时,信息不一样,c,string,strlen,C,String,Strlen,当我改变它的时候,它是好的,但是为什么我在我的个人电脑上得到这个信息呢?(2 GHz Intel Core i7,4 Go 1333 MHz DDR3,Intel HD Graphics 3000 384 Mo)更改这些语句中的格式说明符 test.c:19:17: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat] printf("%d\n", str

当我改变它的时候,它是好的,但是为什么我在我的个人电脑上得到这个信息呢?(2 GHz Intel Core i7,4 Go 1333 MHz DDR3,Intel HD Graphics 3000 384 Mo)

更改这些语句中的格式说明符

test.c:19:17: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
        printf("%d\n", strlen(s2));
                ~~     ^~~~~~~~~~
                %lu
test.c:20:17: warning: format specifies type 'int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
        printf("%d\n", my_strlen(s));
                ~~     ^~~~~~~~~~~~
                %zu


实际上,您的程序具有未定义的行为,因为您使用的格式说明符
%d
不正确,该说明符是为
int
类型的对象设计的,而对象的类型为无符号
size\u t
。例如sizeof(size_t)可以大于sizeof(int),或者size_t对象的正值不能在int类型的对象中表示。编译器会警告您这一点。

首先对不起,我不是英国人 您应该使用%d作为int变量,您应该使用%zu(我认为,但您必须验证)
错误是%d不正确

我在两台计算机上得到了不同的警告,因为编译器设置以及处理器和RAM不同。

哪个编译器?你是如何编译的?我在computersTry
-Wall-Wextra
和它一起使用GCC。可能重复的是,我理解,但为什么我在学校用%d编译它时,没有收到任何错误消息,我工作正常,但是在我的个人计算机上,我想更改它?@user3540997这取决于编译器的设置:编译器应该捕获什么级别的警告。
size\u t
是一个取决于体系结构的类型定义。在没有生成警告的计算机上,它可能是使用默认编译器设置将typedef'd设置为
unsigned int
,在您的计算机上,它是将typedef'd设置为
unsigned long
,如警告文本中所述。好的,谢谢!!你知道我们可以在哪里检查编译器设置吗?@user3540997如果你使用的是IDE,那么你应该检查它的选项。或者,如果您使用的是批处理文件,请查看文件中的设置。我不确定解决方案,但我展示了解决方法,并展示了问题。
test.c:19:17: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
        printf("%d\n", strlen(s2));
                ~~     ^~~~~~~~~~
                %lu
test.c:20:17: warning: format specifies type 'int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
        printf("%d\n", my_strlen(s));
                ~~     ^~~~~~~~~~~~
                %zu
    printf("%d\n", strlen(s2));
    printf("%d\n", my_strlen(s));
    printf("%zu\n", strlen(s2));
    printf("%zu\n", my_strlen(s));