Skip to content

Instantly share code, notes, and snippets.

@kosh04
Created September 15, 2011 07:11
Show Gist options
  • Save kosh04/1218712 to your computer and use it in GitHub Desktop.
Save kosh04/1218712 to your computer and use it in GitHub Desktop.
sizeof演算子で変数の大きさを調べる
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <sys/types.h>
#include <wchar.h>
#include <time.h>
// XXX: "%zd" format cannot work in VC, MinGW
#define prints(type) printf(#type "\t\t" "%lu\n", (unsigned long)sizeof(type))
int main(void)
{
printf("Type" "\t\t" "Byte\n");
printf("----\n");
prints(char);
prints(char *);
prints(short);
prints(int);
prints(long);
prints(long long);
prints(float);
prints(double);
prints(long double);
prints(void);
prints(void *);
prints(size_t);
prints(ssize_t);
prints(time_t);
prints(off_t);
prints(wchar_t);
prints(intptr_t);
prints(ptrdiff_t);
prints(_Bool);
return 0;
}
@kosh04
Copy link
Author

kosh04 commented Sep 15, 2011

// win32 mingw, win32 pcc
char 1
char * 4
short 2
int 4
long 4
long long 8
float 4
double 8
long double 12
void 1
void * 4
size_t 4
ssize_t 4
time_t 4
off_t 4
wchar_t 2
intptr_t 4
ptrdiff_t 4
_Bool 1

@kosh04
Copy link
Author

kosh04 commented Sep 15, 2011

// win64 mingw-w64

char 1
char * 8
short 2
int 4
long 4
long long 8
float 4
double 8
long double 16
void 1
void * 8
size_t 8
ssize_t 8
time_t 8
off_t 4
wchar_t 2
intptr_t 8
ptrdiff_t 8
_Bool 1

@kosh04
Copy link
Author

kosh04 commented Sep 15, 2011

// FreeBSD 8.1 i386

char 1
char * 4
short 2
int 4
long 4
long long 8
float 4
double 8
long double 12
void 1
void * 4
size_t 4
ssize_t 4
time_t 4
off_t 8
wchar_t 4
intptr_t 4
ptrdiff_t 4
_Bool 1

@kosh04
Copy link
Author

kosh04 commented Sep 15, 2011

// Ubuntu 10.04.3 i686

char 1
char * 4
short 2
int 4
long 4
long long 8
float 4
double 8
long double 12
void 1
void * 4
size_t 4
ssize_t 4
time_t 4
off_t 4
wchar_t 4
intptr_t 4
ptrdiff_t 4
_Bool 1

@kosh04
Copy link
Author

kosh04 commented Sep 15, 2011

// win32 VC

char 1
char * 4
short 2
int 4
long 4
long long 8
float 4
double 8
long double 8
void 0 // warning C4034
void * 4
size_t 4
ssize_t undefined
time_t 8
off_t 4
wchar_t 2
intptr_t 4
ptrdiff_t 4
_Bool undefined

@kosh04
Copy link
Author

kosh04 commented Sep 15, 2011

// Borland C++ 5.5.1 for Win32
char 1
char * 4
short 2
int 4
long 4
long long undefined
float 4
double 8
long double 10
void undefined
void * 4
size_t 4
ssize_t undefined
time_t 4
off_t 4
wchar_t 2
intptr_t undefined
ptrdiff_t 4
_Bool undefined

@kosh04
Copy link
Author

kosh04 commented Sep 18, 2011

// CentOS 5.6 Intel Xeon x86_64-redhat-linux

char 1
char * 8
short 2
int 4
long 8
long long 8
float 4
double 8
long double 16
void 1
void * 8
size_t 8
ssize_t 8
time_t 8
off_t 8
wchar_t 4
intptr_t 8
ptrdiff_t 8
_Bool 1

@kosh04
Copy link
Author

kosh04 commented Sep 18, 2011

// FreeBSD 8.2 amd64

char 1
char * 8
short 2
int 4
long 8
long long 8
float 4
double 8
long double 16
void 1
void * 8
size_t 8
ssize_t 8
time_t 8
off_t 8
wchar_t 4
intptr_t 8
ptrdiff_t 8
_Bool 1

@kosh04
Copy link
Author

kosh04 commented Sep 19, 2011

// win32 LSI C-86 試食版

char 1
char * 2
short 2
int 2
long 4
long long undef
float 4
double 8
long double 10
void undef
void * 2
size_t 2
ssize_t undef
time_t 4
off_t 4
wchar_t undef
intptr_t undef
ptrdiff_t 2
_Bool undef

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment