Skip to main content

Data Types in C ๐Ÿ“Š

Mentor's Note: Data types are like egg cartons of different sizes. An egg carton of size 6 cannot hold 12 eggs, and you wouldn't use a 30-egg tray to store 2 eggs. Choosing the right data type saves computer memory! ๐Ÿ’ก

๐Ÿ“š Educational Content: This tutorial is designed to align with GSEB Std 10/12, CBSE Computer Science, and BCA Sem 1 syllabus standards.

What You'll Learn

By the end of this tutorial, you'll know:

  • The 4 primary data types in C (char, int, float, double).
  • How to determine the exact size of any data type using sizeof().
  • How type qualifiers (short, long, signed, unsigned) modify data types.
  • The memory sizes, ranges, and format specifiers for each modified data type.
  • How memory overflow works when you exceed a data type's range.

๐ŸŒŸ The Scenario: The Warehouse Storage Cratesโ€‹

Imagine you run a shipping warehouse. To organize items, you use specific storage crates:

  • Small jewelry drawer: Stores exactly one character tag (like 'A' or 'B'). Takes up very little space.
  • Medium file drawer: Stores whole numbers (like invoice IDs). Can hold numbers up to millions.
  • Graduated cylinder: Stores precise quantities with decimal points (like liters of oil).
  • Giant secure vault: Stores long, ultra-precise scientific decimal measurements.

If you try to store a giant machine in the jewelry drawer, the drawer breaks. If you store a single jewelry ring in the giant vault, you waste an enormous amount of vault space and money.

In C programming:

  • The Jewelry Drawer is the char type (1 byte).
  • The File Drawer is the int type (4 bytes).
  • The Graduated Cylinder is the float type (4 bytes).
  • The Giant Vault is the double type (8 bytes).

๐Ÿ“– Concept Explanationโ€‹

In C, every variable must have a data type. The data type tells the compiler how much memory (how many bytes) to allocate for the variable and how to interpret the binary bits stored inside it.

The 4 Primary Data Typesโ€‹

  1. char (Character):

    • Stores a single character (like 'A', 'z', or '$').
    • Size: 1 byte (8 bits).
    • Under the hood, C actually stores the ASCII numeric value of the character (e.g., 'A' is stored as 65).
  2. int (Integer):

    • Stores whole numbers without decimals (like 10, -450, 0).
    • Size: Typically 4 bytes (32 bits) on modern computers.
  3. float (Single-precision Floating Point):

    • Stores numbers with fractional decimal points (like 3.14, -0.005).
    • Size: 4 bytes (32 bits). Accurate up to 6 decimal places.
  4. double (Double-precision Floating Point):

    • Stores large, highly precise decimal numbers.
    • Size: 8 bytes (64 bits). Accurate up to 15 decimal places.

๐Ÿ” Type Qualifiers: Modifying Primary Typesโ€‹

You can modify the size or range of primary data types using type qualifiers:

  • Size Qualifiers: short (reduces size) and long (increases size).
  • Sign Qualifiers: signed (allows both positive and negative values) and unsigned (allows only positive values, which doubles the positive range).

Primary Data Types Sizes, Ranges & Format Specifiersโ€‹

Below is the complete reference table for standard 32-bit/64-bit systems:

Data TypeSize (Bytes)RangeFormat Specifier
char / signed char1-128 to 127%c or %d (for ASCII value)
unsigned char10 to 255%c or %d
int / signed int4-2,147,483,648 to 2,147,483,647%d
unsigned int40 to 4,294,967,295%u
short int2-32,768 to 32,767%hd
unsigned short int20 to 65,535%hu
long int8 (or 4 on 32-bit)-9.22 ร— 10^18 to 9.22 ร— 10^18%ld
unsigned long int8 (or 4 on 32-bit)0 to 1.84 ร— 10^19%lu
float41.2E-38 to 3.4E+38%f
double82.3E-308 to 1.7E+308%lf
long double10 to 163.4E-4932 to 1.1E+4932%Lf

