You might want to use the following in C++ to understand this:
#include<iostream>
#include<cxxabi.h>
using namespace std;
using namespace abi;
int main () {
int status;
cout << __cxa_demangle(typeid(2000*2000*2000*2000).name(),0,0,&status);
}
As you can see, the type is int
.
In C, you can use (courtesy of):
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#define typename(x) _Generic((x), /* Get the name of a type */ \
\
_Bool: "_Bool", unsigned char: "unsigned char", \
char: "char", signed char: "signed char", \
short int: "short int", unsigned short int: "unsigned short int", \
int: "int", unsigned int: "unsigned int", \
long int: "long int", unsigned long int: "unsigned long int", \
long long int: "long long int", unsigned long long int: "unsigned long long int", \
float: "float", double: "double", \
long double: "long double", char *: "pointer to char", \
void *: "pointer to void", int *: "pointer to int", \
char(*)[]: "pointer to char array", default: "other")
unsigned int a = 3;
int main() {
printf("%s", typename(a-10));
return 0;
}
Here the type of the expression is unsigned int
because the type mismatch implicitly upgrades the type to the largest type between unsigned int
and int
, which is unsigned int
. The unsigned int
will underflow to a large positive, which will be the expected negative when assigned to or interpreted as an int
. The result of the calculation will always be unsigned int
regardless of the values involved.
C
The minimum default type of an integer literal without a suffix is int
, but only if the literal exceeds this, does its type becomes an unsigned int
; if larger than that it is given a type of a long int
, therefore 2000s are all int
s. The type of an expression performed on a literal however, using unary or binary operators, uses the implicit type hierarchy to decide a type, not the value of the result (unlike the literal itself which uses the length of the literal in deciding the type), this is because C uses type coercion and not type synthesis. In order to solve this, you'd have to use long suffixes ul
on the 2000s to explicitly specify the type of the literal.
Similarly, the default type of a decimal literal is double
, but this can be changed with a f
suffix. Prefixes do not change the type of decimal or integer literals.
The type of a string literal is char []
, although it is really a const char []
, and is just an address of the first character in the actual representation of that string literal in .rodata
, and the address can be taken like any array using the unary ampersand &"string"
, which is the same value (address) as "string"
, just a different type (char (*)[7]
vs. char[7]
; "string"
i.e. char[]
is not just (at compiler level) a pointer to the array, it is the array, whereas the unary ampersand extracts just the pointer to the array). The u
prefix changes this to an array of char16_t
, which is an unsigned short int
; the U
prefix changes it to an array of char32_t
, which is an unsigned int
; and the L
prefix changes it to an array of wchar_t
which is an int
. u8
is a char
and an unprefixed string uses implementation specific encoding, which is typically the same as u8
i.e. UTF-8, of which ASCII is a subset. A raw (R
) prefix available only for string literals (and available only on GNU C (std=gnu99
onwards)) can be prefixed i.e. uR
or u8R
, but this does not influence the type.
The type of a character literal is int
unless prefixed with u
(u'a'
is unsigned short int
) or U
(U'a'
is unsigned int
). u8
and and L
are both int
when used on a character literal. An escape sequence in a string or character literal does not influence the encoding and hence the type, it's just a way of actually presenting the character to be encoded to the compiler.
The type of a complex literal 10i+1
or 10j+1
is complex int
, where both the real and the imaginary part can have a suffix, like 10Li+1
, which in this case makes the imaginary part long and the overall type is complex long int
, and upgrades the type of both the real and the imaginary part, so it doesn't matter where you put the suffix or whether you put it on both. A mismatch will always use the largest of the two suffixes as the overall type.
Using an explicit cast instead of a literal suffix always results in the correct behaviour if you use it correctly and are aware of the semantic difference that it truncates/extends (sign extends for signed
; zero extends for unsigned
– this is based on the type of the literal or expression being cast and not the type that's being cast to, so a signed int
is sign extended into an unsigned long int
) a literal to an expression of that type, rather than the literal inherently having that type.
C++
Again, the minimum default type is an int
for the smallest literal base. The literal base i.e. the actual value of the literal, and the suffix influence the final literal type according to the following table where within each box for each suffix, the order of final type is listed from smallest to largest based on the size of the actual literal base. For each suffix, the final type of the literal can only be equal to or larger than the suffix type, and based on the size of the literal base. C exhibits the same behaviour. When larger than a long long int
, depending on the compiler, __int128
is used. I think you could also create your own literal suffix operator i128
and return a value of that type.
The default type of a decimal literal is the same as C.
The type of a string literal is char []
. The type of &"string"
is const char (*) [7]
and the type of +"string"
is const char *
(in C you can only decay using "string"+0
). C++ differs in that the latter 2 forms acquire a const
but in C they don't. The string prefixes behave the same as in C
Character and complex literals behave the same as C.
pow(2000,4)
uses ..double
,2000*2000*2000*2000
usesint
.int
. 2000 is an int. Not long long int2^31 − 1
which is2,147,483,647
is smaller than 2000* 2000* 2000*2000 and since all the 2000s are int the calculation is done as an int. Not as a long long int