C Tutorial



DATA TYPES IN C


Data Types in C

Data types in C tell the compiler what type of data a variable will hold. Each variable in C must be declared with a data type, which determines the size and layout of the variable's memory.

๐Ÿ“˜ Tip: Using the right data type ensures efficient memory usage and better performance.

๐Ÿ“‚ Basic Data Types

Data Type Size (bytes) Description
int 4 Stores integers (whole numbers)
float 4 Stores decimal numbers (6-7 digits precision)
double 8 Stores large decimal numbers (15-16 digits precision)
char 1 Stores single characters (e.g., 'A')

๐Ÿงช Example

#include <stdio.h>

int main() {
    int age = 21;
    float pi = 3.14;
    double bigDecimal = 12345.6789;
    char grade = 'A';

    printf("Age: %d\\n", age);
    printf("Pi: %.2f\\n", pi);
    printf("Big Decimal: %.4lf\\n", bigDecimal);
    printf("Grade: %c\\n", grade);

    return 0;
}
  

๐Ÿ”Ž Format Specifiers

  • %d โ€“ for int
  • %f โ€“ for float
  • %lf โ€“ for double
  • %c โ€“ for char
๐Ÿ’ก Note: Use %.2f or %.4lf in printf to control decimal places.

๐Ÿงฑ Other Data Types

  • short โ€“ smaller int (usually 2 bytes)
  • long โ€“ bigger int (usually 4 or 8 bytes)
  • unsigned int โ€“ only positive integers

โŒ Common Mistakes

  • ๐Ÿšซ Mixing data types (e.g., using %d for float)
  • ๐Ÿšซ Overflowing data type limits
  • ๐Ÿšซ Using incorrect format specifiers in printf

๐Ÿ“ Try It Yourself

  1. Declare one variable of each type: int, float, double, char
  2. Assign sample values and print using the correct format specifier

๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review