๐Ÿ’ป The sizeof() Operator: Inspecting Memoryโ€‹

The sizeof() operator is a built-in unary operator in C that returns the size of a variable or a data type in bytes.

#include <stdio.h>

// ๐Ÿ›’ Scenario: Check variable crate sizes in memory
// ๐Ÿš€ Action: Prints size of types in bytes using sizeof()

int main() {
char sampleChar = 'K';
int sampleInt = 1500;
float sampleFloat = 4.5f;
double sampleDouble = 99.987;

// sizeof returns a size_t type (unsigned integer), printed using %zu
printf("Size of char variable : %zu byte\n", sizeof(sampleChar));
printf("Size of int variable : %zu bytes\n", sizeof(sampleInt));
printf("Size of float variable : %zu bytes\n", sizeof(sampleFloat));
printf("Size of double variable : %zu bytes\n", sizeof(sampleDouble));

// You can also pass types directly
printf("Size of unsigned int : %zu bytes\n", sizeof(unsigned int));
printf("Size of long int : %zu bytes\n", sizeof(long int));

return 0;
}

// Output:
// Size of char variable : 1 byte
// Size of int variable : 4 bytes
// Size of float variable : 4 bytes
// Size of double variable : 8 bytes
// Size of unsigned int : 4 bytes
// Size of long int : 8 bytes

๐Ÿ“Š Sample Dry Runโ€‹

Let's trace what happens when we check sizeof values:

StepStatement ExecutedTarget of sizeofEvaluated ValueDescription
1sizeof(sampleChar)sampleChar (char)1Char occupies exactly 1 byte.
2sizeof(sampleInt)sampleInt (int)432-bit integer occupies 4 bytes.
3sizeof(sampleFloat)sampleFloat (float)4Single-precision float occupies 4 bytes.
4sizeof(sampleDouble)sampleDouble (double)8Double-precision float occupies 8 bytes.

๐Ÿงช Interactive Elementsโ€‹

Try It Yourselfโ€‹

Hands-on Exercise: Overflow Check

Task: Predict what happens if you assign 128 to a variable declared as signed char (which has a max range of 127). Run this code to verify:

#include <stdio.h>
int main() {
signed char num = 127;
num = num + 1;
printf("Value: %d\n", num);
return 0;
}

Hint: When a signed number overflows its maximum limit, it wraps around to the lowest negative number.

Quick Quizโ€‹

Quick Quiz: Character ASCII representation

If you assign a character value like 'A' to an integer variable in C, does it compile?

  • A) No, it throws a type mismatch error.
  • B) Yes, it compiles and stores the ASCII value 65.
  • C) Yes, but it crashes at runtime.
  • D) No, C does not support implicit casting of char to int.

Explanation: In C, characters are treated as small integers under the hood. Assigning 'A' to an int compiles perfectly and stores the integer value 65 (its ASCII representation).


๐Ÿ“‰ Complexity Analysisโ€‹

  • Time Complexity:
    • sizeof evaluation: $O(1)$ constant time. Note: sizeof is evaluated at compile-time by the compiler, not during program execution, so it takes zero runtime.
  • Space Complexity:
    • Memory footprint: Varies based on selected type (1 byte to 16 bytes).

๐ŸŽฏ Practice Problemsโ€‹

Easy Level ๐ŸŸขโ€‹

  1. Write a C program to print the sizes of short int, long int, long double, and unsigned char using sizeof().
  2. Find the error in this code:
    int main() {
    unsigned int score = -500;
    printf("%u", score);
    return 0;
    }

Medium Level ๐ŸŸกโ€‹

  1. Write a program to read a character from the user and display both the character and its numeric ASCII value.

Hard Level ๐Ÿ”ดโ€‹

  1. Write a program that demonstrates integer underflow. Assign the minimum possible value to a signed short int and subtract 1 from it. Print the resulting value and explain the output.

โ“ Frequently Asked Questionsโ€‹

Details

