Languages/C/inttypes

From UIT
(Difference between revisions)
Jump to: navigation, search
(Compiler support & remarks: added msvc++ stdint.h file)
(Integer types in C (and C++))
 
(7 intermediate revisions by one user not shown)
Line 1: Line 1:
 
{{TOC right}}
 
{{TOC right}}
  
= Integer types in C =
+
= Integer types in C (and C++) =
The C language defines a lot of integer types (<code>char</code>, <code>short</code>, <code>int</code>, <code>unsigned int</code>,...)
+
The C language defines a lot of integer types (<code>char</code>, <code>short</code>, <code>int</code>, <code>unsigned int</code>, <code>long</code>, <code>long long</code>,...)
 
but unfortunately their size are implementation dependent (vendor/platform/compiler).
 
but unfortunately their size are implementation dependent (vendor/platform/compiler).
  
Line 13: Line 13:
 
{|class=wikitable
 
{|class=wikitable
 
|-
 
|-
| C99 type || Old HEVs type || size (bits) || minimum value (define) || minimum value (numeric) || maximum value (define) || maximum value (numeric)
+
! C99 type || (Old) HEVs type || size (bits) || minimum value (define) || minimum value (numeric) || maximum value (define) || maximum value (numeric)
 
|-
 
|-
 
| uint8_t  || UINT8        || 8          || -                      || 0                      || UINT8_MAX              || 255
 
| uint8_t  || UINT8        || 8          || -                      || 0                      || UINT8_MAX              || 255
Line 26: Line 26:
 
| int8_t  || INT8          || 8          || INT8_MIN              || -128                      || UINT8_MAX              || 127
 
| int8_t  || INT8          || 8          || INT8_MIN              || -128                      || UINT8_MAX              || 127
 
|-
 
|-
| int16_t || INT16          || 16          || INT16_MIN              || -3'2768                   || UINT16_MAX            || 32'767
+
| int16_t || INT16          || 16          || INT16_MIN              || -32'768                   || UINT16_MAX            || 32'767
 
|-
 
|-
 
| int32_t || INT32          || 32          || INT32_MIN              || -2'147'483'648            || UINT32_MAX            || 2'147'483'647
 
| int32_t || INT32          || 32          || INT32_MIN              || -2'147'483'648            || UINT32_MAX            || 2'147'483'647
 
|-
 
|-
| int64_t || INT64          || 64          || INT64_MIN              || -9'223'372'036'854'775'807 || UINT64_MAX            || 9'223'372'036'854'775'807
+
| int64_t || INT64          || 64          || INT64_MIN              || -9'223'372'036'854'775'808 || UINT64_MAX            || 9'223'372'036'854'775'807
 
|-  
 
|-  
 
|}
 
|}
 +
 +
== Use in <code>#define</code> ==
 +
You can force the use of a type for a define, by using those macros:
 +
[U]INT[8/16/32/64]_C()
 +
 +
example : <code>#define TOTO UINT32_C(12)</code> for defining a unsigned 32 bit constant name TOTO of value 12.
 +
 +
== Use in <code>printf</code>-like functions ==
 +
The <inttypes.h> header defines macro for printf and scanf functions, for instance:
 +
<source lang="C">
 +
#include <stdint.h>
 +
#include <inttypes.h>
 +
 +
uint32_t a = 12;
 +
 +
printf("a = %d\n", a); /* <- This will generate a warning */
 +
printf("a = %"PRIu32"\n", a); /* <- here is the correct way */
 +
 +
</source>
  
 
== C++ support ==
 
== C++ support ==
 
C++ users should use <stdint.h>
 
C++ users should use <stdint.h>
 +
 
C++11 will introduce the <cstdint> header.
 
C++11 will introduce the <cstdint> header.
  
Line 43: Line 63:
  
 
=== HI TECH PIC18 C compiler ===
 
=== HI TECH PIC18 C compiler ===
Since version 9.65, this compiler has the <sdtint.h> file.
+
Since version 9.65, this compiler supports <stdint.h>, without the <code>(u)int64_t</code>.
But there is no <code>(u)int64_t</code>.
+
  
 
=== Microchip XC8 compiler ===
 
