Format Specifiers for Uint8_T, Uint16_T, ...

Why is the format specifier for uint8_t and uint16_t the same (%u)?

printf() is a variadic function. Its optional arguments( and only those ) get promoted according to default argument promotions( 6.5.2.2. p6 ).

Since you are asking for integers, integer promotions are applied in this case, and types you mention get promoted to int. ( and not unsigned int because C )

If you use "%u" in printf(), and pass it an uint16_t variable, then the function converts that to an int, then to an unsigned int( because you asked for it with %u ) and then prints it.

Format specifiers for uint8_t, uint16_t, ...?

They are declared in <inttypes.h> as macros: SCNd8, SCNd16, SCNd32 and SCNd64.
Example (for int32_t):

sscanf (line, "Value of integer: %" SCNd32 "\n", &my_integer);

Their format is PRI (for printf)/SCN (for scan) then o, u, x, X d, i for the corresponding specifier then nothing, LEAST, FAST, MAX then the size (obviously there is no size for MAX). Some other examples: PRIo8, PRIuMAX, SCNoFAST16.

Edit: BTW a related question asked why that method was used. You may find the answers interesting.

printing the uint8_t

You need to construct a format string that's suitable. The printf() function has no way of printing an array in one go, so you need to split it and print each uint8_t:

__printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
orig[0] & 0xff, orig[1] & 0xff, orig[2] & 0xff,
orig[3] & 0xff, orig[4] & 0xff, orig[5] & 0xff);

The & 0xff is to ensure onlu 8 bits is sent to printf(); they shouldn't be needed for an unsigned type like uint8_t though so you can try without too.

This assumes a regular 48-bit MAC, and prints using the conventional colon-separated hex style.

C++ Best Practice: function to accept uint8_t, uint16_t, uint32_t, float

The call is ambiguous because the compiler doesn't know which version of the function you want to call. For example, usage like this:

write(1, 2, 3);

What is the 3? It could be uint8_t or uint16_t or the others ...

Instead of guessing, and potentially doing some crazy things, the compiler tells you it can't know. You can solve this in several ways:

  1. Define distinct names for your functions eg. writeUInt8t(... , uint8_t in)
  2. Specify exactly which to call in your use of the function: write(1, 2, static_cast<uint8_t>(3))

How do I print uint32_t and uint16_t variables' value?

You need to include inttypes.h if you want all those nifty new format specifiers for the intN_t types and their brethren, and that is the correct (ie, portable) way to do it, provided your compiler complies with C99. You shouldn't use the standard ones like %d or %u in case the sizes are different to what you think.

It includes stdint.h and extends it with quite a few other things, such as the macros that can be used for the printf/scanf family of calls. This is covered in section 7.8 of the ISO C99 standard.

For example, the following program:

#include <stdio.h>
#include <inttypes.h>
int main (void) {
uint32_t a=1234;
uint16_t b=5678;
printf("%" PRIu32 "\n",a);
printf("%" PRIu16 "\n",b);
return 0;
}

outputs:

1234
5678

Print out the value uint8_t *

quality is a pointer, or like an array, if you want to print the value that points to you need to specify it. with the index or dereferencing it:

printf("quality: %d", *(dataIndPtr->quality));

Using the zero index like if it was an array should also print the value:

printf("quality: %d", dataIndPtr->quality[0]);

Or if what you want is print the value of the pointer itself then Michal's answer is what you want.



Related Topics



Leave a reply



Submit