Variables & Types
Dynamic vs static typing
Introduction
In this lesson, you'll learn about variables & types in C. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with dynamic vs static typing.
C has its own approach to dynamic vs static typing, which we'll explore step by step.
The C Way
Let's see how C handles this concept. Here's a typical example:
#include <stdio.h>
#include <stdbool.h>
/* Every variable needs an explicit type */
char name[] = "Alice"; /* string = char array + \0 */
int age = 30;
double score = 9.5;
bool active = true;
/* Type CANNOT change */
int x = 42;
/* x = "hello"; */ /* compile error */
/* No arbitrary precision — use long long for big numbers */
long long big = 9223372036854775807LL;Comparing to Python
Here's how you might have written similar code in Python:
name = "Alice"
age = 30
score = 9.5
active = True
# Dynamic — type can change
x = 42
x = "hello" # valid
# Big integers work automatically
big = 2**100You may be used to different syntax or behavior.
C requires explicit type declarations; Python infers types dynamically
You may be used to different syntax or behavior.
C strings are null-terminated char arrays; Python strings are objects
You may be used to different syntax or behavior.
C integers overflow silently; Python integers have arbitrary precision
You may be used to different syntax or behavior.
C has no bool natively before C99; Python has True/False
Step-by-Step Breakdown
1. Explicit Types
C requires every variable to have a declared type. The compiler catches type mismatches before the program runs.
age = 30int age = 30;2. Strings as Char Arrays
Python strings are objects with methods. C strings are just arrays of bytes ending with a null character '\0'.
s = "Alice" # object with .upper(), .split(), etc.char s[] = "Alice"; /* {65,108,105,99,101,0} */3. Integer Overflow
Python integers grow automatically. C integers have fixed sizes and silently wrap around on overflow.
int x = 2147483647; // INT_MAX
x++; // wraps to -2147483648! No error.Common Mistakes
When coming from Python, developers often make these mistakes:
- C requires explicit type declarations; Python infers types dynamically
- C strings are null-terminated char arrays; Python strings are objects
- C integers overflow silently; Python integers have arbitrary precision
Key Takeaways
- All variables need explicit type declarations in C
- C strings are char arrays with null terminator
- C integers overflow; Python integers don't
- C requires #include <stdbool.h> for bool (C99)