Standard data types on UNIX, Linux, and Windows
Learn about standard data types on 32-bit UNIX and Linux, 64-bit UNIX and Linux, and 64-bit Windows applications.
32-bit UNIX and Linux applications
Name | Length |
---|---|
char | 1 byte |
short | 2 bytes |
int | 4 bytes |
long | 4 bytes |
float | 4 bytes |
double | 8 bytes |
long double | 8 bytes |
pointer | 4 bytes |
ptrdiff_t | 4 bytes |
size_t | 4 bytes |
time_t | 4 bytes |
clock_t | 4 bytes |
wchar_t | 4 bytes
Note that on AIX a wchar_t is 2 bytes. |
64-bit UNIX and Linux applications
Name | Length |
---|---|
char | 1 byte |
short | 2 bytes |
int | 4 bytes |
long | 8 bytes |
float | 4 bytes |
double | 8 bytes |
long double | 8 bytes |
pointer | 8 bytes |
ptrdiff_t | 8 bytes |
size_t | 8 bytes |
time_t | 8 bytes |
clock_t | 4 bytes |
wchar_t | 4 bytes
Note that on AIX a wchar_t is 2 bytes. |
Windows 64-bit applications
Name | Length |
---|---|
char | 1 byte |
short | 2 bytes |
int | 4 bytes |
long | 4 bytes |
float | 4 bytes |
double | 8 bytes |
long double | 8 bytes |
pointer | 8 bytes
Note that all pointers are 8 bytes. |
ptrdiff_t | 8 bytes |
size_t | 8 bytes |
time_t | 8 bytes |
clock_t | 4 bytes |
wchar_t | 2 bytes |
WORD | 2 bytes |
DWORD | 4 bytes |
HANDLE | 8 bytes |
HFILE | 4 bytes |
Coding considerations on Windows
- HANDLE hf;
-
Use
hf = CreateFile((LPCTSTR) FileName, Access, ShareMode, xihSecAttsNTRestrict, Create, AttrAndFlags, NULL);
Do not useHFILE hf; hf = (HFILE) CreateFile((LPCTSTR) FileName, Access, ShareMode, xihSecAttsNTRestrict, Create, AttrAndFlags, NULL);
as this produces an error. - size_t len fgets
-
Use
size_t len while (fgets(string1, (int) len, fp) != NULL) len = strlen(buffer);
Do not useint len; while (fgets(string1, len, fp) != NULL) len = strlen(buffer);
- printf
-
Use
printf("My struc pointer: %p", pMyStruc);
Do not useprintf("My struc pointer: %x", pMyStruc);
For hexadecimal output, you have to print the upper and lower 4 bytes separately. - char *ptr
-
Use
char * ptr1; char * ptr2; size_t bufLen; bufLen = ptr2 - ptr1;
Do not usechar *ptr1; char *ptr2; UINT32 bufLen; bufLen = ptr2 - ptr1;
- alignBytes
-
Use
alignBytes = (unsigned short) ((size_t) address % 16);
Do not usevoid *address; unsigned short alignBytes; alignBytes = (unsigned short) ((UINT32) address % 16);
- len
-
Use
len = (UINT32) ((char *) address2 - (char *) address1);
Do not usevoid *address1; void *address2; UINT32 len; len = (UINT32) ((char *) address2 - (char *) address1);
- sscanf
-
Use
MQLONG SBCSprt; sscanf(line, "%d", &SBCSprt);
Do not useMQLONG SBCSprt; sscanf(line, "%1d", &SBCSprt);
%ld tries to put an 8-byte type into a 4-byte type; only use %l if we are dealing with an actual long data type. MQLONG, UINT32 and INT32 are defined to be four bytes, the same as an int on all IBM MQ platforms:
Parent topic: Coding standards on 64-bit platforms