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 ? 
2
  • Similar question: stackoverflow.com/questions/589575/size-of-int-long-etc Commented Dec 26, 2013 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) ? Commented Dec 26, 2013 at 13:53

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I see. But what values will a simple int (signed) hold ? -32767 to 32767 , this would be the same like the short int does!
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 typedefs 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.
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 ?
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<int>::max() and numeric_limits<int>::min() from the limits header.
2

neither. it's an integer type.

Comments

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.)

Comments

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).

Comments

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.

1 Comment

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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.