C - <inttypes.h> (inttypes.h)
The C <inttypes.h> header provides library support for width-based integral types. Including this header automatically includes also <stdint.h> (which defines width-based integral types).
Library Functions
Functions | Description |
---|---|
imaxabs() | Returns the absolute (positive) value of the given number. It is equivalent to abs() function for intmax_t. |
imaxdiv() | Returns quotient and remainder of integer division. It is equivalent to div() function for intmax_t. |
strtoimax() | Convert string to intmax_t. It is equivalent to strtol() function for intmax_t. |
strtoumax() | Convert string to uintmax_t. It is equivalent to strtoul() function for uintmax_t. |
wcstoimax() | Converts wide string to intmax_t. It is equivalent to wcstol() function for intmax_t. |
wcstoumax() | Converts wide string to uintmax_t. It is equivalent to wcstoul() function for uintmax_t. |
Library Types
Types | Description |
---|---|
imaxdiv_t | Structure type, return by imaxdiv() function. It is equivalent to div_t structure for intmax_t. |
Library Macros
Macro constants
Format constants for the printf() family of functions
Macros | Description |
---|---|
PRId8 PRId16 PRId32 PRId64 PRIdLEAST8 PRIdLEAST16 PRIdLEAST32 PRIdLEAST64 PRIdFAST8 PRIdFAST16 PRIdFAST32 PRIdFAST64 PRIdMAX PRIdPTR |
format conversion specifier to output a signed decimal integer value of type int8_t, int16_t, int32_t, int64_t, int_least8_t, int_least16_t, int_least32_t, int_least64_t, int_fast8_t, int_fast16_t, int_fast32_t, int_fast64_t, intmax_t, intptr_t respectively, equivalent to d for int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
PRIi8 PRIi16 PRIi32 PRIi64 PRIiLEAST8 PRIiLEAST16 PRIiLEAST32 PRIiLEAST64 PRIiFAST8 PRIiFAST16 PRIiFAST32 PRIiFAST64 PRIiMAX PRIiPTR |
format conversion specifier to output a signed decimal integer value of type int8_t, int16_t, int32_t, int64_t, int_least8_t, int_least16_t, int_least32_t, int_least64_t, int_fast8_t, int_fast16_t, int_fast32_t, int_fast64_t, intmax_t, intptr_t respectively, equivalent to i for int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
PRIu8 PRIu16 PRIu32 PRIu64 PRIuLEAST8 PRIuLEAST16 PRIuLEAST32 PRIuLEAST64 PRIuFAST8 PRIuFAST16 PRIuFAST32 PRIuFAST64 PRIuMAX PRIuPTR |
format conversion specifier to output an unsigned decimal integer value of type uint8_t, uint16_t, uint32_t, uint64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uintmax_t, uintptr_t respectively, equivalent to u for unsigned int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
PRIo8 PRIo16 PRIo32 PRIo64 PRIoLEAST8 PRIoLEAST16 PRIoLEAST32 PRIoLEAST64 PRIoFAST8 PRIoFAST16 PRIoFAST32 PRIoFAST64 PRIoMAX PRIoPTR |
format conversion specifier to output an unsigned octal integer value of type uint8_t, uint16_t, uint32_t, uint64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uintmax_t, uintptr_t respectively, equivalent to o for unsigned int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
PRIx8 PRIx16 PRIx32 PRIx64 PRIxLEAST8 PRIxLEAST16 PRIxLEAST32 PRIxLEAST64 PRIxFAST8 PRIxFAST16 PRIxFAST32 PRIxFAST64 PRIxMAX PRIxPTR |
format conversion specifier to output an unsigned lowercase hexadecimal integer value of type uint8_t, uint16_t, uint32_t, uint64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uintmax_t, uintptr_t respectively, equivalent to x for unsigned int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
PRIX8 PRIX16 PRIX32 PRIX64 PRIXLEAST8 PRIXLEAST16 PRIXLEAST32 PRIXLEAST64 PRIXFAST8 PRIXFAST16 PRIXFAST32 PRIXFAST64 PRIXMAX PRIXPTR |
format conversion specifier to output an unsigned uppercase hexadecimal integer value of type uint8_t, uint16_t, uint32_t, uint64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uintmax_t, uintptr_t respectively, equivalent to X for unsigned int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
Format constants for the scanf() family of functions
Macros | Description |
---|---|
SCNd8 SCNd16 SCNd32 SCNd64 SCNdLEAST8 SCNdLEAST16 SCNdLEAST32 SCNdLEAST64 SCNdFAST8 SCNdFAST16 SCNdFAST32 SCNdFAST64 SCNdMAX SCNdPTR |
format conversion specifier to input a signed decimal integer value of type int8_t, int16_t, int32_t, int64_t, int_least8_t, int_least16_t, int_least32_t, int_least64_t, int_fast8_t, int_fast16_t, int_fast32_t, int_fast64_t, intmax_t, intptr_t respectively, equivalent to d for int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
SCNi8 SCNi16 SCNi32 SCNi64 SCNiLEAST8 SCNiLEAST16 SCNiLEAST32 SCNiLEAST64 SCNiFAST8 SCNiFAST16 SCNiFAST32 SCNiFAST64 SCNiMAX SCNiPTR |
format conversion specifier to input a signed decimal/octal/hexadecimal integer value of type int8_t, int16_t, int32_t, int64_t, int_least8_t, int_least16_t, int_least32_t, int_least64_t, int_fast8_t, int_fast16_t, int_fast32_t, int_fast64_t, intmax_t, intptr_t respectively, equivalent to i for int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
SCNu8 SCNu16 SCNu32 SCNu64 SCNuLEAST8 SCNuLEAST16 SCNuLEAST32 SCNuLEAST64 SCNuFAST8 SCNuFAST16 SCNuFAST32 SCNuFAST64 SCNuMAX SCNuPTR |
format conversion specifier to input an unsigned decimal integer value of type uint8_t, uint16_t, uint32_t, uint64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uintmax_t, uintptr_t respectively, equivalent to u for unsigned int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
SCNo8 SCNo16 SCNo32 SCNo64 SCNoLEAST8 SCNoLEAST16 SCNoLEAST32 SCNoLEAST64 SCNoFAST8 SCNoFAST16 SCNoFAST32 SCNoFAST64 SCNoMAX SCNoPTR |
format conversion specifier to input an unsigned octal integer value of type uint8_t, uint16_t, uint32_t, uint64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uintmax_t, uintptr_t respectively, equivalent to o for unsigned int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
SCNx8 SCNx16 SCNx32 SCNx64 SCNxLEAST8 SCNxLEAST16 SCNxLEAST32 SCNxLEAST64 SCNxFAST8 SCNxFAST16 SCNxFAST32 SCNxFAST64 SCNxMAX SCNxPTR |
format conversion specifier to input an unsigned hexadecimal integer value of type uint8_t, uint16_t, uint32_t, uint64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uintmax_t, uintptr_t respectively, equivalent to x for unsigned int. These width-based integral types are defined in <stdint.h> header which is included in this header. |
Example:
The example below describes how to use these macro constants with printf() and scanf() functions.
#include <stdio.h> #include <inttypes.h> int main (){ int64_t x, y; printf("Please, enter a number: "); scanf("%" SCNd64, &x); printf("Please, enter another number: "); scanf("%" SCNd64, &y); printf("%" PRId64 " by %" PRId64 " gives %" PRId64, x, y, x*y); return 0; }
The possible output of the above code will be:
Please, enter a number: 10 Please, enter another number: 15 10 by 15 gives 150
Example:
Lets assume that we have a file called test.txt in the current working directory. In the example below, these macro constants is used with fprintf() function to write the formatted string in this file.
#include <stdio.h> #include <inttypes.h> int main (){ //open the file in write and read mode FILE *pFile = fopen("test.txt", "w+"); fprintf(pFile, "Decimals: %" PRId16 " %" PRId16 " \n", 200, 300); fprintf(pFile, "More Decimals: %" PRId64 " %" PRId64 " \n", 20000L, 30000L); fprintf(pFile, "Octals: %" PRIo16 " %#" PRIo16 " \n", 100, 100); fprintf(pFile, "Hexadecimals: %" PRIx16 " %#" PRIx16 " %" PRIX16 " %#" PRIX16 " \n", 100, 100, 100, 100); fprintf(pFile, "Padded number = %05" PRId16 " \n", 89); fprintf(pFile, "Number with Width = %*" PRId16 " \n", 5, 89); //set the position to the start rewind(pFile); //display the content of the file int c = getc(pFile); while(c != EOF) { putchar(c); c = getc(pFile); } //close the file fclose (pFile); return 0; }
The output of the above code will be:
Decimals: 200 300 More Decimals: 20000 30000 Octals: 144 0144 Hexadecimals: 64 0x64 64 0X64 Padded number = 00089 Number with Width = 89