Why do we use suffixes for integer types? Can suffixes determine the integer type?
e.g.
long int a;
a = 50000LL;
Is variable a now long integer or long long integer?
Why do we use suffixes for integer types? Can suffixes determine the integer type?
e.g.
long int a;
a = 50000LL;
Is variable a now long integer or long long integer?
Why do we use suffixes for integer types?
To define the minimum width type and/or make unsigned the constant.
With bit shifting (Assume 32-bit int
) 1u << 31
is good. 1 << 31
is UB (shifting into sign bit). @Eugene Sh.
To form desired constants:
// Assume `long` is 32-bit
long long x = 12345678912345L; // Constant is LL due to range.
long long y = 1000LL * 1000 * 1000 * 1000; // y gets the expected value
long long z = 1000L * 1000 * 1000 * 1000; // UB due to long overflow.
Can suffixes determine the integer type?
Yes. A u
indicates some unsigned type. Useful also with macro processing.
l
, ll
indicate at least a long
, long long
type. Not useful with macro processing as preprocessor arithmetic done in intmax_t/uintmax_t
regardless of the l, ll
suffix.
Is variable
a
nowlong
integer orlong long
integer?
a
is declared as a long int
. Its type does not change due to any assignment.
Why do we use suffixes for integer types? Can suffixes determine the integer type?
...Integer literal is a type of literal for an integer whose value is directly represented in source code...
Prefixes: They are basically represent in four types.
1) Decimal-literal(base 10):- a non-zero decimal digit followed by zero or more decimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9). For example, 56, 78.
2) Octal-literal(base 8) :- a zero followed by zero or more octal digits(0, 1, 2, 3, 4, 5, 6, 7). For example, 045, 076, 06210.
3) Hex-literal(base 16) :- 0x or 0X followed by one or more hexadecimal digits(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F). For example, 0x23A, 0Xb4C, 0xFEA.
4) Binary-literal(base 2) :- 0b or 0B followed by one or more binary digits(0, 1). For example, 0b101, 0B111.