当前位置:网站首页>Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]

Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]

2022-04-23 15:02:00 VelvetShiki_ Not_ VS

VS2022 Debugging skills and precautions

  1. Code alignment : When editing code with the compiler , Programmers who don't pay attention to the cleanliness or format of the code may be confused by others , for example :

The code formats vary , As the command line increases, tidiness will affect the reading experience

  A quick and easy way is to Ctrl A Check all codes , Then press... In turn Ctrl K and Ctrl F You can quickly align code , And unify the format , After use, the effect of the above code is shown in the figure ( Note that the box is set in the format control character “” Spaces in will not be formatted uniformly , In this case scanf):

   2. understand .cpp Source file : When creating a project, there are always two files under the root directory , One by .cpp Represents the code source file , It is a code editing record without function operation function , That is, it is just a text that can be opened with any text editing software ( If the compiler is read-only ,TEXT Text editor or notepad++), But it cannot be operated by the compiler ; The latter is a solution file , It is used to store specific information about the source code .c File solution settings , Provide graphical interface and related information for displaying management files . In other words, the source code text running with this suffix can perform various user-defined functions , And you can continue the last progress work every time you continue the development , Such as layout format , Function writing, etc .

   3. understand debug and release edition :debug Debug version for program , That is, the program code that is still in the test and development stage , Also not yet released code phase .Release For the release version of the code , Translate it into release Of .exe The program file is the final version that the programmer wants the customer to see , But not directly for sharing , And the actual size ratio debug Lower version .

    4. Program execution and debugging shortcut key details :

①F5 And Ctrl F5 difference :F5 To debug a program , Attach a debugger while the code is running , The breakpoint set in the function can force the function to pause during the debugging run , And enable the debugger , Via shortcut key F10 Step by step or F11 Statement by statement debugging code , It's easier to find code errors bug, Or you can observe the execution time of statements and procedures , Optimize the code so that the time and space complexity of the code is less .

②Ctrl F5 To execute complete code without debugging , So this process only compiles and runs in the root directory of the code x64 Drop the target file and connect it .exe Executable program file , Ignore the setting of code break point and run the main function directly .

example : To print Hello World For example

  1. Create a new project and add a project named Hello World.c Source code file for , At this time, the root directory is not only used to edit the source file of the program language text , There are no other documents .
  2. Reference header file and main function , Just use printf Perform formatted output Hello World In the code , Move the cursor to printf On the command line , Press F9 Shortcut key to set breakpoint ( You can also find the new breakpoint in the debug tab ), Then press Ctrl F7 Source files can be compiled , Generate target file .obj, Then press the Ctrl F5 Execution procedure , Get as shown :
  3. Breakpoints are set, but the program is not printf stop , because Ctrl F5 Only the executable file connected by the target file is run .exe Without going through the debugging steps , So I won't stop and wait for the command to be issued .
  4. If you want to make the program run item by item as expected , That is, enter the main function and wait for the command to be issued before printing Hello World, You can press the shortcut key of debugging directly after setting the breakpoint F5, Enter the debugging stage :

① In debugging, the main function consists of main Entrance access , By default, it runs to the first place where the breakpoint is set , Wait for the command to be issued before running the line change statement , Notice that there is a small arrow pointing to the line with the breakpoint set :

② At this time, issue a running instruction to the debugger , Press the process by process debugging instruction F10 Or debug instructions sentence by sentence , The program executes the current line , When finished, it goes to the next breakpoint ( If it exists ):

   

    ③ Press again F10, Program execution return 0, The main function has finished running , But at this time, the debugging is still in progress , Only wait until debugging           The instruction completely jumped out of the main function range ( The entire function field referenced by braces ), Do not wait for user confirmation at this time , Straight out         Program .

④ Close the program without waiting for user confirmation , This also happens when you directly run the executable generated by the source code .exe Or no parameter input without breakpoints during debugging , That is, program execution / Debug , But the user has no time to see and confirm , When the program is finished, it closes :

A direct debugging diagram quickly intercepted by hand

  Directly execute executable programs , Because there is no breakpoint , So the program flashed by

        5. In this case , It's not a test of eyesight and hand speed , There should be a more scientific approach , After concluding , share               Three methods can be used :

① Introduce system commands into the header file #include<windows.h>, And end the statement in the main function return 0; Add a sentence before System pause command system(“pause”);, You can stop the execution program or nonparametric debugging and wait for the observation results :

② The most common method , No need to introduce additional header files , Put the above system Change the command to getchar The implicit pause of the program can be realized , You can insert a breakpoint to observe the execution of the statement :

It can be seen that the program is waiting for the library function getchar Wait for the command , Press down F10 Execute function , The cursor stays on the next line , Wait for the user to enter a character before terminating the program .

③ The most violent way , For the use of VS The user of the compiler can open the general... In the tool options , Automatically close the console and uncheck when debugging stops , But this method is only for debugging code , The official code run will still automatically close the console , For expediency .

Refer to the following articles for knowledge preheating at this stage , Coherent reading is easier to understand .

C Detailed explanation of language knowledge points —— First time to know C Language 【1】_VelvetShiki_Not_VS The blog of -CSDN Blog

版权声明
本文为[VelvetShiki_ Not_ VS]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231411217195.html