Variables & Primitive Types
C is a statically-typed language. Every variable must have an declared data type specifying the exact layout size of memory to allocate for it on stack registers.
1 Core Primitive Types & Specifiers
The standard primitives in C are basic numerical blocks:
| Type | Typical Size | Format Specifier | Description |
|---|---|---|---|
| char | 1 byte | `%c` | Single ASCII character |
| int | 4 bytes | `%d` or `%i` | Integer numerical values |
| float | 4 bytes | `%f` | Single-precision floating points |
| double | 8 bytes | `%lf` | Double-precision floating points (default decimal representation) |
Qualifiers: You can modify ranges using qualifiers like `short`, `long`, `long long`, or `unsigned` (which handles positive values only, doubling the positive range capacity):
- `unsigned int score = 5000;` (Does not store negative values)
- `long long bankBalance = 99999999999LL;` (Utilizes format specifier `%lld`)
2 Declaration, Initialization, & Specifiers
Let's run a program declaring different C types and printing them out with their matching formatting parameters:
C — Data Types & Specifiers
▶ Run Code
#include <stdio.h>
int main() {
char grade = 'A';
int score = 95;
float temp = 98.6f;
double pi = 3.1415926535;
unsigned int id = 452291;
// Formatting output using placeholder parameters
printf("Grade: %c\n", grade);
printf("Score: %d\n", score);
// Controlling decimal precision formatting
printf("Temperature (default): %f\n", temp);
printf("Temperature (2 decimals): %.2f\n", temp);
printf("Pi (8 decimals): %.8lf\n", pi);
printf("Unsigned ID: %u\n", id);
return 0;
}
3 Code Challenge
Challenge: Write a program that calculates the area of a circle with a radius of `5.5`. Define radius and area as `double` variables, use `3.14159` as pi, compute the area, and print the output formatted to exactly 4 decimal places.