C language you don’t know — the first part — compiling and running

Time:2019-12-18

I believe that many students of science and engineering have more or less learned C language in college, but due to the carelessness of teachers or the limitation of our knowledge at that time, we have many misunderstandings about C language. And when we learn C language, in order to be convenient and convenient, most people use IDE, which leads to our complete lack of concept of compilation process.
Now the blogger is a junior student. Fortunately, he read “one stop learning C programming” by Mr. Song Jinshan, and found many mistakes and vague understanding of C language. So he summed it up for his review and share with you
If there is any mistake, please correct it in time. Thank you!

Compile and execute

We are very familiar with the Hello world program that we have to write when we first learn to program. But even such a simple program, we may not have all-around control. Here is the Hello World Code:

#include <stdio.h>
int main(void)
{
        printf("Hello, world.\n");
        return 0;
}

To run it on Linux without any ide compilation, we need to enter a series of commands at the command line:

$ gcc main.c -o main
$ ./main
Hello, world.

GCC is the compiler of C language under Linux,gcc main.c -o mainIt means compiling main. C to generate the executable main, which can be named by itself.

For warnings at compile time, sometimes not all warnings will be given, but if we add the – wall parameter at compile time, the compiler will display all warnings for us to refer to and modify the program:gcc -Wall main.c -o main


Compile time and runtime

When you first learn C language, you will be confused by compile time and run time, because at that time, we used ide to run with one key, which seems to be the same for us. However, with the above knowledge, we can make a good distinction between compile time and run time.

The so-called compile time is the process of generating executable files after we input GCC compile files on the command line. The errors during this period are compile time errors. OnceAn error occurred during compilationJustNo moreWhen the executable is ready, the compilation is interrupted.

The runtime error is the error generated after compiling and generating the executable file, which is generally more difficult to catch.

For example, we have a rule when learning global variables and local variables:

Local variables can beArbitrarilyExpression to initialize, and global variablesOnly constant expressions can be usedTo initialize.

The reason why there is such a rule is that the global variables must be initialized when the program starts to run (before running any statements), so the initial value must be kept in the compiled executable file, so the initial value must be calculated at compile time. Therefore, in order to simplify the compiler, it is stipulated that global variables can only be initialized with constant expressions.

The key is to distinguishCompile timeandRuntime

Reference: one stop learning C programming, song Jingshan

Recommended Today

Redis (1)

Redis About redis Remote dictionary server (redis) is a key value storage system. Redis is an open source log type, key value database written in ANSI C language, complying with BSD protocol, supporting network, memory based and persistent, and providing API in multiple languages. It is often referred to as a data structure server, because […]