int always short when defined? - c++

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 ?

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.

neither. it's an integer type.

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

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

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.

Related

What is the difference between "long", "long long", "long int", and "long long int" in C++?

I am transitioning from Java to C++ and have some questions about the long data type. In Java, to hold an integer greater than 232, you would simply write long x;. However, in C++, it seems that long is both a data type and a modifier.
There seems to be several ways to use long:
long x;
long long x;
long int x;
long long int x;
Also, it seems there are things such as:
long double x;
and so on.
What is the difference between all of these various data types, and do they all have the same purpose?
long and long int are identical. So are long long and long long int. In both cases, the int is optional.
As to the difference between the two sets, the C++ standard mandates minimum ranges for each, and that long long is at least as wide as long.
The controlling parts of the standard (C++11, but this has been around for a long time) are, for one, 3.9.1 Fundamental types, section 2 (a later section gives similar rules for the unsigned integral types):
There are five standard signed integer types : signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list.
There's also a table 9 in 7.1.6.2 Simple type specifiers, which shows the "mappings" of the specifiers to actual types (showing that the int is optional), a section of which is shown below:
Specifier(s) Type
------------- -------------
long long int long long int
long long long long int
long int long int
long long int
Note the distinction there between the specifier and the type. The specifier is how you tell the compiler what the type is but you can use different specifiers to end up at the same type.
Hence long on its own is neither a type nor a modifier as your question posits, it's simply a specifier for the long int type. Ditto for long long being a specifier for the long long int type.
Although the C++ standard itself doesn't specify the minimum ranges of integral types, it does cite C99, in 1.2 Normative references, as applying. Hence the minimal ranges as set out in C99 5.2.4.2.1 Sizes of integer types <limits.h> are applicable.
In terms of long double, that's actually a floating point value rather than an integer. Similarly to the integral types, it's required to have at least as much precision as a double and to provide a superset of values over that type (meaning at least those values, not necessarily more values).
Long and long int are at least 32 bits.
long long and long long int are at least 64 bits. You must be using a c99 compiler or better.
long doubles are a bit odd. Look them up on Wikipedia for details.
long is equivalent to long int, just as short is equivalent to short int. A long int is a signed integral type that is at least 32 bits, while a long long or long long int is a signed integral type is at least 64 bits.
This doesn't necessarily mean that a long long is wider than a long. Many platforms / ABIs use the LP64 model - where long (and pointers) are 64 bits wide. Win64 uses the LLP64, where long is still 32 bits, and long long (and pointers) are 64 bits wide.
There's a good summary of 64-bit data models here.
long double doesn't guarantee much other than it will be at least as wide as a double.
While in Java a long is always 64 bits, in C++ this depends on computer architecture and operating system. For example, a long is 64 bits on Linux and 32 bits on Windows (this was done to keep backwards-compatability, allowing 32-bit programs to compile on 64-bit Windows without any changes). long int is a synonym for long.
Later on, long long was introduced to mean "long (64 bits) on Windows for real this time". long long int is a synonym for this.
It is considered good C++ style to avoid short, int, long etc. and instead use:
std::int8_t # exactly 8 bits
std::int16_t # exactly 16 bits
std::int32_t # exactly 32 bits
std::int64_t # exactly 64 bits
std::size_t # can hold all possible object sizes, used for indexing
You can use these int*_t types by including the <cstdint> header. size_t is in <stdlib.h>.
This looks confusing because you are taking long as a datatype itself.
long is nothing but just the shorthand for long int when you are using it alone.
long is a modifier, you can use it with double also as long double.
long == long int.
Both of them take 4 bytes.
Historically, in early C times, when processors had 8 or 16 bit wordlength,intwas identical to todays short(16 bit). In a certain sense, int is a more abstract data type thanchar,short,longorlong long, as you cannot be sure about the bitwidth.
When definingint n;you could translate this with "give me the best compromise of bitwidth and speed on this machine for n". Maybe in the future you should expect compilers to translateintto be 64 bit. So when you want your variable to have 32 bits and not more, better use an explicitlongas data type.
[Edit: #include <stdint.h> seems to be the proper way to ensure bitwidths using the int##_t types, though it's not yet part of the standard.]
There is no deffirence, (long long x ) is equivalent to (long long int x ), but the second confirms that variable x is integer

Int types in C/C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are C++ int and long types both 4 bytes?
In C/C++, what is the difference between:
u_int64 myNum;
and:
unsigned long myNum;
As far as I can tell, both ate just unsigned integers, with 64bits of memory.
unsigned long does not have to be 64 bits, whereas uint64_t does. There is a kind of hierarchy of integer types where each type has to be at least large as the preceding type: signed char, short, int, long, long long, and similarly for their unsigned counterparts. There are some anchor points, stating that char is one byte (a byte does not have to be 8 bits, as far as I can recall short is at least 2 bytes 16 bits. In C++11, long long is at least 64 bits. But none of these types is exactly a given number of bits.
See fixed width integer types for more information (thanks to #chris for the link).
unsigned long is dependent on the machine like every int in C/C++. Many library especially libraries that allow for two machines to interact will type def most int like numbers to make sure both are the same size. u_int64 is basically type def to an unsigned integer of 64 bits, to allow for use on any machine. In theory unsigned long could be 128, 64, 256 or pretty much any size.
The C language itself doesn't specify how big an int is
long only has to be at least as long as int
long long only has to be at least as long as long
also u_int64_t is a c99 type, and wouldn't be available in ANSI c89.
also among 64 bit architectures that are differences LP64 indicates long's and pointers are 64 bit, LLP64 means that long long's and pointers are 64 bit.

What is the difference between long long and long int

I know the difference between long and int
But What is the difference between "long long" and "long int"
There are several shorthands for built-in types.
short is (signed) short int
long is (signed) long int
long long is (signed) long long int.
On many systems, short is 16-bit, long is 32-bit and long long is 64-bit. However, keep in mind that the standard only requires
sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
And a consequence of this is that on an exotic system, sizeof(long long) == 1 is possible.
According to the C standard the integral types are defined to provide at least the following ranges:
int -32767 to +32767 representable in 16 bits
long -2147483647 to +2147483647 representable in 32 bits
long long -9223372036854775807 to +9223372036854775807 representable in 64 bits
Each can be represented as to support a wider range. On common 32 bit systems int and long have the same 32 bit representation.
Note that negative bounds are symmetric to their positive counterparts to allow for sign and magnitude representations: the C language standard does not impose two's complement.
long long may be a bigger type than long int. For example on x86 32 bit long long would be a 64-bit type rather than 32 bit for long int.
On 64 bit systems it doesn't make any difference in their sizes. On 32 bit systems long long is guaranteed store values of 64 bit range.
Just to avoid all these confusions, it is always better to use the standard integral types: (u)int16_t, (u)int32_t and (u)int64_t available via stdint.h which provides transparency.
An int on 16 bit systems was 16 bits. A "long" was introduced as a 32 bit integer, but on 32 bit systems long and int mean the same thing (both are 32 bit.) So on 32 and 64 bit systems, long long and long int are both 64 bit. The exception is 64 bit UNIX where long is 64 bits.
See the integer Wikipedia article for a more detailed table.
long int is a synonym for long. long long int is a synonym for long long.
The only guarantee you have in standard C++ is that long long is at least as large as long but can be longer. This is specified in ยง3.9.1.2 in the most recent publicly available draft of the standard n3242.
The C standard doesn't make any specific width requirements for integral types other than minimal ranges of values that the type needs to be able to represent, and that the widths are non-decreasing: short <= int <= long int <= long long int (similarly for the unsigned types). long long only became part of the standard in C99 and C++0x, by the way. The minimum required ranges can be found in this Wikipedia article.
I think:
"long" doubles the number of bits allocated to the data type.
So long (32 bits?) becomes 64 bits.
Int (16 bits?) becomes 32 bits.

What's the difference between long long and long

What's the difference between long long and long? And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
#include <iostream>
using namespace std;
int main(){
long long a = 600851475143;
}
Going by the standard, all that's guaranteed is:
int must be at least 16 bits
long must be at least 32 bits
long long must be at least 64 bits
On major 32-bit platforms:
int is 32 bits
long is 32 bits as well
long long is 64 bits
On major 64-bit platforms:
int is 32 bits
long is either 32 or 64 bits
long long is 64 bits as well
If you need a specific integer size for a particular application, rather than trusting the compiler to pick the size you want, #include <stdint.h> (or <cstdint>) so you can use these types:
int8_t and uint8_t
int16_t and uint16_t
int32_t and uint32_t
int64_t and uint64_t
You may also be interested in #include <stddef.h> (or <cstddef>):
size_t
ptrdiff_t
long long does not exist in C++98/C++03, but does exist in C99 and c++0x.
long is guaranteed at least 32 bits.
long long is guaranteed at least 64 bits.
To elaborate on #ildjarn's comment:
And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
The compiler looks at the literal value 600851475143 without considering the variable that you're assigning it to/initializing it with. You've written it as an int typed literal, and it won't fit in an int.
Use 600851475143LL to get a long long typed literal.
Your C++ compiler supports long long, that is guaranteed to be at least 64-bits in the C99 standard (that's a C standard, not a C++ standard). See Visual C++ header file to get the ranges on your system.
Recommendation
For new programs, it is recommended that one use only bool, char, int, and double, until circumstance arises that one of the other types is needed.
http://www.somacon.com/p111.php
Depends on your compiler.long long is 64 bits and should handle 12 digits.Looks like in your case it is just considering it long and hence not handling 12 digits.

The difference between unsigned long and UINT64

What is the difference between unsigned long and UINT64?
I think they are the same, but I'm not sure.
The definition of UINT64 is :
typedef unsigned __int64 UINT64
(by using StdAfx.h)
UINT64 is specific and declares your intent. You want a type that is an unsigned integer that is exactly 64 bits wide. That this may be equal to an unsigned long on some platforms is coincidence.
The C++ standard does not define the sizes of each of the types (besides char), so the size of unsigned long is implementation defined. In most cases I know of, though, unsigned long is an unsigned 32 bit type, while UINT64 (which is an implementation type, not even mentioned in the standard) is a 64 bit unsigned integer in VS.
The unsigned long type size could change depending on the architecture of the system you are on, while the assumption is that UINT64 is definitely 64 bits wide. Have a look at http://en.wikipedia.org/wiki/C_variable_types_and_declarations#Size
See http://msdn.microsoft.com/en-us/library/s3f49ktz(VS.90).aspx
You want to see the difference between unsigned long and unsigned __int64.
A long is typically 32 bits (but this may very per architecture) and an uint64 is always 64 bits. A native data type which is sometimes 64 bits long is a long long int.