I want to know the main difference with unsigned long long and unsigned long long int. Can they be used inter-changeably.
For calculations involving huge decimal numbers like 9223372036854775807, which one is preferred?
Thanks.
Both of following types are semantically equivalent: minimum 64bit integer without sign and with equal or bigger size than unsigned long int
unsigned long long
unsigned long long int
Related
I'm writing a code like this
int reverse(int x) {
long long res;
......
if(x>0&&res>INT_MAX||x<0&&res>INT_MAX+1){
return 0;
}
......
}
It shows overflow,but when I add conversion to this it complies
int reverse(int x) {
long long res;
......
if(x>0&&res>(unsigned long long)INT_MAX||x<0&&res>(unsigned long long)INT_MAX+1){
return 0;
}
......
}
Can somebody please explain to me what is the problem?
INT_MAX+1 is evaluated as an integer addition of two ints. Since the resultant is not with the range of values that can be represented as an int, you see the overflow.
(unsigned long long)INT_MAX+1 is evaluated as an integer addition of two unsigned long longs. Since both sides of the operator and the resultant are well within the range of values that can be represented as an unsigned long long, there is no overflow.
INT_MAX + 1
In this sub-expression, the type of both operands of + are int, so the type of the result is int, causing integer overflow.
However, if you cast one operand to unsigned long long:
(unsigned long long)INT_MAX + 1
The type of the result if unsigned long long, thus no overflow, simple like that.
What triggers the overflow is the expression
INT_MAX+1
Because INT_MAX is of type int (32 bit integer), INT_MAX+1 exceeds the maximum value of int.
Once you cast it to unsigned long long (64 bit integer),
(unsigned long long)INT_MAX
Is now well below the maximum value for its type, and
(unsigned long long)INT_MAX + 1
is valid
When unsigned/signed long int a; is possible
why unsigned/signed long float/double a; is not possible ?
Why do I get too many types in declaration error for the latter and not for the former ?
There are three floating point types: float, double and long double. None of these have unsigned equivalents, so putting signed or unsigned in front of them is not valid. There is no such type as long float.
You are getting that message because a long double exists, but an unsigned long double does not. unsigned can also be interpreted as an int, therefore you possess two types in the latter declaration: unsigned and long double. I do not believe there is a long float in C++.
That is because the first (long int) is a documented variable type, while the second isn't.
The data types that the C++ language supports are:
char
unsigned char
signed char
int
unsigned int
signed int
short int
unsigned short int
signed short int
long int
signed long int
unsigned long int
float
double
long double
int
short
long
long long
unsigned int / unsigned
unsigned short
unsigned long
unsigned long long
char
bool
float
double
I just never get the limit. Are these all or are there more like:
unsigned char
unsigned bool
unsigned float
unsigned double
or any other?
I have a tomorrow and I want to be clear with the basics.
I just never get the limit. Are these all[?] ...
Don't bother providing links, I have a text book for that matter. Just
answer my question. Yes or No? This is really frustrating. Nothing has
been explicitly mentioned anywhere.
No.
Integer and character types (e.g., int, short, char, wchar_t, etc.) support signedness modifiers (signed/unsigned) and can therefore all be unsigned.
Floating point types (e.g., float, double, long double) do not support signedness modifiers and therefore cannot be unsigned or explicitly signed, for that matter.
A few examples of valid expressions:
char
unsigned char
int
signed int
unsigned short
unsigned long long
A few examples of invalid expressions:
signed double
unsigned double
unsigned float
signed unsigned int
For example, if I were to say:
#define UINT_DEF 500u
Then such a definition would have the type unsigned int. However, what is the default rule for when such suffixes are not given? As in
#define SOME_DEF 500
being placed in the type int. That is, at compile-time, if no suffix is given, are the constants slotted into the lowest data type in which they fit?
Would, for instance,
#define SOME_DEF_2 100
Acquire the datatype of char since it fits?
I asked a previous question on a similar topic and had some good responses. However, little was said to the case where no suffix is given. It was said that if a given suffix is requested of the compiler and the assigned value does not fit in such a type then the constant would get promoted, but little else was said about it. I imagine the answer to be something similar to this in that a default casting (perhaps the smallest available) is given to the constants and in such cases where the value should not fit into this default type then a promotion is realized.
And finally, do arithmetic promotion rules still apply as normal for macros? That is, would
#define TEST_DEF 5000000/50
#define TEST_DEF_2 5000000/50.0
respectively evaluate to 100,000 with a type of long int and 100,000.00 of type float (assuming 5,000,000 is a long and 50 is an int/char, whatever).
Or in the case:
#define TEST_MACRO(x) (16*x)
Since 16 is a constant of type int most likely, would TEST_MACRO(70000) promote the whole thing to long?
#define SOME_DEF 500
500 has type int. The type of an unsuffixed decimal integer constant is the first of the corresponding list in which its value can be represented: int, long, long long.
Then:
#define TEST_DEF 5000000/50
#define TEST_DEF_2 5000000/50.0
Assuming 5000000 is of type int in your system then:
5000000/50 is of type int
5000000/50.0 is of type double
Of course the fact that it is macro does not change anything as macros are just relatively simple textual substitutions.
Finally, assuming 70000 is of type int then:
16 * 70000 is also of type int
Per the 2011 online draft of the C standard:
6.4.4.1 Integer constants
...
5 The type of an integer constant is the first of the corresponding list in which its value can be represented.
Suffix Decimal Constant Octal or Hexadecimal
Constant
-----------------------------------------------------------------
None int int
long int unsigned int
long long int long int
unsigned long int
long long int
unsigned long long int
-----------------------------------------------------------------
u or U unsigned int unsigned int
unsigned long int unsigned long int
unsigned long long int unsigned long long int
------------------------------------------------------------------
l or L long int long int
long long int unsigned long int
long long int
unsigned long long int
------------------------------------------------------------------
Both u or U unsigned long int unsigned long int
and l or L unsigned long long int unsigned long long int
------------------------------------------------------------------
ll or LL long long int long long int
unsigned long long int
------------------------------------------------------------------
Both u or U unsigned long long int unsigned long long int
and ll or LL
So, if you have a decimal integer constant without a suffix, its type will be the smallest of int, long int, or long long int that can represent that value.
Not so elegant but possible is to cast:
#include <inttypes.h> /* For uint16_t */
#define MYFLOAT ((float) 1)
#define MYUNSIGNED16BITINT ((uint16_t) 42.)
#define MYVOIDPOINTER ((void *) 0)
This question already has answers here:
How many bytes is unsigned long long?
(4 answers)
Closed 9 years ago.
What should be the exact typedef for unsigned long long ?
For example:
typedef unsigned int uint32 ; //considering my machine reading
typedef unsigned char byte ;
So what is
typedef unsigned long long ______ ;
Is it uint64 or more than this?
unsigned long long is guaranteed to be at least 64 bits in size. Thus, it is equivalent to uint_least64_t:
typedef unsigned long long uint_least_64_t
unsigned long long my_variable;
means my_variable is an unsigned integer value of 64 bits (or more)
you may find helpfult this too:
How many bytes is unsigned long long?
unsigned long long
is a synonim of
unsigned long long int
that is defined as
typedef unsigned long long int uint64_t
see
Standard Integer Types