Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a C program to display all datatypes ranges in tabular form
In C programming, data types define the type and range of values that can be stored in variables. This program displays all fundamental data types with their minimum and maximum ranges in a tabular format using the limits.h header file.
Syntax
#include <limits.h> #include <float.h> // Use predefined constants like INT_MIN, INT_MAX, FLT_MIN, FLT_MAX
Primary Data Types
C supports four fundamental data types −
- Integer − Stores whole numbers (int, short, long)
- Character − Stores single characters (char)
- Floating-point − Stores decimal numbers (float)
- Double precision − Stores high-precision decimal numbers (double)
Data Type Ranges Summary
| Data Type | Size (bytes) | Range | Format Specifier |
|---|---|---|---|
| char | 1 | -128 to 127 | %c |
| unsigned char | 1 | 0 to 255 | %c |
| short int | 2 | -32,768 to 32,767 | %hd |
| unsigned short | 2 | 0 to 65,535 | %hu |
| int | 4 | -2,147,483,648 to 2,147,483,647 | %d |
| unsigned int | 4 | 0 to 4,294,967,295 | %u |
| long int | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | %ld |
| float | 4 | 3.4E-38 to 3.4E+38 | %f |
| double | 8 | 1.7E-308 to 1.7E+308 | %lf |
Example: Complete Data Type Range Display
Here's a comprehensive program that displays ranges for all C data types −
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
printf("=== C DATA TYPE RANGES ===<br><br>");
printf("%-20s %-15s %-25s<br>", "DATA TYPE", "SIZE (bytes)", "RANGE");
printf("%-20s %-15s %-25s<br>", "---------", "-----------", "-----");
// Integer types
printf("%-20s %-15d %d to %d<br>", "char", sizeof(char), CHAR_MIN, CHAR_MAX);
printf("%-20s %-15d 0 to %u<br>", "unsigned char", sizeof(unsigned char), UCHAR_MAX);
printf("%-20s %-15d %d to %d<br>", "short int", sizeof(short), SHRT_MIN, SHRT_MAX);
printf("%-20s %-15d 0 to %u<br>", "unsigned short", sizeof(unsigned short), USHRT_MAX);
printf("%-20s %-15d %d to %d<br>", "int", sizeof(int), INT_MIN, INT_MAX);
printf("%-20s %-15d 0 to %u<br>", "unsigned int", sizeof(unsigned int), UINT_MAX);
printf("%-20s %-15d %ld to %ld<br>", "long int", sizeof(long), LONG_MIN, LONG_MAX);
printf("%-20s %-15d 0 to %lu<br>", "unsigned long", sizeof(unsigned long), ULONG_MAX);
// Floating-point types
printf("%-20s %-15d %E to %E<br>", "float", sizeof(float), FLT_MIN, FLT_MAX);
printf("%-20s %-15d %E to %E<br>", "double", sizeof(double), DBL_MIN, DBL_MAX);
printf("%-20s %-15d %LE to %LE<br>", "long double", sizeof(long double), LDBL_MIN, LDBL_MAX);
return 0;
}
=== C DATA TYPE RANGES === DATA TYPE SIZE (bytes) RANGE --------- ----------- ----- char 1 -128 to 127 unsigned char 1 0 to 255 short int 2 -32768 to 32767 unsigned short 2 0 to 65535 int 4 -2147483648 to 2147483647 unsigned int 4 0 to 4294967295 long int 8 -9223372036854775808 to 9223372036854775807 unsigned long 8 0 to 18446744073709551615 float 4 1.175494E-38 to 3.402823E+38 double 8 2.225074E-308 to 1.797693E+308 long double 16 3.362103E-4932 to 1.189731E+4932
Key Points
- The
limits.hheader provides constants for integer type limits - The
float.hheader provides constants for floating-point type limits - Signed types can store both positive and negative values
- Unsigned types can only store non-negative values but have a larger positive range
- Size may vary between different systems and compilers
Conclusion
Understanding data type ranges is crucial for choosing appropriate types for variables and avoiding overflow errors. The limits.h and float.h headers provide portable ways to access these ranges programmatically.
Advertisements
