int / char / double / float
In C, every variable must be given a type. The type determines the size in memory and the kind of data that can be stored. The three most fundamental types are the integer type int, the character type char, and the double-precision floating-point type double.
Syntax
// Declares an integer variable. int variableName; int variableName = initialValue; // Declares a character variable (can also be used as a 1-byte integer). char variableName; char variableName = 'character'; // Declares a double-precision floating-point variable. double variableName; double variableName = initialValue; // Declares a single-precision floating-point variable. float variableName; float variableName = initialValue;
Basic Data Types
| Type | Size (typical) | Description |
|---|---|---|
| char | 1 byte | Stores a single character or a small integer (-128 to 127). Character literals are enclosed in single quotes. |
| int | 4 bytes | Stores an integer value. The size varies by environment, but is 32 bits (-2147483648 to 2147483647) on most platforms. |
| short | 2 bytes | Stores a short integer (-32768 to 32767). |
| long | 4–8 bytes | Stores a long integer. The size varies by environment. |
| float | 4 bytes | Stores a single-precision floating-point number. Provides about 7 significant digits. |
| double | 8 bytes | Stores a double-precision floating-point number. Provides about 15 significant digits, making it suitable for calculations that require higher precision. |
Sample Code
#include <stdio.h>
int main(void) {
// Declare and use int variables.
int age = 25;
int score = -100;
printf("Age: %d\n", age); // Outputs "Age: 25".
printf("Score: %d\n", score); // Outputs "Score: -100".
// Declare and use a char variable.
char grade = 'A';
char newline = '\n'; // Escape sequences are also supported.
printf("Grade: %c\n", grade); // Outputs "Grade: A".
printf("Character code: %d\n", grade); // Outputs "Character code: 65".
// Declare and use double variables.
double pi = 3.14159265358979;
double price = 1980.5;
printf("Pi: %.10f\n", pi); // Outputs pi to 10 decimal places.
printf("Price: %.1f\n", price); // Outputs "Price: 1980.5".
// Float literals require a trailing 'f'.
float temperature = 36.5f;
printf("Temperature: %.1f\n", temperature); // Outputs "Temperature: 36.5".
// Integer division truncates the fractional part.
int a = 7, b = 2;
printf("Integer division: %d\n", a / b); // Outputs "3".
printf("Floating-point division: %.1f\n", (double)a / b); // Outputs "3.5".
return 0;
}
Notes
C is a statically typed language, so you must declare the type of every variable before using it. Dividing two integers produces an integer result — the fractional part is truncated. When you need a fractional result, cast at least one operand to double before dividing.
The char type is internally treated as an integer. A character is stored as a numeric code (such as an ASCII value), which means you can print it as a character with %c or as a number with %d. Character literals are enclosed in single quotes (e.g., 'A') and are distinct from string literals.
The exact size of each type can vary between compilers and platforms. To determine the actual size at runtime, use the sizeof operator. For type qualifiers such as unsigned, signed, const, and volatile, see unsigned / signed / const / volatile.
If you find any errors or copyright issues, please contact us.