Skip to content

Instantly share code, notes, and snippets.

@jonasraoni
Created October 25, 2017 22:59
Show Gist options
  • Save jonasraoni/513f655d528ec5430d708ce4683eb381 to your computer and use it in GitHub Desktop.
Save jonasraoni/513f655d528ec5430d708ce4683eb381 to your computer and use it in GitHub Desktop.
Displays the memory alignment of each data type.
#include <stdio.h>
#define ALIGN_OF(t) ((int)(sizeof(struct{char c; t x;}) - sizeof(t)))
int main()
{
printf("char: %d\n", ALIGN_OF(char));
printf("short: %d\n", ALIGN_OF(short));
printf("int: %d\n", ALIGN_OF(int));
printf("long: %d\n", ALIGN_OF(long));
printf("long long: %d\n", ALIGN_OF(long long));
printf("void*: %d\n", ALIGN_OF(void*));
printf("float: %d\n", ALIGN_OF(float));
printf("double: %d\n", ALIGN_OF(double));
printf("long double: %d\n", ALIGN_OF(long double));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment