Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of "Macros for minimum-width integer constants"

Tags:

c

integer

c99

In C99 standard Section 7.18.4.1 "Macros for minimum-width integer constants", some macros defined as [U]INT[N]_C(x) for casting constant integers to least data types where N = 8, 16, 32, 64. Why are these macros defined since I can use L, UL, LL or ULL modifiers instead? For example, when I want to use at least 32 bits unsigned constant integer, I can simply write 42UL instead of UINT32_C(42). Since long data type is at least 32 bits wide it is also portable.

So, what is the purpose of these macros?

like image 863
obareey Avatar asked May 03 '13 14:05

obareey


People also ask

What is Stdint H used for?

h is a header file in the C standard library introduced in the C99 standard library section 7.18 to allow programmers to write more portable code by providing a set of typedefs that specify exact-width integer types, together with the defined minimum and maximum allowable values for each type, using macros .

What are fixed width integer types?

The fixed-width integer types that <inttypes. h> provides, include signed integer types, such as int8_t, int16_t, int32_t, int64_t, and unsigned integer types, such as uint8_t, uint16_t, uint32_t, and uint64_t.

What is int_fast16_t?

int_fast16_t is most efficient type in speed with at least the range of a 16 bit int. Example: A given platform may have decided that int should be 32-bit for many reasons, not only speed. The same system may find a different type is fastest for 16-bit integers.

Which of the following data types are standard integer types defined in the Stdint H header file?

h — Integer types. The stdint. h header defines integer types, limits of specified width integer types, limits of other integer types and macros for integer constant expressions.


1 Answers

You'd need them in places where you want to make sure that they don't become too wide

#define myConstant UINT32_C(42)

and later

printf( "%" PRId32 " is %s\n", (hasproperty ? toto : myConstant), "rich");

here, if the constant would be UL the expression might be ulong and the variadic function could put a 64bit value on the stack that would be misinterpreted by printf.

like image 69
Jens Gustedt Avatar answered Oct 26 '22 20:10

Jens Gustedt