HOME HTML EDITOR C JAVA PHP

C Data Types: The Foundation of Data

In C programming, **Data Types** are declarations for variables. This determines the type and size of data associated with variables. Since C is a statically typed language, you must specify the data type of a variable at the time of declaration so the compiler can allocate the correct amount of memory.

1. Basic Data Types in C

C provides a set of built-in data types that handle various kinds of information, from simple characters to large decimal numbers. These are known as Primary Data Types.

2. Detailed Breakdown of Primary Types

The size and range of these data types can vary depending on the compiler and the architecture of the computer (32-bit vs 64-bit). However, the standard sizes are as follows:

Data Type Size Format Specifier Description
int 2 or 4 bytes %d or %i Stores whole numbers.
float 4 bytes %f Stores fractional numbers (6 decimals).
double 8 bytes %lf Stores fractional numbers (15 decimals).
char 1 byte %c Stores a single character/ASCII value.

3. Type Qualifiers: Modifying Data Types

C allows you to use Type Modifiers to change the range or size of the basic data types to suit your needs better. This is useful for optimizing memory.

4. Practical Implementation

Here is how you use different data types in a real program to store diverse information:

#include <stdio.h>

int main() {
    // Integer
    int items = 50;

    // Floating point
    float costPerItem = 9.99;

    // Double precision
    double totalRevenue = 499.5050;

    // Character
    char currencySymbol = '$';

    printf("Items: %d\n", items);
    printf("Cost: %c%.2f\n", currencySymbol, costPerItem);
    printf("Total: %.4lf\n", totalRevenue);

    return 0;
}

5. The sizeof() Operator

If you are ever unsure about how much memory a data type is occupying on your specific system, C provides the sizeof() operator. This is extremely helpful for writing platform-independent code.

printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of char: %zu byte\n", sizeof(char));

6. Understanding ASCII and Characters

In C, characters are actually stored as integers internally. Every character has a corresponding **ASCII value**. For example, 'A' is stored as 65. You can even perform mathematical operations on characters!

Pro Tip: Using the correct data type is not just about making the code work; it's about efficiency. Using a double when a float is sufficient wastes memory, which can slow down large-scale applications.