C Data Types Explained: int, float, char, and More – A Complete Guide
C Data Types
This tutorial demystifies the fundamental data types in C—int, float, char, and others—so you can write precise, efficient code.
Video: Data Types in C Programming
In C, every variable is declared with a data type that defines its size and value range. For example:
int myVar;
Here, myVar is an int (integer) variable, typically occupying 4 bytes.
Basic Types
Below is a quick reference for the most commonly used C data types.
| Type | Size (bytes) | Format Specifier |
|---|---|---|
int | at least 2, usually 4 | %d, %i |
char | 1 | %c |
float | 4 | %f |
double | 8 | %lf |
short int | 2 usually | %hd |
unsigned int | at least 2, usually 4 | %u |
long int | at least 4, usually 8 | %ld, %li |
long long int | at least 8 | %lld, %lli |
unsigned long int | at least 4 | %lu |
unsigned long long int | at least 8 | %llu |
signed char | 1 | %c |
unsigned char | 1 | %c |
long double | at least 10, usually 12 or 16 | %Lf |
int
An int represents whole numbers—zero, positives, and negatives—without decimal points. Typical examples include 0, -5, and 10.
Declare an integer variable like this:
int id;
The variable id now holds an integer. You can also declare multiple integers simultaneously:
int id, age;
On most platforms, int is 4 bytes (32 bits) and can represent 232 distinct values ranging from –2,147,483,648 to 2,147,483,647.
float and double
Use float and double to store real (floating‑point) numbers.
float salary;
double price;
Both types support scientific notation, e.g.,
float normalizationFactor = 22.442e2;
Key difference: float (single precision) occupies 4 bytes, whereas double (double precision) occupies 8 bytes.
char
The char keyword declares a single character variable. Example:
char test = 'h';
Its size is exactly 1 byte.
void
void represents the absence of type. It’s used for functions that return no value:
void printMessage() {
printf("Hello, world!\n");
}
You cannot create variables of type void.
short and long
When you need a larger numeric range, use long or long long:
long a;
long long b;
long double c;
For small integer ranges (–32,767 to 32,767), short suffices:
short d;
Check a type’s size with sizeof():
#include <stdio.h>
int main() {
short a;
long b;
long long c;
long double d;
printf("size of short = %d bytes\n", sizeof(a));
printf("size of long = %d bytes\n", sizeof(b));
printf("size of long long = %d bytes\n", sizeof(c));
printf("size of long double= %d bytes\n", sizeof(d));
return 0;
}
signed and unsigned
Modifiers that control a type’s value range:
signed– allows negative and positive values.unsigned– restricts to non‑negative values only.
Examples:
// valid
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int
// invalid – unsigned cannot hold negative numbers
unsigned int num = -35;
With a 4‑byte int, y spans –231 to 231‑1, while x spans 0 to 232‑1.
Derived Data Types
Derived types build on fundamental types: arrays, pointers, structures, and functions. Further tutorials will cover these in depth.
- bool type
- Enumerated type
- Complex types
C Language
- Master C# Variables & Primitive Data Types: A Complete Guide
- Understanding C++ Data Types: Fundamentals, Modifiers, and Best Practices
- Four Proven Patterns for User‑Defined Functions in C
- Mastering Python Data Types: A Practical Guide
- Java Primitive Data Types: A Complete Guide with Examples
- C# Data Types Explained: Int, Double, Bool, String & More
- Verilog Data Types: Bits, Wires, and Logic Values Explained
- Understanding C Data Types: A Comprehensive Guide
- Understanding MATLAB Data Types: A Comprehensive Guide
- Comprehensive Guide to C# Data Types: Value, Reference, and Pointer