Create a new C project in Visual Studio 2010 environment

New C engineering project



Preface

There are many compilers used to learn C language. Common ones include VC++6.0, Dev-C++, Visual Studio, CodeBlocks and other environment software.
The functions of Visual Studio and CodeBlocks are slightly more powerful, but the operation steps are also relatively complicated. Dev-C++ is easy to operate and has relatively few functions. It is suitable for writing and learning C with simple basics. The code is enough.

Choosing a compiler generally depends on personal needs to choose a suitable and convenient one to use.

The following takes Visual Studio 2010 software as an example to create a C language engineering project for learning and use.

In the future, you can also use this project as a template, copy and save it as a new project, and add relevant module code to it, which saves the step of creating a new project every time.


1. Create a new empty project

Step 1: Double-click to open the VS 2010 software.

Insert image description here

After opening, the following interface appears:

Insert image description here

Step 2: Click New Project.

Insert image description here

As shown in the figure below, follow the labeling steps to create a new one.

Insert image description here

Step 3: Click Next by default.

Insert image description here

Step 4: Select console application as application type, and select empty project as additional option.
If you choose the precompiled header, the generated project will be a .cpp file, which is a C++ file, and the C style is still quite large and I don’t feel comfortable with it.

Finally click Finish.

Insert image description here

Step 5: After all the final steps are added, you can see an empty project framework, and just add the source files on it.

Insert image description here

2. Write a program

Step one: Add source code under the project file framework.
Click the steps to add a new item.

Insert image description here

Step 2: Save as xx.c file.

Insert image description here

It should be noted here that there is no C file option here, only C++. This is because most compilers have integrated C/C++, and the C++ environment can completely compile C code, so this does not It does not affect the use. Just change the suffix to .c when adding the source file name.

Step 3: After saving the xx.c file, you can see that there is a .c file under the project.

Insert image description here

Step 4: Double-click the .c file and add relevant code in it.
The simplest C program for beginners in C language is as follows:

Insert image description here

Step 5: After writing the code, click the compile button, and the system will automatically check for grammatical errors.

Insert image description here

Compilation without syntax errors will directly output the following content. If it fails, 0 means there are no errors.
========== Generation: 1 successful, 0 failed, 0 latest, 0 skipped ==========

Insert image description here

Then click the triangle button to start debugging, or press the keyboard shortcut F5.

Insert image description here

If there is no syntax error, the information result of the console will be output directly. If there is a syntax error, the following error will be prompted:

Insert image description here

At this time, you need to click No, double-click the error in the output prompt box, the cursor will automatically jump to the location of the error, and then check the syntax error at the specific location in the program and make modifications. (Debugging requires repeating this step process)

2.1 Solution to console crash

After clicking the run button, I found that the console flashed and then disappeared, and no output information could be seen at all.

After encountering this problem, I checked online and found that the program execution was too fast in this compilation environment. It could be solved by adding a statement to pause the program. (Dev-C++ clicks to run and debug without this situation)

You can use system(“pause”) or getchar() statements, their function is to pause the program.

It should be noted that: When using the system("pause") statement, you need to introduce the #include <stdlib.h> header file at the beginning of the code, otherwise system(" pause") cannot be used and the program reports an error.

Notes:
1. These two statements are placed in main. If there is a return, they must be placed in front of the return.
2. The getchar() statement cannot be used together with the scanf() statement. Otherwise, if you enter the content and hit Enter to exit the console directly, you will not be able to see the output information, so the program uses scanf() It can be used with the system("pause") statement after the statement.
3. Some compilation environments cannot use the system("pause") statement. In this case, you need to make a choice based on the compiler.

2.2 Hello World output

code show as below:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
    
    printf("Hello World!\n");

    system("pause");
    return 0;
}

The execution output is as follows:

Insert image description here

2.3 Execute the .exe file separately

When the project is closed, the compiled executable file can be executed independently. You can see an xx.exe file in the directory of the project Debug folder.

Insert image description here

Double-click this file to open it normally and see the output information, which is the same as when you click Run in the project.
Similarly, this file can be opened normally when placed on other computers.

Note: The exe file generated by double-clicking the compiler cannot be opened (a black window will flash and then disappear).
The solution is the same as the console above. When the program adds system("pause") or getchar() statements, the computer can open the .exe executable file separately, so that there will be no crash. . (Move to refer to 2.1 Console crash solution)

2.4 Introduction to project folders

The xx.sln file is a project file. You can open the entire project by double-clicking it.

Insert image description here


3. Summary

The above is the process of using Visual Studio 2010 to create a simple C project. Relatively speaking, the steps are a bit complicated, but the problem is not big. It takes no effort to sharpen the knife.

Finally, the first output statement of C language can be realized, "Hello World!".

You can officially start your C language learning journey anywhere! !

##Expansion-Modular Programming

A relatively large C project often does not only have a simple .c file, but also has many .c/.h file combinations.

Just add the .h file according to the steps.

Insert image description here

Select header file (.h)

Insert image description here

After the addition is completed, it will look like this:

Insert image description here

In this way, you can write the code in the corresponding .h file. After the writing is completed, introduce the corresponding header file in front of the .c file, and then call it in the main function.

demo.c
introduce header files and function calls

Insert image description here

demo.h
Sokan function copy

Insert image description here

test.h

Insert image description here

The compilation results are as follows:

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_53944340/article/details/130847256