Our website uses cookies to enhance your browsing experience.
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at

If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

Intmax_t / uintmax_t

intmax_t / uintmax_t

Jul 24 2015

Imagine the following situation. We are working with some variable "var" of unsigned integer type defined by the programmer.

mytype_t var;

The variable length is unknown or can change depending on the compiler implementation. Our task is to correctly print this variable's value using the printf function. What output modifier should we use then? What about "llu", just to be sure?

printf("%llu", (unsigned long long)var);

But what if this variable's type is larger than unsigned long long and has no associated output modifier? This is where uintmax_t comes to help.

Under the language standard, the intmax_t and uintmax_t data types are, respectively, signed and unsigned integer types with the largest length possible. They can be represented through extended integer types. Section of the Standard only requires that intmax_t and uintmax_t should be large enough to store values represented by any other integer data types. Like extended integer types, they are defined in the stdint.h header file together with their smallest and largest values INTMAX_MIN, INTMAX_MAX, and UINTMAX_MAX. For intmax_t and uintmax_t, the "j" letter serves as an input/output modifier. Note also that Visual Studio 2012 and earlier versions don't support it. Since any unsigned integer value can fit into uintmax_t, casting any integer type to this one will guarantee keeping the saved value unchanged. So the correct printing of the "var" variable will look like this:

printf("%ju", (uintmax_t) var);

It's the same with the "scanf" function.

mytype_t var;
scanf("%llu", &var);

This code can cause incorrect value reading if mytype_t is larger than unsigned long long or an overflow of the "var" variable if mytype_t is less than unsigned long long. Correct reading can be ensured in the following way:

mytype_t var;
uintmax_t temp;
scanf("%ju", &temp);
if(temp <= MYTYPE_MAX)
  var = temp;

There is one intricate thing about it, though. Some readers using __int128 or its unsigned counterpart may wonder why intmax_t is defined as long long in my clang or gcc compiler, while its size is actually less than that of __int128. You see, neither clang nor gcc treat __int128 as an extended integer type because that would imply changing the intmax_t type, which in its turn would break the ABI-compatibility with other applications.

Imagine you have a program using a function with an intmax_t parameter in a dynamic library. If the compiler changes the value of intmax_t and recompiles the program, the program and the library will start referring to different types, thus breaking the binary compatibility.

In the final analysis, the intmax_t/uintmax_t types don't quite match the purposes specified in the standard.


Popular related articles

Comments (0)

Next comments next comments
close comment form