Data Types in C
Before you store something, C wants to know what kind of data it is: a whole number, a decimal number, or a letter. This is called the data type.
The data type decides two things: what values a variable can hold and how much memory it takes. In this lesson you will learn all the data types of C with their sizes.
What You Will Learn
- What a data type is.
- The 3 categories of data types: basic, derived and user-defined.
- The 4 basic types:
int,char,float,double(andvoid). - Size and range of each type.
- How to find the size with
sizeof.
Data Types in C Explained
Exam definition: A data type tells the compiler what type of value a variable can store and how much memory it needs. C data types are of three kinds: basic (primary), derived and user-defined.
| Category | Types |
|---|---|
| Basic (primary) | int, char, float, double, void |
| Derived | Array, pointer, function |
| User-defined | struct, union, enum, typedef |
Memory trick: "B-D-U" = Basic, Derived, User-defined. Derived = "APF" (Array, Pointer, Function). User-defined = "SUET" (Struct, Union, Enum, Typedef).
The basic data types
| Type | Stores | Size (usual) | Format | Example |
|---|---|---|---|---|
char | A single character | 1 byte | %c | char grade = 'A'; |
int | Whole numbers | 4 bytes | %d | int age = 19; |
float | Decimal numbers (about 6–7 digits) | 4 bytes | %f | float price = 49.5f; |
double | Decimal numbers (about 15–16 digits) | 8 bytes | %lf (scanf), %f (printf) | double pi = 3.14159265; |
void | No value (empty) | – | – | void show(void); |
Sizes can differ between computers. The values above are true for almost all modern 64-bit systems. char is always 1 byte.
Range of values
| Type | Range (usual) |
|---|---|
char | −128 to 127 (as signed char; 0 to 255 as unsigned char) |
int | −2,147,483,648 to 2,147,483,647 (about ±2.1 billion) |
float | about 1.2 × 10−38 to 3.4 × 1038 |
double | about 2.3 × 10−308 to 1.7 × 10308 |
float vs double
float= single precision: 4 bytes, about 6–7 correct digits.double= double precision: 8 bytes, about 15–16 correct digits.- Use
doublewhen you need more accuracy (money calculations, science).
Syntax: Declaring Variables of Each Type
char letter = 'C'; /* single quotes for a character */
int count = 100;
float marks = 92.5f; /* the f means "this is a float" */
double area = 153.938;
Example 1: Size of Data Types Using sizeof
The sizeof operator tells you how many bytes a type uses on your computer.
#include <stdio.h>
int main(void)
{
printf("Size of char = %zu byte\n", sizeof(char));
printf("Size of int = %zu bytes\n", sizeof(int));
printf("Size of float = %zu bytes\n", sizeof(float));
printf("Size of double = %zu bytes\n", sizeof(double));
return 0;
}
Output
Size of char = 1 byte Size of int = 4 bytes Size of float = 4 bytes Size of double = 8 bytes
Code Explanation
sizeof(char): gives the size ofcharin bytes.%zu: the correct format specifier for the result ofsizeof(an unsigned size value). Some old compilers use%luinstead.- Each line prints one type and its size.
How Data Types Work in Memory
- Memory is made of bytes. One byte = 8 bits (8 zeros and ones).
- An
inttakes 4 bytes = 32 bits. With 32 bits, about 4.29 billion different values can be stored, which is why its range is about ±2.1 billion. - A
charis stored as a number too.'A'is stored as 65 (its ASCII code). - Decimal numbers (
float,double) are stored in a special format, so they are sometimes slightly inaccurate (0.1 cannot be stored exactly).
Example 2: Student Details Using Different Data Types
#include <stdio.h>
int main(void)
{
char section = 'B';
int roll_no = 27;
float percentage = 86.75f;
double fees = 125000.50;
printf("Section : %c\n", section);
printf("Roll No : %d\n", roll_no);
printf("Percentage : %.2f\n", percentage);
printf("Fees : %.2f\n", fees);
printf("ASCII of %c : %d\n", section, section);
return 0;
}
Output
Section : B Roll No : 27 Percentage : 86.75 Fees : 125000.50 ASCII of B : 66
The last line prints the same char twice: once as a letter (%c) and once as a number (%d). This shows that characters are stored as numbers.
Important Points to Remember
- 3 categories: Basic, Derived, User-defined.
- Basic types:
int,char,float,double,void. - Usual sizes: char 1, int 4, float 4, double 8 bytes.
charis stored as its ASCII number ('A' = 65).voidmeans no value.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
int price = 49.99; | Decimal part is lost; stores 49 | Use float or double |
char name = "Riya"; | Error / wrong value: a char holds one character | Use a string (array): char name[] = "Riya"; |
char c = "A"; | Double quotes make a string, not a character | char c = 'A'; |
Printing a float with %d | Wrong output | Use %f |
Data Types in C Practice Questions
- What is a data type? Name its three categories.
- Write the four basic data types with their usual sizes.
- What is the difference between
floatanddouble? - Name two derived and two user-defined data types.
- Which data type would you use for: age, price of a phone, first letter of your name?
Show answers
- It tells the type of value a variable can store and its memory size. Categories: basic, derived, user-defined.
char1 byte,int4 bytes,float4 bytes,double8 bytes.floatis single precision (4 bytes, ~6–7 digits);doubleis double precision (8 bytes, ~15–16 digits).- Derived: array, pointer (also function). User-defined: struct, union (also enum, typedef).
- Age:
int. Price:floatordouble. First letter:char.
Coding Exercise
Write a program that stores a product's code letter ('P'), quantity (12) and price per item (24.50), calculates the total cost in a double, and prints all values.
Show solution
#include <stdio.h>
int main(void)
{
char code = 'P';
int quantity = 12;
double price = 24.50;
double total = quantity * price;
printf("Code: %c\n", code);
printf("Quantity: %d\n", quantity);
printf("Price: %.2f\n", price);
printf("Total: %.2f\n", total);
return 0;
}
Code: P Quantity: 12 Price: 24.50 Total: 294.00
Quick Quiz: Data Types in C (MCQ)
-
What is the size of
charin C?A) 1 byte B) 2 bytes C) 4 bytes D) 8 bytes
-
Which type stores decimal numbers with the most precision?
A)
intB)charC)floatD)double -
An array is which kind of data type?
A) Basic B) Derived C) User-defined D) None
-
Which format specifier prints a
char?A)
%dB)%fC)%cD)%s -
voidmeans:A) Zero B) No value C) Empty string D) Error
Show answers
- A: 1 byte.
- D:
double. - B: derived.
- C:
%c. - B: no value.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Categories | Basic, Derived, User-defined |
|---|---|
| Basic types | int, char, float, double, void |
| Derived | Array, Pointer, Function |
| User-defined | struct, union, enum, typedef |
| Sizes | char 1, int 4, float 4, double 8 (bytes) |
| Formats | %c, %d, %f, %lf |
| 'A' stored as | 65 (ASCII) |
Frequently asked questions
- What is the difference between float and double?
floatis 4 bytes, single precision;doubleis 8 bytes, double precision.- Why is the size of int not fixed in C?
- It depends on the compiler and computer; C only fixes the minimum. On modern systems it is 4 bytes.
- What is void used for?
- To say "no value", e.g. a function that returns nothing.
- How is a character stored in memory?
- As a number: its ASCII code ('A' = 65, 'a' = 97).
Remember
- Categories B-D-U; Derived APF; User-defined SUET.
- Sizes: 1-4-4-8 (char, int, float, double).
Next Topic
Next lesson: Type Modifiers in C. You will learn how short, long, signed and unsigned change the size and range of a type.