Skip to content

Instantly share code, notes, and snippets.

@rajesh-s
Created April 25, 2020 07:08
Show Gist options
  • Save rajesh-s/3b285953f61cc3fd88ae0a5fe3953a6d to your computer and use it in GitHub Desktop.
Save rajesh-s/3b285953f61cc3fd88ae0a5fe3953a6d to your computer and use it in GitHub Desktop.
Snippet to determine the defined sizes of C data types on any machine
// Determine ranges of short,int,long (unsigned/signed) and char by implementation
#include <stdio.h>
#include <limits.h> // Defines size constants for integral typs
#include <float.h> // Defines floating-point-arithmetic related parameters
void main()
{
printf("The range of %d bit char values: %d to %d", CHAR_BIT, CHAR_MIN, CHAR_MAX); // These are #defines in the header
printf("\nThe range of signed char values: %d to %d", SCHAR_MIN, SCHAR_MAX);
printf("\nThe range of unsigned char values: %d", UCHAR_MAX);
printf("\n================ INTEGRAL TYPES ================");
printf("\nThe range of %d byte signed int values: %d to %d", sizeof(int), INT_MIN, INT_MAX);
printf("\nThe range of %d byte unsigned int values: %lu", sizeof(unsigned int), UINT_MAX);
printf("\nThe range of %d byte signed long values: %li to %li", sizeof(long), LONG_MIN, LONG_MAX);
printf("\nThe range of %d byte unsigned long values: %lu", sizeof(unsigned long), ULONG_MAX);
printf("\nThe range of %d byte signed short values: %d to %d", sizeof(short), SHRT_MIN, SHRT_MAX);
printf("\nThe range of %d byte unsigned short values: %d", sizeof(unsigned short), USHRT_MAX);
printf("\n============= FLOATING-POINT TYPES =============");
printf("\nThe range of %d byte float values: %.2e to %.2e", sizeof(float), FLT_MIN, FLT_MAX);
printf("\nThe range of %d byte double values: %.2e to %.2e", sizeof(double), DBL_MIN, DBL_MAX);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment