9

As far as I know the suffix t in uint32_t denote type name but I wonder to know what is the C in UINT32_C and what is the differences?

Mohammad Yousefi
  • 563
  • 2
  • 7
  • 21
  • `uint32_t` is a standard [fixed-width integer type](http://en.cppreference.com/w/c/types/integer), and is defined in the `` system header file. Because it's a standard type, it's portable between platforms. – Some programmer dude Aug 02 '17 at 06:59

5 Answers5

14

UINT32_C is a macro which defines integer constant of type uint_least32_t. For example:

UINT32_C(123) // Might expand to 123UL on system where uint_least32_t is unsigned long
              // or just 123U, if uint_least32_t is unsigned int.

7.20.4.1 Macros for minimum-width integer constants

  1. The macro INTN_C(value) shall expand to an integer constant expression corresponding to the type int_leastN_t. The macro UINTN_C(value) shall expand to an integer constant expression corresponding to the type uint_leastN_t. For example, if uint_least64_t is a name for the type unsigned long long int, then UINT64_C(0x123) might expand to the integer constant 0x123ULL.

It is thus possible that this constant is more than 32 bits on some rare systems.

But if you are on a system where multiple of 8-bits 2's complement types are defined (most modern systems), and uint32_t exists, then this creates 32-bit constant.

They all are defined in stdint.h, and have been part of the C standard since C99.

user694733
  • 15,208
  • 2
  • 42
  • 68
8

UINT32_C is a macro for writing a constant of type uint_least32_t. Such a constant is suitable e.g. for initializing an uint32_t variable. I found for example the following definition in avr-libc (this is for the AVR target, just as an example):

#define     UINT32_C(value)   __CONCAT(value, UL)

So, when you write

UINT32_C(25)

it's expanded to

25UL

UL is the suffix for an unsigned long integer constant. The macro is useful because there is no standard suffix for uint32_t, so you can use it without knowing that on your target, uint32_t is a typedef e.g. for unsigned long. With other targets, it will be defined in a different way.

  • @JonathanLeffler my bad, I checked the standard, but only searching for the exact identifier. Will update the answer. –  Aug 02 '17 at 07:10
  • Exactly. It is standard, but IMO useless – 0___________ Aug 02 '17 at 07:10
  • @PeterJ I tried to explain how it is useful (and thinking about a compiler targeting 8bit µCs, I think it really is), but I indeed never needed something like this before. –  Aug 02 '17 at 07:12
  • Have you seen anyone using it even on the 8bit platforms.? It is better to write numbers yourself. This makes the code unreadable as well - or maybe less than the "normal" numbers with the suffix – 0___________ Aug 02 '17 at 07:18
  • 1
    @PeterJ: Imagine you write code you want to compile an a 64bit *LP64* target as well as on a 8bit target. The 8bit target will have 16bit `int`, so a constant larger than `32767` will not compile without a suffix. But the compiler for LP64 target will compile the constant with `UL` suffix into a 64bit value, not what you want. –  Aug 02 '17 at 07:21
  • @Felix Palmen code which will compile and run on 8 & 64 bit platforms is not worth touching. 8 bit platforms require a very specific programming as any longer than 8 bits math operations are extremely expensive. Remember - 8 bit targets often do not have even hardware multiplication – 0___________ Aug 02 '17 at 07:26
  • @PeterJ But you know that one design goal of C is to actually *abstract* from the hardware? I agree a situation needing those macros is probably rare (therefore this made-up example), but they are there to achieve exactly this abstraction properly. –  Aug 02 '17 at 07:27
  • 2
    @PeterJ - It is most certainly not useless. Sure you won't use it to initialize a variable, but what about passing an argument to a function chosen by `_Generic` selection? The type of the constant will have great effect. – StoryTeller - Unslander Monica Aug 02 '17 at 07:29
  • @Felix Palmen Maybe C theoreticians never had to program 8 bit targets, and their knowledge is nomen omen theoretical, with no practical value – 0___________ Aug 02 '17 at 07:30
  • @StoryTeller same useless feature. Potential source of very difficult to locate errors. Added probably to be like C++, and say : `you can almost overload in C too`. If you need overloading write in C++ – 0___________ Aug 02 '17 at 07:34
  • 1
    @PeterJ - I take it you don't like the ISO C committee much. – StoryTeller - Unslander Monica Aug 02 '17 at 07:36
  • @StoryTeller they are not fixing issues, and try to make from C a different language. Useless. Power of is C is its simplicity, not too much abstraction. I actively use both C & C++ and do not see any point of implementing C++ like features in C. _Generic is one of the examples. – 0___________ Aug 02 '17 at 07:45
3

These constants are defined something like this:

#define UINT32_C(value) (value##UL)

You can only put constant values as macro argument, otherwise it wont compile.

UINT32_C(10); // compiles

uint32_t x = 10;
UINT32_C(x); // does not compile
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
2

Don't know about keil, but at least in Linux UINT32_C is a macro to create a uint32_t literal.

And as mentioned by others, uint32_t is a type defined as of C99 in stdint.h.

janneb
  • 36,249
  • 2
  • 81
  • 97
-2

It is the macro appending the suffix creating the literal for example #define UINT32_C(c) c##UL

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Interesting minus - what is wrong with this answer or maybe it is for example the revenge one – 0___________ Aug 02 '17 at 13:14
  • This answer simple says the macro appends a suffix. No rational is provided for what suffix, no mention of the resultant type, no mention that the value needs to be such that it is suffix-able. Minor: In C, a constant is created, not a _literal_. In C, its literals (string, compound) can have their address taken - constants not so. – chux - Reinstate Monica Aug 02 '17 at 15:41
  • integer literal is in the common use. I do not think that anyone will be confused by this. See the answer below given by janneb. Another example https://www.tutorialspoint.com/cprogramming/c_constants.htm. http://en.cppreference.com/w/cpp/language/integer_literal or https://en.wikipedia.org/wiki/Integer_literal. – 0___________ Aug 02 '17 at 16:12
  • @chux and in the formal language theory a literal is a notation for representing a fixed value in source code. The C standard uses term `constant` IMO improperly, not as it is usually understood in the computer science. – 0___________ Aug 02 '17 at 16:24