Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 uint types vs u_int

I just stumbled upon the type u_int8_t because it did not compile in Windows+MinGW (but compiled fine under Linux). According to this site the C++11 standard defines the type uint8_t. I just used the latter and everything worked.

The questions that arised are:

  1. Is there any difference between u_int8_t and uint8_t?
  2. Is there a reason (besides legacy code) to use u_int8_t?
  3. Is it safe to assume that uint8_t will be present if I use a C++11 compiler (on different OS or architectures)?
  4. Are the answers to the above questions also valid for the other types (intX_t and uintX_t)?
like image 212
masgo Avatar asked Apr 25 '14 11:04

masgo


People also ask

Should I use unsigned int or uint32_t?

uint32_t is used when you must have a 32 bit unsigned. int or unsigned int for general purposes when you don't need a guaranteed size and unsigned only if you can ensure that you won't have negative numbers.

Is unsigned int same as Uint?

Unsigned Integers (often called "uints") are just like integers (whole numbers) but have the property that they don't have a + or - sign associated with them. Thus they are always non-negative (zero or positive). We use uint's when we know the value we are counting will always be non-negative.

Does C have Uint?

The unsigned int can contain storage size either 2 or 4 bytes where values ranging from [0 to 65,535] or [0 to 4,294,967,295]. The format specifier used for an unsigned int data type in C is “ %u ”.

Does Uint exist in C++?

Unsigned int data type in C++ is used to store 32-bit integers. The keyword unsigned is a data type specifier, which only represents non-negative integers i.e. positive numbers and zero.


1 Answers

Is there any difference between u_int8_t and uint8_t?

u_int8_t is just a very old name that was not standardised. Avoid it.

Is there a reason (besides legacy code) to use u_int8_t?

Suicide by coworker.

Is it safe to assume that uint8_t will be present if I use a C++11 compiler (on different OS or architectures)?

The C++ standard requires it to be present on all implementations that have an unsigned 8-bit type available (today that means everything that is not exotic).

Are the answers to the above questions also valid for the other types (intX_t and uintX_t)?

Pretty much, yes.

like image 66
R. Martinho Fernandes Avatar answered Sep 20 '22 14:09

R. Martinho Fernandes