Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can sized integer types be used interchangeably with typedefs?

Visual Studio stdint.h seems to have the following typedefs:

typedef signed char        int8_t;
typedef short              int16_t;
typedef int                int32_t;
typedef long long          int64_t;
typedef unsigned char      uint8_t;
typedef unsigned short     uint16_t;
typedef unsigned int       uint32_t;
typedef unsigned long long uint64_t;

However, sized integer types use the __intN syntax, as described here: https://learn.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170

Is there any difference (for example) between using int32_t versus using __int32?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I guess I am a little confused if the purpose of the int32_t typedef is to be the ANSI C99 standard to abstract away the specific compiler syntax (so you can use int32_t in both Visual Studio and gcc C code for example), then I’m not sure why the typedef in Visual Studio wouldn’t be: typedef __int32 int32_t;

Say that the codebase has the following:

#ifdef _MSC_VER
    typedef __int64 PROD_INT64;
#else
    typedef int64_t PROD_INT64;
#endif

And it uses PROD_INT64 everywhere for a 64-bit signed integer, and it is compiled in both Visual Studio and gcc.

Can it simply use the int64_t in both Visual Studio and gcc? It would seem this is changing __int64 for long long in Visual Studio.

>Solution :

sized integer types use the __intN syntax, as described here: https://learn.microsoft.com/en-us/cpp/cpp/int8-int16-int32-int64?view=msvc-170

Your wording suggests that you think the __intN syntax is somehow more correct or fundamental than all other alternatives. That’s not what the doc you link says. It simply defines what those particular forms mean. In Microsoft C, those are preferred over Microsoft’s older, single-underscore forms (_intN), but there’s no particular reason to think that they are to be preferred over other alternatives, such as the intN_t forms available when you include stdint.h. The key distinguishing characteristic of the __intN types is that they are built in, available without including any particular header.

Is there any difference (for example) between using int32_t versus using __int32?

  • On Windows, int32_t is the same type as __int32, but the former is standard C, whereas the latter is not.

  • You need to include stdint.h to use int32_t, whereas __int32 is built in to MSVC.

I’m not sure why the typedef in Visual Studio wouldn’t be: typedef __int32 int32_t;

It’s an implementation decision that may or may not have a well-considered reason. As long as the implementation provides correct definitions — and there’s no reason to think MSVC is doing otherwise — you shouldn’t care about the details.

Say that the codebase has the following:

#ifdef _MSC_VER
    typedef __int64 PROD_INT64;
#else
    typedef int64_t PROD_INT64;
#endif

And it uses PROD_INT64 everywhere for a 64-bit signed integer, and it
is compiled in both Visual Studio and gcc.

Can it simply use the int64_t in both Visual Studio and gcc?

Yes, and that’s certainly what I would do.

It would
seem this is changing __int64 for long long in Visual Studio.

Which is a distinction without a difference. Both of those spellings give you the same type in MSVC.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading