Skip to content

Instantly share code, notes, and snippets.

@Alexey-N-Chernyshov
Created June 12, 2016 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexey-N-Chernyshov/2835ee95908b681c8f5287b101cf4083 to your computer and use it in GitHub Desktop.
Save Alexey-N-Chernyshov/2835ee95908b681c8f5287b101cf4083 to your computer and use it in GitHub Desktop.
C struct allignment.
#include <stdio.h>
#include <stddef.h>
struct foo {
short a;
int b;
char c;
};
int main(void) {
printf("sizeof(short) is %d\n", sizeof(short));
printf("sizeof(int) is %d\n", sizeof(int));
printf("sizeof(char) is %d\n\n", sizeof(char));
printf("sizeof(foo) is %d\n\n", sizeof(struct foo));
printf ("offsetof(struct foo,a) is %d\n",(int)offsetof(struct foo,a));
printf ("offsetof(struct foo,b) is %d\n",(int)offsetof(struct foo,b));
printf ("offsetof(struct foo,c) is %d\n",(int)offsetof(struct foo,c));
return 0;
}
@Alexey-N-Chernyshov
Copy link
Author

Output:
sizeof(short) is 2
sizeof(int) is 4
sizeof(char) is 1

sizeof(foo) is 12

offsetof(struct foo,a) is 0
offsetof(struct foo,b) is 4
offsetof(struct foo,c) is 8

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