VS compiler common problems

Hits: 0

VS [compiler] common problems


When we start to learn the C language, we will encounter various compilers, VS, Dev-C++, VC++6.0, etc. Of course, the compiler is just a learning tool. We will not do too much here. judge. Here we will make a simple solution to the problems often encountered by friends who use VS2013 and VS2019.

Problem 1 – VS2013 console flashed by

As the saying goes: the beginning of everything is difficult. Some friends use VS2013 to write a hello world, but the result is not printed. I think: I just wrote a hello world, and I encounter problems. Why is this programming so difficult? In fact, it is not that the result is not printed, but the console flashes, and of course the solution is very simple.

Solution 1

The first step
is to find the solution explorer (if not, find the view in the upper menu bar, and then find the solution explorer in the view), right-click the mouse in the project you created, find properties, and click Properties.
Step 2
After clicking Properties, the property page window will pop up, click in order: Configuration Properties -> Connector -> System, then select the console in the subsystem, and click OK to complete.

Solution 2

Add system(“pause”) below the print hello world; because system is used, the library should be imported. Although this method can be solved, it is not a once and for all method, so it is recommended to use the solution 1
PS: This problem will only appear on VS2013, and VS2019 will not have this problem, because the VS2019 subsystem defaults to the console.

Question 2 – the scanf function reports an error

This problem is also a problem encountered by Xiaobai when he first installed the VS compiler (both VS2013 and VS2019). Every time he encounters input scanf, he always encounters such an error.

#include <stdio.h>
int main()
    int a = 0;
    scanf("%d", &a);
    printf("%d\n", a);
    return 0;

This will inevitably give Xiaobai another blow, so how to solve it? We can see that this error is that scanf is not safe, we need to use the scanf_s() function, so we only need to change scanf to scanf_s, but there is another problem: scanf_s is VS’s own input function, so it is not It is portable, that is to say, if we put the code into other compilers, the scanf_s function will not be recognized, so we need to find other solutions.


We can see that the above error message mentions such a sentence: use _CRT_SECURE_NO_WARNINGS. If we want scanf not to report an error, we only need to put such a sentence at the beginning and modify it with #define (the latter 1 can be added or not). like this:
In this way, there will be no errors, but if you need to add such a sentence every time, it will be too troublesome. So how do we do it?
first step:
Find your own VS installation directory.
Step 2:
Enter the directory: Visual Studio IDE–>Common7–>IDE–>VC–>VCProjectItems, you can see a newc++file at this time.
third step:
Open Notepad, enter the sentence #define _CRT_SECURE_NO_WARNINGS 1, and then save (save or save to the original location). In this way, you can add #define _CRT_SECURE_NO_WARNINGS 1 directly after creating a new file.

Problem 3 – Errors caused by creating multiple source files

In the early stage of learning programming, some friends do not like to comment out the original code, but prefer to use the main() function for new items, so that they can see multiple source files, so that their code becomes organized , It is a good habit to have this habit, but it also brings a problem. For example:
we created two source files: test.c and test1.c.
When we compile one of them, an error will be reported:
Many Xiaobai are confused here, what is this? In fact, it is because you have two main() functions. We know that there can only be one main() function in a project. However, test.c has a main() function, and test1.c has a main() function. The compiler does not know which entry to the program is, so an error will be reported. Knowing the cause, the solution is simple.

Solution 1

If you want to compile the test1.c file, then we can shield the test.c file.
Step 1:
Find test.c, right-click, and find properties.
Step 2:

Find General in Configuration Properties, select “Yes” in the Exclude from Build drop-down box, and click OK. This way the compiler excludes the compilation process of test.c.

Solution 2

Just annotate the main() function directly. If it is a normal code practice, we don’t need to create a new .c file to rewrite the main() function at all. We only need to write a small exercise and write the main() The function is commented out, and the main() function can be rewritten. If you want to create a new .c file to rewrite the main() function, you need to comment out the main() function that is not needed, so that no error will be reported.
Well, these are some of the more common problems with the VS compiler. If it helps you, don’t forget to like it. If you have any questions or suggestions, please send a private message and comment, thank you!

Leave a Reply

Your email address will not be published.