C is a statically typed programming language, which means that every variable must have a clearly defined type. Data types in C determine the amount of memory that a variable occupies, as well as the operations that can be performed on it. Simply put, in the C programming language, data types refer to things that are separate entities in other programming languages. That's why we'll learn them all at once. Just keep in mind that many things in the C programming language are limited and don't even have what the C++ programming language has, let alone what we can tell you about other more modern programming languages. There are four main categories of data types in C:
- Basic or primitive data types
- Certain type modifiers
- Complex data types
- User-defined types
Integers - Integer Types:
int - Used for whole numbers (e.g. 5, -10, 42). The size depends on the system architecture (usually 4 bytes on 32-bit or 64-bit systems).
short - A shorter integer (usually 2 bytes).
long - A longer integer (4 or 8 bytes, depending on the system).
long long - An even longer integer (at least 8 bytes, introduced in C99).
Modifiers:
signed - Allows positive and negative values (the default for int).
unsigned - Allows only non-negative values, which doubles the positive range (e.g. unsigned int).
Floating-Point Numbers - Floating-Point Types:
float - Single precision (usually 4 bytes), for decimal numbers (e.g. 3.14).
double - Double precision (usually 8 bytes), for greater accuracy of decimal numbers.
long double - Extended precision (size depends on the system, often 10, 12 or 16 bytes).
Characters - Character Type:
char - A single character (e.g. 'A', 'b') or a small integer (1 byte).
signed char - From -128 to 127.
unsigned char - From 0 to 255.
Logical Type - Boolean:
In standard C before C99, there was no special type, but since C99, _Bool is introduced, 0 for false, 1 for true. With the <stdbool.h> library, bool can be used as an alias for _Bool, along with true and false.
Empty Type - Void:
void - Indicates the absence of a type. It is used in functions that do not return a value or in pointers to an undefined data type void*.
Why Are Primitive Types in C Close to Hardware and How Does This Differ from Other Languages?