=== Microchip XC8 compiler ===
 
This compiler has the <sdtint.h> file.
 
This compiler has the <sdtint.h> file.
But there is no <code>(u)int64_t</code>.
+
Since version 9.65, this compiler supports <stdint.h>, without the <code>(u)int64_t</code>.
 +
 
 +
=== IAR - MSP430 ===
 +
This compiler supports <stdint.h>.
  
 
=== Microsoft Visual C++ ===
 
=== Microsoft Visual C++ ===
 
Here is a suitable [http://msinttypes.googlecode.com/svn/trunk/stdint.h stdint.h] file.
 
Here is a suitable [http://msinttypes.googlecode.com/svn/trunk/stdint.h stdint.h] file.
 +
 +
== Bit shift operators and signed integers ==
 +
 +
What is the value of <code>((int32_t)-1) >> 1</code> and <code>((int32_t)-1) << 1</code> ?
 +
-> The result is implementation dependent.
 +
 +
Conclusion : Don't use shift operators on signed integers!
  
  
 
[[Category:Languages]] [[Category:C]]
 
[[Category:Languages]] [[Category:C]]

Latest revision as of 14:11, 12 February 2013

Contents

Integer types in C (and C++)

The C language defines a lot of integer types (char, short, int, unsigned int, long, long long,...) but unfortunately their size are implementation dependent (vendor/platform/compiler).

Writing programs in a portable manner requires the use of known size integers.

C99 integer sizes

Recent compilers provide the <stdint.h> file that defines a lot of integer types, and because this is a standard, new code should no more use the "old" HEVs coding style.

C99 type (Old) HEVs type size (bits) minimum value (define) minimum value (numeric) maximum value (define) maximum value (numeric)
uint8_t UINT8 8 - 0 UINT8_MAX 255
uint16_t UINT16 16 - 0 UINT16_MAX 65'535
uint32_t UINT32 32 - 0 UINT32_MAX 4'294'967'295
uint64_t UINT64 64 - 0 UINT64_MAX 18'446'744'073'709'551'615
int8_t INT8 8 INT8_MIN -128 UINT8_MAX 127
int16_t INT16 16 INT16_MIN -32'768 UINT16_MAX 32'767
int32_t INT32 32 INT32_MIN -2'147'483'648 UINT32_MAX 2'147'483'647
int64_t INT64 64 INT64_MIN -9'223'372'036'854'775'808 UINT64_MAX 9'223'372'036'854'775'807

Use in #define

You can force the use of a type for a define, by using those macros: [U]INT[8/16/32/64]_C()

example : #define TOTO UINT32_C(12) for defining a unsigned 32 bit constant name TOTO of value 12.

Use in printf-like functions

The <inttypes.h> header defines macro for printf and scanf functions, for instance:

#include <stdint.h>
#include <inttypes.h>
 
uint32_t a = 12;
 
printf("a = %d\n", a);		/* <- This will generate a warning */
printf("a = %"PRIu32"\n", a);	/* <- here is the correct way */

C++ support

C++ users should use <stdint.h>

C++11 will introduce the <cstdint> header.

Compiler support & remarks

GCC

gcc fully supports <stdint.h>.

HI TECH PIC18 C compiler

Since version 9.65, this compiler supports <stdint.h>, without the (u)int64_t.

Microchip XC8 compiler

This compiler has the <sdtint.h> file. Since version 9.65, this compiler supports <stdint.h>, without the (u)int64_t.

IAR - MSP430

This compiler supports <stdint.h>.

Microsoft Visual C++

Here is a suitable stdint.h file.

Bit shift operators and signed integers

What is the value of ((int32_t)-1) >> 1 and ((int32_t)-1) << 1 ? -> The result is implementation dependent.

Conclusion : Don't use shift operators on signed integers!

Personal tools
Namespaces
Variants
Actions
Navigation
Browse
Toolbox