PY
C

Python to C

10 lessons

Progress0%
1Variables & Types2Functions3Lists → Arrays4Memory Management5String Handling6Structs7Preprocessor8File I/O9Pointers and Manual Memory10Build System: Makefile
All Mirror Courses
PY
C
Variables & Types
MirrorLesson 1 of 10
Lesson 1

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.

Mirror Card
PY
From Python:

In Python, you're familiar with dynamic vs static typing.

C
In C:

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:

C
C 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:

PY
Python (What you know)
name = "Alice"
age = 30
score = 9.5
active = True

# Dynamic — type can change
x = 42
x = "hello"  # valid

# Big integers work automatically
big = 2**100
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C requires explicit type declarations; Python infers types dynamically

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C strings are null-terminated char arrays; Python strings are objects

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C integers overflow silently; Python integers have arbitrary precision

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

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.

PY
Python
age = 30
C
C
int 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'.

PY
Python
s = "Alice"  # object with .upper(), .split(), etc.
C
C
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.

C
C
int x = 2147483647; // INT_MAX
x++;  // wraps to -2147483648! No error.
Common Pitfall
Always use the right integer type for your range; consider overflow in arithmetic.

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
Common Pitfall
Don't assume C works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

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)
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C to practice these concepts.
OverviewNext