1.2 Python to C
To learn C from a Python background, here are key steps to help you transition:
-
Understand Syntax Differences:
- C is statically typed (you need to declare variable types), while Python is dynamically typed.
- Example:
int x = 10;
-
Memory Management:
- C requires manual memory management using
malloc()
andfree()
for dynamic memory allocation, while Python handles this automatically.
- C requires manual memory management using
-
Pointers:
- C uses pointers to reference memory locations. Grasp how to use pointers and understand pointer arithmetic.
-
Functions and Libraries:
- C standard libraries like
<stdio.h>
and<stdlib.h>
are used for I/O and memory management. Functions need explicit return types and argument declarations.
- C standard libraries like
-
Build Process:
- C requires compiling with a compiler like
gcc
, while Python is interpreted. Learn to compile, link, and execute C code.
- C requires compiling with a compiler like
-
Debugging:
- Unlike Python, C can lead to segmentation faults due to memory misuse. Learn to use debugging tools like
gdb
orvalgrind
.
- Unlike Python, C can lead to segmentation faults due to memory misuse. Learn to use debugging tools like
-
Practical Projects:
- Start with simple programs like basic calculators, file I/O operations, and eventually implement data structures.
-
Resources:
- Books like “The C Programming Language” by Kernighan and Ritchie.
- Online platforms like Learn-C.org or Exercism.
1. Hello World
Python | C |
---|---|
|
|
2. Use of Semicolon ;
The semicolon (;
) is treated differently in Python and C:
In C:
-
Semicolon is required to terminate statements: Each statement in C must end with a semicolon to indicate the end of the command.
Example:
int x = 5; // Semicolon requiredprintf("%d\n", x); // Semicolon requiredWithout a semicolon, the compiler throws an error.
In Python:
-
No semicolons are required: Python uses newlines and indentation to define the end of a statement or block of code. Semicolons are optional but rarely used.
Example:
x = 5 # No semicolon neededprint(x) # No semicolon needed -
Optional use: You can use semicolons to write multiple statements on a single line, but this is not typical Python style.
Example:
x = 5; print(x) # Multiple statements on one line using a semicolon
3. User Input
Python | C |
---|---|
|
|
3.1 Control Structures
Control structures are constructs in programming that dictate the flow of execution based on conditions, loops, and decision-making. They help manage how and when specific parts of the code are executed.
3.1.1 If Statement
- Python: Uses indentation to define code blocks.
- C: Uses braces
{}
to group code blocks
Python | C |
---|---|
|
|
3.1.2 For Loop
- Python: Uses for item in
range()
. - C: Uses initialization, condition, and increment in a more explicit form.
Python | C |
---|---|
|
|
3.1.3 While Loop
- Both languages use while, but C requires braces
{}
for blocks.
Python | C |
---|---|
|
|
3.1.4 Swtich Statement
- C: Includes switch-case for multi-branch decision-making, which Python doesn’t have directly.
Python | C |
---|---|
|
|
3.1.5 Ternary Operator
- Python: Uses conditional expressions, which are more readable but serve a similar purpose.
- C: Supports the ternary operator
?
:
, a shorthand for if-else.
Python | C |
---|---|
|
|
3.2 Function Calls
3.2.1 Main Function
Python | C |
---|---|
|
|
3.2.2 Returning Values
Python | C |
---|---|
|
|