C Basics 01
Summary of C Language Basics - 01
Introduction
I recently revisited the basics of C language systematically, and it has been quite rewarding. Many concepts that previously seemed mystical have become simpler after systematic study. I’m summarizing my learning here as a review of my recent studies.
The most important parts of C language are pointers and memory, which are also the two main criteria for assessing mastery of C language. However, we won’t dive into these two topics just yet; let’s start with the basics.
The Four Steps of C Language Compilation
Most people who are new to C language use integrated development tools (e.g., VS) directly, and may not be familiar with the basic steps of C language compilation. Actually, C language compilation and debugging only require installing a compiler (gcc) and a debugger (gdb). I highly recommend using VS Code for setting up the environment, as VS Code is lightweight and can run multiple codes through environment variable configurations, making it convenient for learning other languages in the future. If you don’t know how to set up the VS Code C/C++ environment, you can check out an article I wrote earlier. Now, let’s get straight into the topic.
Create a new hello.c file, open it with a text editor, and write a simple program:
1 |
|
Then save and exit. Enter cmd in the address bar and press Enter to open the terminal with the current path as the working directory.
Preprocessing Stage:
Expanding header files, replacing macro definitions, deleting comments, and expanding conditional compilations (note: syntax is not checked, and everything can be expanded).
Implementation code (note the strict case sensitivity in command line operations):
1 | gcc -E hello.c -o hello.i |
This generates a .i file (preprocessed file) from the .c file (source file).
If you encounter an error, check your spelling or verify that the compiler is installed correctly.
Compilation Stage:
Syntax is checked line by line (this is the most time-consuming part of the compilation process), and if there are no errors, the code is translated into assembly language.
Implementation code:
1 | gcc -S hello.i -o hello.s |
This generates a .s file (assembly file) from the .i file (preprocessed file).
Assembly Stage:
The assembly language is translated into binary machine code.
Implementation code:
1 | gcc -c hello.s -o hello.o |
This generates a .o file (object file) from the .s file (assembly file).
Linking Stage:
Library inclusion, data segment merging, and data address backfilling (just a concept for now, not sure what it specifically means).
Implementation code:
1 | gcc hello.o -o hello.exe |
This generates a .exe file (executable file) from the .o file (object file).
Then run the .exe file:
1 | hello.exe |
In fact, the above steps can be completed directly in one step:
1 | gcc hello.c -o hello.exe |
The above breaks down into the four steps corresponding to the four compilation steps.
Syntax errors in C language files are reported by gcc, and if the code runs without issues but fails to achieve the desired functionality, then you may need to use gdb for debugging. There are plenty of excellent resources online if you want to delve deeper.
- Title: C Basics 01
- Author: LHT
- Created at : 2021-09-28 23:34:37
- Link: https://blog.327774.xyz/2021/09/28/C/C-basics-01/
- License: This work is licensed under CC BY-NC-SA 4.0.