Syntax, types, and compiling
โ Report an issue with this lesson#include <stdio.h>
int main() {
int age = 28;
float price = 29.99;
char grade = 'A';
printf("Age: %d, Price: %.2f\n", age, price);
return 0;
}
gcc main.c -o main
./main
Unlike Python or JavaScript, C is compiled ahead of time into a binary
โ gcc turns your source into an executable your OS runs
directly, with no interpreter involved.
C's basic types don't have a fixed size guaranteed by the language --
only a minimum range. On most modern desktop systems, int is
32 bits, float is 32 bits, and double is 64 bits,
but relying on that without checking is a real source of portability bugs
on embedded or older systems. When the exact size matters, the standard
library gives you explicit-width types instead:
#include <stdint.h>
int32_t exact_count = 100; // guaranteed exactly 32 bits, wherever this compiles
uint8_t small_flag = 1; // guaranteed exactly 8 bits, unsigned
printf("int is %zu bytes\n", sizeof(int)); // sizeof returns size_t, printed with %zu
signed vs unsigned matters more than it might
seem. An unsigned int can never go negative -- subtracting
past zero wraps around to a very large positive number instead of raising
an error:
unsigned int count = 0;
count = count - 1; // wraps to 4294967295, not -1
int signed_count = 0;
signed_count = signed_count - 1; // -1, as expected
This "unsigned underflow" is a genuinely common real bug -- especially
around array lengths and loop counters, which are often
size_t (an unsigned type). A loop like
for (size_t i = length - 1; i >= 0; i--) never terminates
normally, because i can never go below zero -- it wraps
around to a huge number instead and keeps looping, reading far past the
array.
Format specifiers in printf/scanf have to
match the type exactly -- %d for int,
%f for float/double, %c
for char, %s for a C string, %ld for
long. Getting this wrong doesn't raise a compile error the way
a type mismatch would in a language with a real type checker -- it's
undefined behavior, and often prints garbage or crashes at runtime instead
of failing where the mistake actually is.
Try it yourself
Run your code and get it working before marking this lesson complete.
8 more lessons โ including Capstone project: a linked list library โ plus a certificate are waiting.
Unlock the full course โ $149.99