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
Lists → Arrays
MirrorLesson 3 of 10
Lesson 3

Lists → Arrays

Sequential data storage

Introduction

In this lesson, you'll learn about lists → arrays 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 sequential data storage.

C
In C:

C has its own approach to sequential data storage, 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 <stdlib.h>
#include <stdio.h>
#include <string.h>

/* Fixed array */
int nums[8] = {1, 2, 3, 4, 5};
int len = 5; /* track length manually */

/* "Append" — manual management */
if (len < 8) nums[len++] = 6;

/* Dynamic array */
int *dyn = malloc(8 * sizeof(int));
int dyn_len = 0;
dyn[dyn_len++] = 1;
/* grow: */
dyn = realloc(dyn, 16 * sizeof(int));
free(dyn);

/* Slice: just use pointer + offset */
int *sub = nums + 1; /* points to nums[1] */
/* length of sub is 3 if we track it */

/* Sort with qsort */
int cmp(const void *a, const void *b) {
    return *(int*)a - *(int*)b;
}
qsort(nums, len, sizeof(int), cmp);

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
nums = [1, 2, 3, 4, 5]
nums.append(6)
nums.extend([7, 8])

length = len(nums)

sub = nums[1:4]
rev = nums[::-1]

doubled = [n * 2 for n in nums]

# Sort
nums.sort()
nums.sort(reverse=True)

for i, v in enumerate(nums):
    print(i, v)
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C arrays are fixed-size; Python lists grow dynamically

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C has no len() — track array length manually or use sizeof trick

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

No list comprehensions, slicing syntax, or .sort() method in C

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C
In C:

C dynamic arrays need malloc/realloc/free; Python's GC handles memory

Step-by-Step Breakdown

1. Fixed vs Dynamic

C arrays are fixed at compile time. For Python-like dynamic growth, use malloc and track the length yourself.

PY
Python
nums = [1, 2, 3]; nums.append(4)
C
C
int *arr = malloc(capacity * sizeof(int));
arr[len++] = 4; // manual length tracking
if (len == capacity) arr = realloc(arr, 2*capacity*sizeof(int));

2. No len() — Track Manually

C arrays don't know their own length. You must track it with a separate variable and pass both the array and its length to functions.

PY
Python
print(len(nums))
C
C
int n = sizeof(nums) / sizeof(nums[0]); // only for fixed arrays
printf("%d\n", n);

3. qsort

C's qsort requires a comparator function. It's more verbose than Python's list.sort() but equally powerful.

PY
Python
nums.sort()
C
C
int cmp(const void *a, const void *b) {
    return *(int*)a - *(int*)b;
}
qsort(nums, len, sizeof(int), cmp);

Common Mistakes

When coming from Python, developers often make these mistakes:

  • C arrays are fixed-size; Python lists grow dynamically
  • C has no len() — track array length manually or use sizeof trick
  • No list comprehensions, slicing syntax, or .sort() method in C
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

  • Fixed arrays or malloc for dynamic storage
  • Track length manually — no len() in C
  • No comprehensions, slicing, or append — use loops and manual management
  • qsort with comparator function for sorting
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C to practice these concepts.
PreviousNext