Q: Why is the size of int 4 bytes on some machines and 2 bytes on others? The size of int is platform-dependent. In old 16-bit DOS operating systems (like those running Turbo C++), an integer was 2 bytes. In modern 32-bit and 64-bit operating systems, an integer is 4 bytes. The C standard only mandates that sizeof(short int) <= sizeof(int) <= sizeof(long int).

Details

Q: What is the difference between float and double? float is a single-precision type occupying 4 bytes (6 decimal accuracy), while double is a double-precision type occupying 8 bytes (15 decimal accuracy). By default, any decimal constant (like 3.14) in C is treated as a double. To make it a float, you must add an 'f' suffix (like 3.14f).

Details

Q: What is a void data type? void means "no value" or "empty." It is used to specify that a function does not return any value (e.g., void display()) or that a function takes no arguments (e.g., int main(void)). You cannot declare a variable of type void.


๐Ÿ“š Best Practices & Common Mistakesโ€‹

โœ… Best Practicesโ€‹

  • Use double for precision: Unless you are working on resource-constrained embedded systems, prefer double over float to avoid decimal rounding errors.
  • Use unsigned for counts: If a variable will never be negative (like array indices, student count, age), declare it as unsigned to protect against negative logic bugs and double the positive limit.
  • Use %zu for sizeof: Always print the return value of sizeof() with the %zu format specifier, which is standard for size_t.

โŒ Common Mistakes โš ๏ธโ€‹

  • Decimal constant mismatch: Declaring float x = 3.14; instead of 3.14f. Without the 'f', the compiler warns you about converting a double to a float.
  • Overflowing variables: Storing a number like 100000 in a short int (max limit 32767).
  • Wrong format specifier: Printing a float variable using %d or an integer using %f. This prints garbage values.

โœ… Summaryโ€‹

In this tutorial, you've learned:

  • โœ… C has 4 primary data types: char, int, float, and double.
  • โœ… Data types determine the size of memory allocation and range of values.
  • โœ… Qualifiers like short, long, signed, and unsigned adjust size and sign.
  • โœ… The sizeof() operator is evaluated at compile-time to return byte sizes.
  • โœ… Exceeding a type's limits triggers overflow/underflow, wrapping values.

๐Ÿ’ก Pro Tip: Use #include <limits.h> to access constants like INT_MAX, INT_MIN, and CHAR_MAX to check variables limits directly in your C program!


๐Ÿ“š Further Readingโ€‹

Continue your learning path:

Go deeper:


---

๐Ÿ“ Visit Us

๐Ÿซ VD Computer Tuition Surat

VD Computer Tuition
๐Ÿ“ Address
2/66 Faram Street, Rustompura
Surat โ€“ 395002, Gujarat, India
๐Ÿ“ž Phone / WhatsApp
+91 84604 41384
๐ŸŒ Website

Computer Classes & Tuition โ€” Areas We Serve in Surat

Adajanโ€ขAlthanโ€ขAmroliโ€ขAthwaโ€ขAthwalinesโ€ขBhagalโ€ขBhatarโ€ขBhestanโ€ขCanal Roadโ€ขChowkโ€ขCitylightโ€ขDumasโ€ขGaurav Pathโ€ขGhod Dod Roadโ€ขHaziraโ€ขJahangirpuraโ€ขKamrejโ€ขKapodraโ€ขKatargamโ€ขLimbayatโ€ขMagdallaโ€ขMajura Gateโ€ขMota Varachhaโ€ขNanpuraโ€ขNew Citylightโ€ขOlpadโ€ขPalโ€ขPandesaraโ€ขParle Pointโ€ขPiplodโ€ขPunaโ€ขRanderโ€ขRing Roadโ€ขRustampuraโ€ขSachinโ€ขSalabatpuraโ€ขSarthanaโ€ขSosyo Circleโ€ขUdhnaโ€ขVarachhaโ€ขVed Roadโ€ขVesuโ€ขVIP Road
๐Ÿ“ž Call Sir๐Ÿ’ฌ WhatsApp Sir