C in 100 Seconds: Variables and Types | Episode 2
Video: C in 100 Seconds: Variables — int float char double | Episode 2 by Taught by Celeste AI - AI Coding Coach
C Variables and Types: int, float, char, double
int age = 25;float price = 9.99;char grade = 'A';double pi = 3.14159265;. Four basic types, four format specifiers. The starting vocabulary of every C program.
C has just a handful of built-in types. The rest of the language builds on these primitives — pointers, arrays, structs, all eventually bottom out in int, float, char, or double.
The four types
#include <stdio.h>
int main() {
int age = 25;
float price = 9.99;
char grade = 'A';
double pi = 3.14159265;
printf("Age: %d\n", age);
printf("Price: %.2f\n", price);
printf("Grade: %c\n", grade);
printf("Pi: %.8f\n", pi);
return 0;
}
Output:
Age: 25
Price: 9.99
Grade: A
Pi: 3.14159265
int — integers
int age = 25;
printf("%d\n", age);
int holds whole numbers — positive, negative, or zero. Typical size is 4 bytes (32 bits) on most systems, range -2,147,483,648 to 2,147,483,647.
%d is the format specifier for "decimal integer." There's also %u (unsigned), %x (hex), %o (octal). Episode 3 covers them all.
For larger ranges, use long (8 bytes on 64-bit systems) or long long (always at least 8 bytes):
long population = 8000000000L;
long long world_total = 8000000000LL;
The L and LL suffixes tell the compiler "this literal is long/long long." Without them, big literals can overflow the default int.
float — single-precision floating point
float price = 9.99;
printf("%.2f\n", price);
float holds decimals with about 7 digits of precision. 4 bytes. Used when memory is tight (graphics, embedded systems) and rough precision is enough.
%f is the format specifier. %.2f says "2 decimal places."
9.99 is technically a double literal; the compiler converts to float for assignment. To suppress the warning, use 9.99f:
float price = 9.99f;
double — double-precision floating point
double pi = 3.14159265;
printf("%.8f\n", pi);
double is the default floating-point type — 8 bytes, ~15-17 digits of precision. Almost everywhere except where you specifically need float's smaller size, you want double.
%f is also the format for double. printf doesn't distinguish — both promote to double when passed to printf. (To pass a float value strictly, you'd actually need to cast — but in practice you can use %f for both.)
char — single character
char grade = 'A';
printf("%c\n", grade);
char is one byte. It can hold a character code ('A' is 65 in ASCII) or a small integer (-128 to 127, or 0 to 255 if unsigned char).
Single quotes 'A' for a character literal; double quotes "A" for a string of length 1 ("A" is two bytes — 'A' and a '\0' terminator).
%c prints the character glyph. %d prints the numeric value:
printf("%c is %d\n", 'A', 'A');
// A is 65
Format specifier cheat sheet
| Type | Specifier |
|---|---|
int |
%d |
unsigned int |
%u |
long |
%ld |
long long |
%lld |
float, double |
%f |
char (as char) |
%c |
char (as int) |
%d |
char * (string) |
%s |
| pointer | %p |
Wrong specifier = undefined behavior. The compiler will warn (with -Wall) but won't always catch.
Variables are typed at declaration
int x = 5;
x = "hello"; // ERROR: can't assign string to int
Once x is an int, it stays an int. Different from Python or JavaScript, where variables hold values of any type.
This is static typing — types are checked at compile time. Bugs are caught earlier; runtime is faster (no per-operation type checks).
Initialize when you declare
int x; // uninitialized — value is garbage
printf("%d\n", x); // could print any number
Local variables in C are not zero-initialized by default. They contain whatever was in that memory location previously. Always initialize on declaration.
int x = 0; // good
Global/static variables are zero-initialized:
int counter; // global — automatically 0
sizeof
printf("int: %lu bytes\n", sizeof(int));
printf("float: %lu bytes\n", sizeof(float));
printf("double: %lu bytes\n", sizeof(double));
printf("char: %lu bytes\n", sizeof(char));
sizeof is a compile-time operator that returns the size of a type or value in bytes. char is always 1; the rest depend on the platform (but typically 4 for int/float, 8 for double).
Common mistakes
Forgetting f on float literal. float x = 9.99; triggers a warning ("conversion from double"). Either use double or write 9.99f.
Using %d for a float. Prints garbage — printf reinterprets the float bytes as an int. Always match types.
Reading uninitialized variables. Garbage values, sometimes consistent enough that the bug only surfaces in release builds.
Confusing 'A' and "A". Single-quote = char. Double-quote = string (multi-byte). char x = "A"; doesn't compile.
Integer overflow. int big = 2147483648; (one more than max int) wraps around to negative. Use long for safety.
What's next
Episode 3: format specifiers in depth. %d, %x, %f, %s, %p, plus width and precision modifiers like %10d and %-10d.
Recap
int (whole numbers, 4 bytes), float (decimals, 4 bytes, ~7 digits), double (decimals, 8 bytes, ~15 digits), char (one byte, character or small int). Format specifiers: %d, %f, %c. Single-quote 'A' for char; double-quote "A" for string. Always initialize local variables; they're not zero-initialized like in Java/Python. sizeof(type) gives byte size.
Next episode: format specifiers.