0

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?

Miki
  • 31
  • 7
  • This is best answered by the relevant [standard section](http://port70.net/~nsz/c/c11/n1570.html#6.4.4.1) – Eugene Sh. Dec 09 '19 at 21:05
  • @EugeneSh. - The link _seems_ to go nowhere. – ryyker Dec 09 '19 at 21:09
  • As an example, you may get a warning if you assign `0x80000000` to `uint32_t num`. One way to eliminate this is like this: `0x80000000U`. – Fiddling Bits Dec 09 '19 at 21:10
  • 1
    @ryyker Just verified. It goes straight where it should. What are you getting? – Eugene Sh. Dec 09 '19 at 21:10
  • @FiddlingBits For 32-bit `int` system `0x80000000` will have `unsigned int` type. – Eugene Sh. Dec 09 '19 at 21:14
  • 1
    When used in various expressions, the types of integer values matter—what happens when a value is shifted or used in a bit-wise complement or other operations depends on its type. The suffixes give some control over what type the value has. Assigning a value to an object does not alter the type of the object. – Eric Postpischil Dec 09 '19 at 21:14
  • 1
    @EugeneSh. - My screen froze, then got a times out message. Its on my side. Probably an over-zealous security protocol. – ryyker Dec 09 '19 at 21:22
  • Does this answer your question? [Why do we put suffixes following numeric literals?](https://stackoverflow.com/questions/40242582/why-do-we-put-suffixes-following-numeric-literals) – phuclv Dec 10 '19 at 00:18
  • so many duplicates: [what is the reason for explicitly declaring L or UL for long values](https://stackoverflow.com/q/13134956/995714), [Are literal suffixes needed in standard C?](https://stackoverflow.com/q/43193065/995714), [Why do you need to append an L or F after a value assigned to a C++ constant?](https://stackoverflow.com/q/1380653/995714), [what is the reason for explicitly declaring L or UL for long values](https://stackoverflow.com/q/13134956/995714), [Are long-suffix and unsigned-suffix needed when declaring long literals in C++?](https://stackoverflow.com/q/975942/995714) – phuclv Dec 10 '19 at 00:19

2 Answers2

3

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 now long integer or long long integer?

a is declared as a long int. Its type does not change due to any assignment.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Why do we use suffixes for integer types? Can suffixes determine the integer type?

From here:

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

  • Suffixes: They are represented in many ways according to their data types.
    1) int:- No suffix are required because integer constant are by default assigned as int data type.
    2) unsigned int: character u or U at the end of integer constant.
    3) long int: character l or L at the end of integer constant.
    4) unsigned long int: character ul or UL at the end of integer constant.
    5) long long int: character ll or LL at the end of integer constant.
    6) unsigned long long int: character ull or ULL at the end of integer constant.
ryyker
  • 22,849
  • 3
  • 43
  • 87