[SOLVED] int or uint8_t

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I would like to know personally what do you use int or uint8_t in c program. I have seen in my research that unit_8t is used by most of the people but no idea why do they use it instead of int

Warm regards
Pushkar
 
Last edited:

Deleted member 115935

Joined Dec 31, 1969
0
I would like to know personally what do you use int or uint8_t in c program. I have seen in my research that unit_8t is used by most of the people but no idea why do they use it instead of int

Warm regards
Pushkar
Its all down to how much you want to control whats happening,

int is machine / compiler dependent,
where as you have uint8 defined exactly

get inot habit of being specif and exact, and you wont go wrong
 

click_here

Joined Sep 22, 2020
548
In the limits.h header file there are minimum sizes of variable types.

Section 5.2.4.2.1 of the C99 says that no char can be less than 8 bits wide (it can be larger), so that is fine to use for int8_t.

[edit]
i.e.
Code:
char apple;
// vs
int8_t banana;
[/edit]

The reason you would use intN_t is if the size of the variable is important to the self documenting of the code.

Some other good data types to look into are (from C99 standards: 7.18.1 Integer types):
int_least8_t -
"The typedef name int_leastN_t designates a signed integer type with a width of at least N, such that no signed integer type with lesser size has at least the specified width.
Thus, int_least32_t denotes a signed integer type with a width of at least 32 bits."

int_fast8_t -
"The typedef name int_fastN_t designates the fastest signed integer type with a width of at least N. The typedef name uint_fastN_t designates the fastest unsigned integer
type with a width of at least N"
 
Last edited:

click_here

Joined Sep 22, 2020
548
I should probably also add that intN_t and uintN_t (int sizes of exact width) are not compulsory in a C compiler, but int_leastN_t and int_fastN_t are (C99 7.18.1.1 Exact-width integer types)
 

Ian Rogers

Joined Dec 12, 2012
1,136
@Pushkar1 ... In simple terms, UINT8_T is defined for simplicity.. It just means Unsigned 8 bit integer.

"int" is very abstract nowadays... Some compilers default to unsigned 8 bit integer and others to signed 8 bit integer.

People, cleverer than us, give us the "types" header so we can all sing from the same song sheet!

Usage....
Signed 8 bit means you can represent numbers from -128 right through to +127..
Un Signed 8 bit means you can represent numbers from 0 right through to +255..

If you need to count to 200 and you use a signed int8_t, when you get to 127, the count fails and goes to -128. so the program has a bug..

"Int" just means integer ( A whole number ) and can be 8,16,32,64 or more bits in length.. This is why uint8 , uint16, uint32 and int8, int16, int32, are defined so mistakes are fewer..
 

click_here

Joined Sep 22, 2020
548
Also, an "int" should be compared to int16_t, because the minimum size of an "int" is 16 bits - It can be larger, but not smaller.

As I said earlier, int8_t is more comparible to a "char", because of its allowable minimum size
 

dl324

Joined Mar 30, 2015
18,226
I would like to know personally what do you use int or uint8_t in c program.
You need to be more specific. The size of int is machine dependent and is a signed quantity. uint*_t is unsigned.

I find my code to be clearer if I use uint*_t instead of unsigned int, unsigned long int, or unsigned long long int because they're at least 16, 32, and 64 bits, respectively, but could be more.
 
Top