C — Data Types
Type categories
C organizes types into three categories:
| Category | Types |
|---|---|
| Basic | char, int, float, double |
| Derived | array, pointer, function, struct, union, enum |
| Qualifiers | const, volatile, restrict |
Integer types
#include <stdio.h>
int main() {
char c = 'A'; // 1 byte, -128 to 127
unsigned char uc = 255; // 1 byte, 0 to 255
short s = -32000; // 2 bytes
unsigned short us = 65535; // 2 bytes
int i = 2147483647; // 4 bytes (typical)
unsigned int ui = 4294967295U; // 4 bytes
long l = 1000000L; // 4 or 8 bytes
long long ll = 9223372036854775807LL; // 8 bytes
printf("char: %d bytes\n", sizeof(char));
printf("short: %d bytes\n", sizeof(short));
printf("int: %d bytes\n", sizeof(int));
printf("long: %d bytes\n", sizeof(long));
printf("long long: %d bytes\n", sizeof(long long));
return 0;
}
Floating-point types
#include <stdio.h>
int main() {
float f = 3.14f; // 4 bytes, ~7 decimal digits
double d = 3.141592653589793; // 8 bytes, ~15 decimal digits
long double ld = 3.141592653589793238L; // 12-16 bytes
printf("float: %.7f\n", (double)f);
printf("double: %.15f\n", d);
printf("long double: %.18Lf\n", ld);
// Special values
printf("Infinity: %f\n", 1.0 / 0.0); // inf
printf("NaN: %f\n", 0.0 / 0.0); // nan
printf("-Infinity: %f\n", -1.0 / 0.0); // -inf
return 0;
}
Character type
#include <stdio.h>
int main() {
char letter = 'A';
char newline = '\n';
char null_char = '\0';
char escaped = '\\'; // Backslash
// Character operations
printf("Uppercase: %c\n", letter); // A
printf("Lowercase: %c\n", letter + 32); // a
printf("ASCII value: %d\n", letter); // 65
// Character classification (<ctype.h>)
printf("Is letter? %d\n", isalpha('A')); // 1
printf("Is digit? %d\n", isdigit('9')); // 1
printf("Is uppercase? %d\n", isupper('A')); // 1
return 0;
}
Arrays
#include <stdio.h>
int main() {
// Declaration
int numbers[5]; // Uninitialized
int zeros[5] = {0}; // All zeros
int primes[] = {2, 3, 5, 7, 11}; // Size inferred: 5
int grid[3][4]; // 2D array: 3 rows, 4 columns
// Initialization
int scores[3] = {95, 87, 92};
scores[0] = 100; // Modify first element
printf("First score: %d\n", scores[0]); // 100
printf("Array size: %zu\n", sizeof(scores)); // 12 bytes (3 × 4)
printf("Element count: %zu\n", sizeof(scores) / sizeof(scores[0])); // 3
return 0;
}
Structures
Group related data together:
#include <stdio.h>
#include <string.h>
struct Point {
int x;
int y;
};
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Point p1 = {10, 20};
struct Person person = {"Alice", 30, 5.7f};
printf("Point: (%d, %d)\n", p1.x, p1.y);
printf("Name: %s, Age: %d, Height: %.1f\n",
person.name, person.age, person.height);
// Modify fields
p1.x = 15;
person.age = 31;
return 0;
}
Unions
Share memory between different types:
#include <stdio.h>
union Data {
int integer;
float decimal;
char character;
};
int main() {
union Data data;
data.integer = 42;
printf("int: %d\n", data.integer); // 42
data.decimal = 3.14f;
printf("float: %.2f\n", data.decimal); // 3.14
printf("int (overwritten): %d\n", data.integer); // Garbage
// Size = size of largest member
printf("Size: %zu bytes\n", sizeof(union Data)); // 4
return 0;
}
Enums
Named integer constants:
#include <stdio.h>
enum Color { RED, GREEN, BLUE }; // 0, 1, 2
enum Status { OK = 200, NOT_FOUND = 404, ERROR = 500 };
enum Day { MON = 1, TUE, WED, THU, FRI, SAT, SUN };
int main() {
enum Color favorite = BLUE;
enum Status code = NOT_FOUND;
printf("Favorite: %d\n", favorite); // 2
printf("Status: %d\n", code); // 404
return 0;
}
Type qualifiers
#include <stdio.h>
int main() {
// const: value cannot change
const int MAX = 100;
// MAX = 200; // Compiler error
// volatile: tells compiler value may change externally
volatile int sensor_reading = 0; // Could change via hardware
// restrict: hints for optimization (C99+)
// Only in function parameters — tells compiler pointer is exclusive
printf("MAX: %d\n", MAX);
return 0;
}
Type aliases with typedef
#include <stdio.h>
typedef unsigned int uint;
typedef struct {
float x, y, z;
} Vector3;
int main() {
uint count = 42;
Vector3 position = {1.0f, 2.0f, 3.0f};
printf("Count: %u\n", count);
printf("Position: (%.1f, %.1f, %.1f)\n",
position.x, position.y, position.z);
return 0;
}
Mini Practice
Write C code that:
- Creates variables of each integer type and prints their sizes
- Defines a
struct Bookwith title, author, and pages - Creates an enum for days of the week
- Demonstrates the difference between
constandvolatile
Up Next
In the next lesson, you'll learn about Constants — defining fixed values in C.
Related Topics
Frequently Asked Questions about Data Types
What is Data Types in C?
Data Types is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Data Types?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.
Why is Data Types important in C?
Data Types is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.