Data Types
Integer
int (signed int), unsigned int
short (signed short int), unsigned short
long (signed long int), unsigned long
#include <stdio.h>
#include <limits.h>
int main()
{
printf("Int: %lu bytes\n", sizeof(int)); // 4
printf("Min Int: %d\n", INT_MIN); // -2147483648
printf("Max Int: %d\n", INT_MAX); // 2147483647
printf("Unsigned Int: %lu bytes\n", sizeof(unsigned int)); // 4
printf("Min Unsigned Int: %d\n", 0); // 0
printf("Max Unsigned Int: %u\n", UINT_MAX); // 4294967295
return 0;
}
#include <stdio.h>
#include <limits.h>
int main()
{
printf("Int: %lu bytes\n", sizeof(int)); // 4
printf("Short: %lu bytes\n", sizeof(short)); // 2
printf("Long: %lu bytes\n", sizeof(long)); // 8
printf("Min Short: %d\n", SHRT_MIN); // -32768
printf("Max Short: %u\n", SHRT_MAX); // 32767
printf("Min Short: %ld\n", LONG_MIN); // -9223372036854775808
printf("Max Short: %ld\n", LONG_MAX); // 9223372036854775807
return 0;
}
Overflow and Underflow
#include
#include
int main()
{
// Overflow
short a = SHRT_MAX + 1;
printf("Overflow: %d\n", a); # -32768
// Underflow
short b = SHRT_MIN - 1;
printf("Underflow: %d\n", b); # 32767
return 0;
}
Char
unsigned char, char (signed char)
char type in C is integer
#include <stdio.h>
#include <limits.h>
int main()
{
char var = 'a';
printf("Char: %c\n", var);
// use char as integer
printf("Int: %d\n", var); // 97
printf("Int: %d\n", var+1); // 98
// user integer as char
char var2 = 97;
printf("Char: %c\n", var2); // a
printf("Char: %c\n", 97); // a
printf("Char: %lu bytes\n", sizeof(char)); // 1
printf("Min Char: %d\n", CHAR_MIN); // -128
printf("Max Char: %d\n", CHAR_MAX); // 127
return 0;
}
Float
float
double
long double
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main()
{
printf("Float: %lu bytes\n", sizeof(float)); // 4
printf("Min Float: %e\n", FLT_MIN);
printf("Max Float: %e\n", FLT_MAX);
printf("Double: %lu bytes\n", sizeof(double)); // 8
printf("Min Double: %e\n", DBL_MIN);
printf("Max Double: %e\n", DBL_MAX);
printf("Long Double: %lu bytes\n", sizeof(long double)); // 16
return 0;
}
Enum
#include <stdio.h>
int main()
{
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
enum week day;
day = Wed;
printf("%d\n", day); // 2
int i;
for(i = Mon; i <= Sun; i++)
printf("%d\n", i); // 0 1 2 3 4 5 6
return 0;
}
Define Type
#include <stdio.h>
typedef float myType;
int main()
{
myType Pi = 3.14;
printf("Pi: %f\n", Pi);
return 0;
}