Have you ever wondered why people uses different keywords to define the same variable in C or C++ program?
Sometimes you would have noticed that 8 bit variable has been defined as
unsigned char a;
uint8_t b;
Also, many a times you would notice integer has been defined as
unsigned int c;
uint16_t d;
uint32_t e;
Initially when I was in the learning phase of C programming with microcontrollers, I used to get confused, what to use when.
Sometimes you would have noticed that 8 bit variable has been defined as
unsigned char a;
uint8_t b;
Also, many a times you would notice integer has been defined as
unsigned int c;
uint16_t d;
uint32_t e;
Initially when I was in the learning phase of C programming with microcontrollers, I used to get confused, what to use when.
Fixed width integers
The keywords like uint8_t, int8_t and uint16_t, all are called fixed width integers. These keywords contain numbers in their keyword, which defines the width of a variable in bytes.
E. g.
- uint8_t defines 8 bits width of variable b(in the above example).
- uint16_t defines 16 bits width of variable d(in the above example).
- uint32_t defines 32 bits width of variable e(in the above example).
The support for uint8_t, uint16_t and uint32_t has been added from C11 standard(C language standard).
Advantage
Fixed width integers helps in eliminating the confusion in assumption of the width of variable.
I will explain how it is confusing.
Different C compilers designed and optimized for different architectures of microprocessor or microcontrollers.
Generally if microcontroller have 8 or 16 bit architecture, the width of the integer would be by default 16 bit in its C compiler and if microcontroller have 32 bit architecture, width would be 32 bits. Now when I will try to run the same program on different architectures, it might create unexpected results.
When I define unsigned int c in the program for PIC18 microcontroller, compiler by default will assign 2 bytes to variable c. But when I will use same declaration in ARM based microcontroller, the compiler by default will assign 4 bytes to variable c.
When I define unsigned int c in the program for PIC18 microcontroller, compiler by default will assign 2 bytes to variable c. But when I will use same declaration in ARM based microcontroller, the compiler by default will assign 4 bytes to variable c.
Now when you define uint8_t or int8_t, it states that the width of a variable is strictly 8 bits only in any architecture, may it be 8/16/32 bits. This will make your program easily portable across different microcontrollers.
Backward Compatibility
Sometimes, a C compiler is modified from C standard library earlier than C11, for example C99, they define typedef for the new keyword for compatibility purpose. They are mostly defined in stdint.h or cstdint.h
typedef uint8_t unsigned char
Available Fixed width integers
Keyword | Description |
---|---|
int8_t | Signed integer type with width of exactly 8 bit |
int16_t | Signed integer type with width of exactly 16 bit |
int32_t | Signed integer type with width of exactly 32 bit |
int64_t | Signed integer type with width of exactly 64 bit |
int_fast8_t | Fastest signed integer type with width of at least 8 bits |
int_fast16_t | Fastest signed integer type with width of at least 16 bits |
int_fast32_t | Fastest signed integer type with width of at least 32 bits |
int_fast64_t | Fastest signed integer type with width of at least 64 bits |
int_least8_t | Smallest signed integer type with width of at least 8 bits |
int_least16_t | Smallest signed integer type with width of at least 16 bits |
int_least32_t | Smallest signed integer type with width of at least 32 bits |
int_least64_t | Smallest signed integer type with width of at least 64 bits |
intmax_t | Maximum width integer type |
intptr_t | Integer type capable of holding a pointer |
uint8_t | Unsigned integer type with width of exactly 8 bits |
uint16_t | Unsigned integer type with width of exactly 16 bits |
uint32_t | Unsigned integer type with width of exactly 32 bits |
uint64_t | Unsigned integer type with width of exactly 64 bits |
uint_fast8_t | Fastest unsigned integer type with width of at least 8 bits |
uint_fast16_t | Fastest unsigned integer type with width of at least 16 bits |
uint_fast32_t | Fastest unsigned integer type with width of at least 32 bits |
uint_fast64_t | Fastest unsigned integer type with width of at least 64 bits |
uint_least8_t | Smallest unsigned integer type with width of at least 8 bits |
uint_least16_t | Smallest unsigned integer type with width of at least 16 bits |
uint_least32_t | Smallest unsigned integer type with width of at least 32 bits |
uint_least64_t | Smallest unsigned integer type with width of at least 64 bits |
uintmax_t | Maximum width unsigned integer type |
uintptr_t | Unsigned integer type capable of holding a pointer |
If you like the content, like our facebook page.
No comments:
Post a Comment