Getting to know C language (the most complete C language in the whole network from entry to proficiency)

Table of contents

foreword

1. What is the C language? 

 2. The first C language program

1. Create a new project

2. Create a .c file

3. Write code

4. main function


foreword

Hello everyone, starting today I will share with you all kinds of knowledge about C language, and there are still questions, let us learn together, from the beginning to the proficiency of C language. Not much to say, let us enter the learning of C language together.


1. What is the C language? 

C language is a general-purpose computer programming language, which is widely used in low-level development. The design goal of the C language is to provide a way to compile in a suggested way, handle low-level memory, generate a small amount of machine code, and run without any runtime environment support.

programming language.

Although the C language provides many low-level processing functions, it still maintains a good cross-platform feature. A C language program written in a standard specification can be compiled on many computer platforms, even including some embedded processors (single chip microcomputers). or MCU) and supercomputers and other operating platforms.

In the 1980s, in order to avoid differences in the C language grammar used by various developers, the US National Bureau of Standards formulated a complete set of American National Standard grammar for the C language, called ANSI C, as the initial standard of the C language. .

C language is a process-oriented computer programming language, which is different from object-oriented programming languages ​​such as C++ and java.

Its compilers mainly include Clang, GCC, WIN-TC, SUBLME, MSVC, Turbo C, etc.

 2. The first C language program

Let's write the first C language code now, so how to write C language code? The compiler we use here is VS2019/2022. Here we take VS2019 as an example.

1. Create a new project

As shown in the figure, after opening VS2019, we can create a new project.

 When we are done, we click "Create New Project" to create an empty project. (as the picture shows)

 Attention everyone, if there is no empty project in our new project, it must be that we installed it wrong during installation, and we can reinstall it. Then we go to the next step.

After clicking Next, you will enter the configuration new project interface, where we can set the project name, adjust the project location, and where the settings are stored, as shown in the figure below:

Here is a small point to remind everyone that when we edit the project name, try not to use Chinese, because VS2019 is not very compatible with Chinese, just in case we can use English letters to express, at the same time, the project location should not be Use the default, because the default generally occupies the system disk, and we can manually adjust the storage location.

After setting the project name and location, we click Create to enter VS2019 to write code, as shown below:

When we see the picture below, it means our project is created. 

 In this way, we have completed the first step of creating a new project.

Supplement: There is one point that needs to be supplemented here. After entering VS2019, some of our friends did not see the solution to the resource manager, as shown in the figure below:

This time is actually very easy to solve, we only need to click the view option above, as shown in the figure:

After finishing, click on the solution resource manager, so that our solution resource manager can be called out. 

2. Create a .c file

In a C language program, there are generally 2 files:

One is a file with a suffix of .h, where h is actually a header, which is what we call a header file.

Another type of file is the .c file we want to use, called the source file.

We need to write some simple files first, so we don’t need to use the .h header files. We will learn the .h header files later, and here we only need to use the source files.

We need to create a source file, open the solution explorer, find the source file in it, right-click Add, and click New Item, as shown in the following figure:

 After clicking the new item, we will go to the page of adding a new item, as shown in the figure:

 We will find that there are only C++ files here, and there are no C files. Here I will add to you. In fact, both C and C++ choose the above C++ files. There is no separate file for C here. Everyone needs to understand here one time.

After finishing, let's look at the name. Here our name suffix is ​​.cpp. Please note that if we write a c language program, we need to change the file suffix .cpp to .c. If we write c++, the file The suffix is ​​.cpp.

Here, .cpp means .cplusplus, which is what we mean by c++, and what we write with the suffix .c is our c language. If you write .cpp, the compiler will compile according to the syntax of c++, so when we write c language, it is recommended that you use .c as the suffix, so that the compiler will use the syntax of c language to run the code. Everyone needs to understand this concept.

 Here we changed the project name to .c as the suffix. Note that the name should not use Chinese as much as possible. Here I wrote it as test.c. After we are done, we click Add, as shown in the figure:

 After clicking Add, we are done adding a new item. As shown below:

 The .c file is created here, and it opens the file we created by default. At this point, the second step of creating the .c file is complete.

3. Write code

Here, let's write a simple c language code to print hello world on the screen, as shown in the figure:

 As shown in the picture, my code has been written, so how to run it? Here is something to add to everyone.

The c language code we wrote is text information, and these codes cannot be run directly, so how do we make it run? Note here that the .c file we wrote has to go through the process of "compiling + linking" to finally generate our "executable program".

So how do we compile it? Here I want to tell you that the VS2019 or 2022 we use is called an integrated development environment, and its functions are already very powerful, including: editing, compiling, linking, running, debugging and other functions. And our compiling + linking + running can be done in one step on the computer, that is, we only need to hold down the computer’s Ctrl+F5, or some students’ computers are notebooks, you need Ctrl+Fn+F5. The code starts to run, as shown below:

This is very convenient for us, I just pressed a Ctrl+F5, and he completed compiling, linking and running, and output our code. At this point, some students will say, then I don’t need to press Ctrl, I can just press F5 directly. Here, I will not explain to you, but pressing F5 is a wrong way. Pressing F5 alone is called debugging, not Compile + link + run, everyone needs to remember here.

In this way, our first C language code has been written. Some students said that we didn't talk about how to write code. I still don't know how to write code. Don't worry, let's start now, step by step.

4. main function

First of all, when we write code, we need to start writing from the main function, that is, the main function:

int main()
{
    return 0;
}

This is a standard way of writing the main function. Among them, main is the name of this function, which cannot be changed arbitrarily, and the int inside means integer in C language, and the int here echoes the return 0 below, 0 is an integer, So what we wrote earlier is int, here echoes each other. The curly braces that wrap return 0, here we call the function body, and the curly braces cannot be scribbled.

At this time, some students will wonder, why do we write the main function? Next, let me tell you what the function of the main function is.

In the C language, the main function is the entry point of the program, and the program is executed from the first line of the main function.

 Everyone should pay attention here, be careful not to write main wrong, otherwise the code will not be able to run.

Moreover, there is only one main function. Once there are two main functions in one code, the code will report an error, so please remember that in C language, there is only one main function.

At this point, our first C language code is finished. This time I will come here first, I will send the code to my Gitee and Github, please pay attention to me.

Gitee account: milk tea (lk-love-hxl) - Gitee.com

Next time I will learn data types with you, thank you for your support.

Guess you like

Origin blog.csdn.net/lm_love_hxl/article/details/131051607