天天看点

c语言long long int出错,c – 打印unsigned long long int值类型返回奇怪的结果

这是错误的:

printf("unsigned long long int: \n%llu to %llu \n\n", 0, ULONG_MAX);

您使用无符号长长格式说明符,但您传递int和无符号长整型值.促销规则意味着您可以对于大小或更小的一切都是马虎,这不适用于长时间.

使用演员:

printf("unsigned long long int: \n%llu to %llu \n\n",

0ULL, (unsigned long long) ULONG_MAX);

说明:将参数传递给printf时,可以将适用于int的任何类型升级为int,然后将可以匹配无符号int的任何类型提升为unsigned int.只要可以使用格式说明符指定的类型表示传递的值,也可以将无符号类型传递给带格式的格式说明符,反之亦然.

所以你一定要小心长久,但是你可以用int,short和char来形容词.

大多数编译器都有设置让他们警告你这种类型的错误,因为它可以很容易地在编译时检测到; GCC和Clang有-Wformat,其结果如下警告:

test.c:5: warning: format ‘%llu’ expects type ‘long long unsigned int’, but argument 2 has type ‘int’

test.c:5: warning: format ‘%llu’ expects type ‘long long unsigned int’, but argument 3 has type ‘long unsigned int’