0

Just a little question : Today I got back to the value the variable types can hold and I wondered if an int defined without short or long is always short or long, like signed or unsigned!

int i ; //short or long ?
Maroun
  • 94,125
  • 30
  • 188
  • 241
Hans Peter
  • 571
  • 1
  • 8
  • 14
  • Similar question: http://stackoverflow.com/questions/589575/size-of-int-long-etc – cageman Dec 26 '13 at 13:20
  • @Maroun Maroun : so you edited my question, but could you please tell me how I should get int i; into code from my mobile (==stack overflow mobile version) ? – Hans Peter Dec 26 '13 at 13:53

5 Answers5

2

neither. it's an integer type.

zapredelom
  • 1,009
  • 1
  • 11
  • 28
2

No. int is a distinct type from short and long. short, int, and long can have 3 different sizes. (Also, note that usually, instead of modifying int as short int or long int, it's generally preferred to just write short or long.)

user2357112
  • 260,549
  • 28
  • 431
  • 505
2

It's neither. On a lot of systems, including the usual Linuxes, int will be 32 bits, short will be 16, and long 64. This isn't guaranteed by the C language however--the types need to be ordered by size but they don't have to be those specific sizes (e.g. int could be 64 bits on some systems, or 16).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2

The answer is "neither". int without a modifier is int, short int is no larger than int and may be the same size. long int is no smaller than int, but may be the same size. This is all according to the C++ standard, which also says that short must be at least 16 bits, and long should be at least 32 bits. But it's entirely possible to have a machine where all are 32 or 64 bits.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I see. But what values will a simple int (signed) hold ? -32767 to 32767 , this would be the same like the short int does! – Hans Peter Dec 26 '13 at 13:25
  • Depends on the implementation of the compiler. `int` is guaranteed to have at least 16 bits, which covers that range, but in most modern systems, an `int` is 32 bits. It could be 36, 48 or 64 bits too. If you have specific requirements that your variables have a certain range, then using `typedef`s that define types of X bits (or "no less than X") is a good idea. There is `cstdint` and `stdint.h` (for C++ and C respectively) that define a number of types like `uint32_t` and `uint_least32_t` for this purpose. If you don't care, use `int`. – Mats Petersson Dec 26 '13 at 13:32
  • Wait a sec! I tried sizeof(int) and it says 4 bytes, means 32 bits, so the biggest number is 4294967295 (unsigned), and now, if I define the int as signed (like default), the biggest is 2147483647 , and now : is the smallest (negative) number 2147483647 or 2147483648 ? – Hans Peter Dec 27 '13 at 21:33
  • The smallest negative number depends on the architecture. C and C++ doesn't specify in detail what number system the compiler should use - it could be oneäs complement or two's complement. One's complement has a "negative zero", which means both the negative and positive sides are the same size. Two's complement has no negative zero, and thus the negative side is "one more" than the positive side. You can use `INT_MAX` and `INT_MIN` to find out (from cstdint), or `numeric_limits::max()` and `numeric_limits::min()` from the limits header. – Mats Petersson Dec 28 '13 at 08:30
1

Unless specified, int is always signed.

Just to know, short is 16 bits, long is 32 bits, and int is either 16 or 32 bits depending on compiler. Unless specified, all these are signed by default.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
dips
  • 114
  • 6
  • `short` is **at least** 16 bits, `int` is **at least** 16 bits, `long` is **at least** 32 bits. Further, `sizeof(short) <= sizeof(int)` and `sizeof(int) <= sizeof(long)`. – Pete Becker Dec 26 '13 at 13:55