Understanding Data Types in C: Size, Minimum and Maximum Values


The code is a C program that prints the size, minimum and maximum value of different data types. Here is an explanation of each line of the code:

  • #include <stdio.h> and #include<limits.h> and #include<float.h> are preprocessor directives that include the contents of the standard input-output header "stdio.h" and the limits header "limits.h" and the float header "float.h" respectively in the program. "limits.h" contains the macros for minimum and maximum values of different data types and "float.h" contains the macros for the size of different floating-point data types.
  • printf("\nShort int %u Bytes %d To %d",sizeof(short int),SHRT_MIN,SHRT_MAX); prints the size of short int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nunsigned short int %u Bytes 0 To %d",sizeof(unsigned short int),USHRT_MAX); prints the size of unsigned short int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nunsigned int %u Bytes 0 To %u",sizeof(unsigned int),UINT_MAX); prints the size of unsigned int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nint %u Bytes %d To %d",sizeof(int),INT_MIN,INT_MAX); prints the size of int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nlong int %u Bytes %ld To %ld",sizeof(long int),LONG_MIN,LONG_MAX); prints the size of long int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nunsigned long int %u Bytes 0 To %u",sizeof(unsigned long int),ULONG_MAX); prints the size of unsigned long int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nlong long int %u Bytes %lld To %lld",sizeof(long long int),LONG_LONG_MIN,LONG_LONG_MAX); prints the size of long long int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nunsigned long long int %u Bytes 0 To %llu \n\n",sizeof(unsigned long long int),ULONG_LONG_MAX); prints the size of unsigned long long int in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nCharacter %u Bytes %d To %d",sizeof(char),CHAR_MIN,CHAR_MAX); prints the size of char data type in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nCharacter %u Bytes 0 To %d\n\n",sizeof(unsigned char),UCHAR_MAX); prints the size of unsigned char data type in bytes, the minimum value it can hold and maximum value it can hold.
  • printf("\nFloat %u Bytes",sizeof(float)); prints the size of the float data type in bytes.
  • printf("\nDouble %u Bytes",sizeof(double)); prints the size of the double data type in bytes.
  • printf("\nLong Double %u Bytes\n\n",sizeof(long double)); prints the size of the long double data type in bytes.
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned as the exit status of the program, which indicates a successful execution.

When you run this code, it will print the size, minimum and maximum value of different data types in bytes to the console.


List of Data Types in C Programming

Data Type in C Programming

Integer Datatype

An integer is a whole number that can be positive, negative, or zero.

Data Type Memory Range Format Specifier
short int 2 -32768 to 32767 %hd
unsigned short int 2 0 to 65535 %hu
unsigned int 4 0 to 4294967295 %u
int 4 -2147483648 to 2147483647 %d
long int 4 -2147483648 to 2147483647 %ld
unsigned long int 4 0 to 4294967295 %lu
long long int 8 -9223372036854775808 To 9223372036854775807 %lld
unsigned long long int 8 0 to 18446744073709551615 %llu

Character Data Type in C Programming

Character is a data type that holds one character (letter, number, etc.) of data. It must be enclosed with single quotes. e.g. 'A', '4', or '#'

Data Type Memory Range Format Specifier
char or signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c

Floating Point Data Type in C Programming

Float is a shortened term for floating point.

Data Type Memory Range Format Specifier
float 4 3.4E-38 to 3.4E+38 %f
double 8 1.7E-308 to 1.7E+308 %lf
long double 12 3.4E-4932 to 1.1E+4932 %lf

Program to Find the maximum and minimum values to Store in a Datatypes



Source Code

#include<stdio.h>
#include<limits.h>
#include<float.h>
int main()
{
  //Integer
  printf("\nShort int %u Bytes %d To %d",sizeof(short int),SHRT_MIN,SHRT_MAX);
  printf("\nunsigned short int %u Bytes 0 To %d",sizeof(unsigned short int),USHRT_MAX);
  printf("\nunsigned int %u Bytes 0 To %u",sizeof(unsigned int),UINT_MAX);
  printf("\nint %u Bytes %d To %d",sizeof(int),INT_MIN,INT_MAX);
  printf("\nlong int %u Bytes %ld To %ld",sizeof(long int),LONG_MIN,LONG_MAX);
  printf("\nunsigned long int %u Bytes 0 To %u",sizeof(unsigned long int),ULONG_MAX);
  printf("\nlong long int %u Bytes %lld To %lld",sizeof(long long int),LONG_LONG_MIN,LONG_LONG_MAX);
  printf("\nunsigned long long int %u Bytes 0 To %llu \n\n",sizeof(unsigned long long int),ULONG_LONG_MAX);
  //Character
  printf("\nCharacter %u Bytes %d To %d",sizeof(char),CHAR_MIN,CHAR_MAX);
  printf("\nCharacter %u Bytes 0 To %d\n\n",sizeof(unsigned char),UCHAR_MAX);
  //Float
  printf("\nFloat %u Bytes",sizeof(float));
  printf("\nDouble %u Bytes",sizeof(double));
  printf("\nLong Double %u Bytes\n\n",sizeof(long double));
  return 0;
}
To download raw file Click Here

Output

Short int 2 Bytes -32768 To 32767
unsigned short int 2 Bytes 0 To 65535
unsigned int 4 Bytes 0 To 4294967295
int 4 Bytes -2147483648 To 2147483647
long int 4 Bytes -2147483648 To 2147483647
unsigned long int 4 Bytes 0 To 4294967295
long long int 8 Bytes -9223372036854775808 To 9223372036854775807
unsigned long long int 8 Bytes 0 To 18446744073709551615

Character 1 Bytes -128 To 127
Character 1 Bytes 0 To 255

Float 4 Bytes
Double 8 Bytes
Long Double 12 Bytes

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C