0

I read that stdint.h is used for portability, but I'm confused.

If I wrote a program on a 32-bit system, uint32_t (unsigned int) is 4-bytes.

But when this program is run on 16-bit system, int is 2bytes and uint32_t (unsigned int) is 2bytes.

I think portability is not guaranteed in this case. Is there anything I am understanding wrong?

4
  • 7
    uint32_t is always 32 bits. That's why it has 32 in the name. Commented Nov 15, 2022 at 17:00
  • 3
    What 16 bit system? How have you determined that uint32_t is 2 bytes? Please edit your question to include these details Commented Nov 15, 2022 at 17:06
  • Why would uin32_t be unsigned int if int is only 16 bits? Sounds rather broken. Where did you get that idea? Commented Nov 15, 2022 at 17:20
  • 1
    porpomas, "... and uint32_t (unsigned int) is 2bytes." ---> No. Post the code you used to determine that. Commented Nov 15, 2022 at 18:24

2 Answers 2

6

Is there any thing I am understanding wrong?

Yes.

Type uint32_t is always an unsigned integer type with exactly 32 bits, none of them padding bits. On many modern systems that corresponds to type unsigned int, but on others it might correspond to a different type, such as unsigned long int. On systems that do not have a type with the correct properties, it is not defined at all.

The point of uint32_t and the other explicit-width data types from stdint.h is exactly to address the issue you raise, that (for example) unsigned int has a different size on different machines.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought stdint.h just use typedef. But it seems use other mechanisms like cheking wordsize and include other header files. these thing make the bit size consistent, right?
@porpomas, the language spec does not specify what mechanism a C implementation uses to define uint32_t, etc. A typedef is a likely possibility, but not the only one. But perhaps you are failing to recognize that different C implementations might provide different typedefs for uint32_t and other types.
@porpomas yes, it mostly provides typedef. That does not mean you get the same definitions for every compiler.
@porpomas: At the end of the day stdint.h indeed comes down to typedefs, however the particular exact definition of that will be determined at compile time by a combination of preprocessor #ifs and target macros defined by the compiler itself. Also some compilers (for example GCC and Clang) will define macros with the appropriate type definitions. If using GCC or Clang look at it's output when calling it like this: gcc -E -dM - < /dev/null | egrep '__.INT.*_TYPE'
@porpomas: Now if you were to call it with a different compilation target, those type definition macros would be sufficiently changed for that particular target.
1

A uint32_t and a uint16_t are distinct types from int.

While the size of int may vary, a uint32_t is always 32 bit and a uint16_t is always 16 bit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.