Is `int` always signed?

时间秒杀一切 提交于 2021-01-27 04:17:04

问题


I always thought that in C, int stands for signed int ; but I have heard that this behavior is platform specific and in some platforms, int is unsigned by default. Is it true? What says the standard, and has it evolved over time?


回答1:


You are quite right. As per C11 (the latest c standard), chapter §6.7.2

  • int, signed, or signed int

is categorized as same type (type specifiers, to be exact). So, int is the same as signed int.

Also, re-iterating the same, from chapter §6.2.5/P4

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) [....]

So, for any conforming environment, int stands for signed int and vice versa.




回答2:


int, signed, and signed int are all the same type.

The exact form of int is implementation specific; the range must be at least -32767 to +32767. There is no upper limit on the range. Note also that the complement scheme can differ too: 2's complement is common these days although 1's complement and signed magnitude are also allowed.




回答3:


According to this Wikipedia article, int is a signed integral data type which is at least 16 bits in size.



来源:https://stackoverflow.com/questions/44624857/is-int-always-signed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!