Relearning C Language: Lesson Two

Course Address:https://www.cc4e.com/

This lesson, From Python to C, mainly teaches how to quickly switch from Python to C language.

C is the mother of high-level programming languages, and Python itself is written in C. Many programming languages borrow syntax from C.

The instructor does not want this to be your first language course; rather, they hope you have learned previous languages (don’t worry, we will update related content).

Differences between Python and C

In Python, whitespace (indentation) is necessary and part of the syntax, while this aspect is overlooked in C.

Python is very object-oriented, while C is not object-oriented at all.

Python has comfortable data structures, such as lists and dictionaries, while C does not; C has structures and pointers, which are efficient and powerful, and it runs very fast.

The common theme of the entire course is how Python achieves its goals by writing C code.

By the end of the course, you will understand how Python automatically allocates memory.

Python introduces many libraries, and indentation is mandatory.

Although the instructor advises against copying and pasting, cheating, or searching for answers online, they support you copying their code to submit for assignments because they want to act as a “tiger mom,” giving you something to grasp and encouraging you to paste C code, run it, and understand it. They hope you can connect your existing complex Python knowledge with C language concepts.

Similarities and Differences between C and Python

Similarities

Arithmetic operators: + – * / %

Comparison operators: == != <> <=

Naming rules: letters/underscores + numbers/letters/underscores, case-sensitive

While loops include break and continue

Constants are similar (except for strings, letters, and boolean types)

Both have int / float and char / byte

C does not have strings, lists, or dictionaries

Python does not have structures or double precision floats

Differences

Boolean logical operators: and, or, not vs. &&, ||, !

C loops must have termination conditions; there are no direct iteration operations like in Python.

C does not have predefined true and false values.

None and NULL are similar concepts, but there are many differences; None is a specially marked empty type, while NULL is equivalent to zero in mathematics.

Strings and character arrays are similar concepts, but they differ significantly; character arrays are like unsupported strings.

C does not have lists, tuples, or dictionaries.

Python does not have structures, and C does not have double precision floats.

Python comments: # single line comment

C comments: /* multi-line comment */

C language must have a main function definition, which can return parameters.

C does not have built-in input/output, so input/output must use the standard library stdio.h.

In C, ” corresponds to a single character, while “” corresponds to a character array.

Python’s print function implicitly adds a newline, while C must explicitly write \n for a newline.

%d means there is an integer here, and I want you to convert it to a string for printing.

%.1f prints a floating-point number with one decimal precision.

%s outputs “string” (which is actually a character array).

“Sarah” actually consists of six characters because there is a terminating zero character at the end.

C must declare your variable types, while Python does not require this initially.

In scanf, & is used to get the address, not the value, as it is called by reference.

C arrays must have a defined length; their length is fixed and will not automatically expand.

char name[100]; if you input 20 characters, 80 characters are fine; if you input 101, it will crash.

%100s reads a maximum of 100 characters; note that in this case, input is by value, not by address, because name is an array.

How to read a whole line? scanf(“%1000[^
]”, line);

This reads until the end of the line or up to 1000 characters.

You can also use fgets(line, 1000, stdin); to read 1000 characters from the standard input stream.

C has three standard streams: standard input (keyboard input), standard output (screen output), and standard error (screen output).

C for loops consist of three parts, separated by “;”.

Initialization; condition to continue the loop; variable change condition.

for(i = 0; i < 5; i++) {

}

Difference between elif and else if

In Python, elif is a reserved word, while in C, else if consists of two reserved words. Elif is a true multi-way if, while else if is not.

How to understand this? Elif and if are parallel, while else if and else are like having an if and else inside else, which are at different levels.

However, for beginners, not knowing this is not a big issue.

C function calls must declare parameter types, while Python does not.

To summarize,

This lesson briefly covered:

Input/Output

Loops

File reading

Strings

Floating-point types

The upcoming lessons will teach how to implement Python’s dictionaries, lists, strings, etc., using C language.

Special note: If you need related PPT materials, you can add me with the note: CC4E.

Relearning C Language: Lesson Two

Leave a Comment