Quantcast
Viewing latest article 11
Browse Latest Browse All 222

Answer by Peter - Reinstate Monica for What is the integer literal suffix 'i' in C?

I have been programming C for almost 40 years now and must admit: This question caught me off-guard.

There is no integer suffix.

In 6.4.4.1, "Integer Constants", of the C23 draft, the syntax of an integer constant is given as

decimal-constant integer-suffixopt

The suffix is defined as

integer-suffix:

                  unsigned-suffix long-suffixopt

                  unsigned-suffix long-long-suffix

                  unsigned-suffix bit-precise-int-suffix

                  long-suffix unsigned-suffixopt

                  long-long-suffix unsigned-suffixopt

                  bit-precise-int-suffix unsigned-suffixopt

You can see that a "normal plain int suffix" is not among them. An int constant is a decimal integer literal whose value x satisfies INT_MIN <= x <= INT_MAX.

The reason is certainly that int is the "normal case"; only special cases would need a syntax.

That is quite unsatisfying because, say, the type of 32768 is implementation specific. On machines with 16 bit ints, it would have type long int. In that case, e.g. printf("%d", 32768) would be wrong.

Admittedly, that isn't as bad as in C++ where function overloading can make the specific type more important. And of course, you can always write (int)32768 (but I suppose many compilers would not warn of a narrowing conversion here because with an explicit cast, the programmer is responsible).


Viewing latest article 11
Browse Latest Browse All 222

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>