To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
* By clicking this button you agree to our Privacy Policy statement

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

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

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

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
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →