Format specifiers for uint8_t, uint16_t, ...?

C++C++11Scanf

C++ Problem Overview


If I have an integer variable I can use sscanf as shown below by using the format specifier %d.

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

Where can I find format specifiers for uint8_t, uint16_t, uint32_t and uint64_t?

uint64_t has probably %lu.

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

As others said, include <stdint.h> header that defines the format macros. In C++, however, define __STDC_FORMAT_MACROS prior to including it. From stdint.h:

/* The ISO C99 standard specifies that these macros must only be
   defined if explicitly requested.  */
#if !defined __cplusplus || defined __STDC_FORMAT_MACROS

Solution 3 - C++

According to 7.19.6 Formatted input/output functions of ISO/IEC 9899:TC2, there are no such format specifiers (so I doubt there any for C++2003). Even though there are some #define-macros available in C99's inttypes.h, cinttypes and inttypes.h are not part of the current standard. Of course, fixed-size integer types are non-standard as well.

Anyways, I seriously recommemend using streams instead:

<any_type> x;
f >> x;

and be done. E.g.:

std::stringstream ss;
uint32_t u;
std::cin >> u;

This has the advantage that one time in the future, changing the type of the variable does not cause a cascade of subtle bugs and undefined behaviour.

Solution 4 - C++

In C, the header is <inttypes.h>, and formats such as SCNX8, SCNd16.

The same header probably works for C++ too.

Solution 5 - C++

The right format to read a uint64_t (typedef unsigned long long int) is, with scanf and not sscanf, "%" SCNu64 and for print is also SCNu64 Example. in your code you read for example my_integer variable, then you do scanf ("Value of integer:%" SCNu64, & my_integer); and to write the same but with printf.

Solution 6 - C++

You can check types in <cstdint> for C++ or in <types.h> for C. Then you can specify format which is currently compiled on your machine. If variable is unsigned then you need to use %u, if variable is signed, then use %d. For variables size lower than 32 bytes you don't have to specify long prefix.

In my configuration I have 64 bit program, so my uint64_t is compiled as unsigned long long int, so it means to use %llu. If your program will be compiled in 32 bit you can use %lu because uint64_t will be compiled as unsigned long int

But who is using x86 still? :), so generally:

uint8_t, uint16_t, uint32_t - %u
uint64_t - %llu
int8_t, int16_t, int32_t - %d
int64_t -%lld

Solution 7 - C++

Refer to this for sscanf usage.

These datatypes are defined in stdint.h. Refer here for stdint.h

Shash

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUsmanView Question on Stackoverflow
Solution 1 - C++AProgrammerView Answer on Stackoverflow
Solution 2 - C++Maxim EgorushkinView Answer on Stackoverflow
Solution 3 - C++Sebastian MachView Answer on Stackoverflow
Solution 4 - C++Jonathan LefflerView Answer on Stackoverflow
Solution 5 - C++Alejandro CaroView Answer on Stackoverflow
Solution 6 - C++Paweł IwaneczkoView Answer on Stackoverflow
Solution 7 - C++Shash316View Answer on Stackoverflow