Skip to content

Instantly share code, notes, and snippets.

@WesleyGoncalves
Last active June 1, 2019 13:28
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 WesleyGoncalves/b914272d146917db37c6937cc2643064 to your computer and use it in GitHub Desktop.
Save WesleyGoncalves/b914272d146917db37c6937cc2643064 to your computer and use it in GitHub Desktop.
Sizes of data types and pointers in C
/**
* Sizes of data types and pointers in C
* \author Wesley Gonçalves (@wesleygoncalves)
*
* You can download this file
* directly to your project from the command-line
* curl -O https://gist.githubusercontent.com/WesleyGoncalves/b914272d146917db37c6937cc2643064/raw/894f67d5e98083281d87a7880da7d04455b2105f/datatype_sizes.c
*/
#include <stdio.h>
int main(void)
{
// Basic Data Types (signed)
char c;
short s;
int i;
float f;
double d;
long l;
long long ll;
long double ld;
// Unsigned data types
unsigned char uc;
unsigned short us;
unsigned int ui;
unsigned long ul;
unsigned long long ull;
// Pointers
char *cp = NULL;
short *sp = NULL;
int *ip = NULL;
float *fp = NULL;
double *dp = NULL;
long *lp = NULL;
long long *llp = NULL;
long double *ldp = NULL;
unsigned char *ucp = NULL;
unsigned short *usp = NULL;
unsigned int *uip = NULL;
unsigned long *ulp = NULL;
unsigned long long *ullp = NULL;
printf("BASIC DATA TYPES\n");
printf("char %lu bytes\n", sizeof(c));
printf("short %lu bytes\n", sizeof(s));
printf("int %lu bytes\n", sizeof(i));
printf("float %lu bytes\n", sizeof(f));
printf("double %lu bytes\n", sizeof(d));
printf("long %lu bytes\n", sizeof(l));
printf("long long %lu bytes\n", sizeof(ll));
printf("long double %lu bytes\n", sizeof(ld));
printf("\nUNSIGNED DATA TYPES\n");
printf("unsigned char %lu bytes\n", sizeof(uc));
printf("unsigned short %lu bytes\n", sizeof(us));
printf("unsigned int %lu bytes\n", sizeof(ui));
printf("unsigned long %lu bytes\n", sizeof(ul));
printf("unsigned long long %lu bytes\n", sizeof(ull));
printf("\n---POINTERS---\n");
printf("char %lu bytes\n", sizeof(cp));
printf("short %lu bytes\n", sizeof(sp));
printf("int %lu bytes\n", sizeof(ip));
printf("float %lu bytes\n", sizeof(fp));
printf("double %lu bytes\n", sizeof(dp));
printf("long %lu bytes\n", sizeof(lp));
printf("long long %lu bytes\n", sizeof(llp));
printf("long double %lu bytes\n", sizeof(ldp));
printf("unsigned char %lu bytes\n", sizeof(ucp));
printf("unsigned short %lu bytes\n", sizeof(usp));
printf("unsigned int %lu bytes\n", sizeof(uip));
printf("unsigned long %lu bytes\n", sizeof(ulp));
printf("unsigned long long %lu bytes\n", sizeof(ullp));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment