Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is uint_8 etc. used in C/C++?

Tags:

c++

c

I've seen some code where they don't use primitive types int, float, double etc. directly. They usually typedef it and use it or use things like uint_8 etc.

Is it really necessary even these days? Or is C/C++ standardized enough that it is preferable to use int, float etc directly.

like image 822
user855 Avatar asked Feb 20 '11 03:02

user855


People also ask

Why do we use Uint8?

The uint8 class is primarily meant to store integer values. Most operations that manipulate arrays without changing their elements are defined (examples are reshape , size , subscripted assignment and subscripted reference).

What does Uint8 mean in C?

A UINT8 is an 8-bit unsigned integer (range: 0 through 255 decimal).

Should I use Uint8?

If the intended use of the variable is to hold an unsigned numerical value, use uint8_t; If the intended use of the variable is to hold a signed numerical value, use int8_t; If the intended use of the variable is to hold a printable character, use char.

What is a Uint8 data type?

A uint8 data type contains all whole numbers from 0 to 255. As with all unsigned numbers, the values must be non-negative. Uint8's are mostly used in graphics (colors are always non-negative).


1 Answers

Because the types like char, short, int, long, and so forth, are ambiguous: they depend on the underlying hardware. Back in the days when C was basically considered an assembler language for people in a hurry, this was okay. Now, in order to write programs that are portable -- which means "programs that mean the same thing on any machine" -- people have built special libraries of typedefs and #defines that allow them to make machine-independent definitions.

The secret code is really quite straight-forward. Here, you have uint_8, which is interpreted

  • u for unsigned
  • int to say it's treated as a number
  • _8 for the size in bits.

In other words, this is an unsigned integer with 8 bits (minimum) or what we used to call, in the mists of C history, an "unsigned char".

like image 77
Charlie Martin Avatar answered Sep 30 '22 08:09

Charlie Martin