C Numeric Data Types

Welcome to The Coding College! In this tutorial, we’ll discuss numeric data types in C, their classifications, and how to use them efficiently in your programs. Numeric data types are used to store numbers, including integers and floating-point numbers, making them a cornerstone of programming in C.

What Are Numeric Data Types?

In C, numeric data types are used to handle numbers of various kinds, such as whole numbers and decimal values. These data types are further classified into:

  1. Integer Types – For whole numbers.
  2. Floating-Point Types – For numbers with fractional parts.

Choosing the correct numeric data type ensures optimal memory usage and prevents errors in calculations.

Integer Data Types

What Are Integer Data Types?

Integer data types store whole numbers without any fractional component.

Types of Integer Data Types

Data TypeSize (in bytes)Range (Signed)Range (Unsigned)Example
int2 or 4-32,768 to 32,767 (2 bytes) / -2,147,483,648 to 2,147,483,647 (4 bytes)0 to 65,535 (2 bytes) / 0 to 4,294,967,295 (4 bytes)int age = 25;
short2-32,768 to 32,7670 to 65,535short num = 10;
long4 or 8-2,147,483,648 to 2,147,483,647 (4 bytes) / Large Range (8 bytes)Similar to signedlong bigNum = 1000000;
long long8Very Large RangeSimilar to signedlong long hugeNum = 123456789;

Examples of Integer Data Types

Example 1: Declaring and Using Integer Types

#include <stdio.h>  

int main() {  
    int num = 100;  
    long bigNum = 1000000;  
    unsigned int positiveNum = 500;  

    printf("Integer: %d\n", num);  
    printf("Long Integer: %ld\n", bigNum);  
    printf("Unsigned Integer: %u\n", positiveNum);  

    return 0;  
}  

Output:

Integer: 100  
Long Integer: 1000000  
Unsigned Integer: 500  

Floating-Point Data Types

What Are Floating-Point Data Types?

Floating-point data types store numbers with fractional parts, such as 3.14. These types are used when precision is required.

Types of Floating-Point Data Types

Data TypeSize (in bytes)PrecisionRangeExample
float46 to 7 decimal places1.2E-38 to 3.4E+38float pi = 3.14;
double815 decimal places2.3E-308 to 1.7E+308double largeValue = 2.718281;
long double12, 16, or 8 (compiler-dependent)More than double precisionLarger than double rangelong double precise = 1.41421356237;

Examples of Floating-Point Data Types

Example 2: Declaring and Using Floating-Point Types

#include <stdio.h>  

int main() {  
    float radius = 5.5;  
    double area = 3.14159 * radius * radius;  

    printf("Radius: %.2f\n", radius);  
    printf("Area of Circle: %.5f\n", area);  

    return 0;  
}  

Output:

Radius: 5.50  
Area of Circle: 95.03318  

Combining Integer and Floating-Point Data Types

When using both integer and floating-point numbers in calculations, implicit or explicit type conversions occur.

Example 3: Mixed Operations

#include <stdio.h>  

int main() {  
    int num1 = 5;  
    float num2 = 2.5;  

    float result = num1 + num2;  
    printf("Result: %.2f\n", result);  

    return 0;  
}  

Output:

Result: 7.50  

Best Practices for Numeric Data Types

  1. Choose the Smallest Possible Data Type
    • Use short or unsigned short for small numbers.
    • Use float instead of double if precision isn’t critical.
  2. Be Aware of Overflow and Underflow
    • Assigning a value outside the range of a data type results in overflow or underflow, causing unpredictable behavior.
  3. Use long double for High Precision
    • When dealing with scientific calculations or very large values, opt for long double.

FAQs

1. What is the difference between float and double?

  • float uses 4 bytes of memory and has lower precision (6–7 decimal places).
  • double uses 8 bytes and offers higher precision (up to 15 decimal places).

2. Why use unsigned integers?

Unsigned integers cannot store negative values but offer a larger range of positive values, making them ideal for counting or indices.

3. How do I decide between int and long?

Use int for small numbers within its range and long for larger numbers that exceed the range of int.

Conclusion

Numeric data types in C are vital for defining and manipulating numbers in your programs. Understanding when to use integers, floating-point numbers, or their variations ensures optimal memory usage and prevents errors.

Leave a Comment