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?
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
__intNsyntax, 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_tversus using__int32?
-
On Windows,
int32_tis the same type as__int32, but the former is standard C, whereas the latter is not. -
You need to include
stdint.hto useint32_t, whereas__int32is 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; #endifAnd it uses
PROD_INT64everywhere for a 64-bit signed integer, and it
is compiled in both Visual Studio and gcc.Can it simply use the
int64_tin both Visual Studio and gcc?
Yes, and that’s certainly what I would do.
It would
seem this is changing__int64forlong longin Visual Studio.
Which is a distinction without a difference. Both of those spellings give you the same type in MSVC.