当前位置:网站首页>Detailed explanation of C language knowledge points -- first knowledge of C language [1]

Detailed explanation of C language knowledge points -- first knowledge of C language [1]

2022-04-23 14:28:00 VelvetShiki_ Not_ VS

C language ⾔

C Language is a process oriented programming language , Applied to the underlying development , Unlike object-oriented programming languages ( Such as C++,JAVA), Many high-level languages or operating systems, such as python,C++,Linux It's all based on C Based on .C Language can be compiled in a simple way 、 Processing low-level memory , It only produces a small amount of machine language and efficient programming language that can run without any running environment support .

C standard

from ANSI C By the American National Standards Institute (ANSI) And the international organization for Standardization (ISO) About C The standard of language . current C11 The standard is C The third official standard of language , The standard better supports Chinese character function names and Chinese character identifiers , To a certain extent, Chinese character programming is realized . Now commonly used by the public C The standard is C99.

Main compiler

MSVC, GCC, Sublime, Turbo C, Clang etc. .

VS2022: Slightly different from the compiler , It is an integrated editor , compiler (MSVC) An easy-to-use integrated development environment integrated with debugger .

General steps for editing code and running

  1. Preparation before compiling :

① Create a new project —— Generate empty project (C++), The project name shall be in English , There should be no spaces or special characters between characters , Store to a specific disk location .

② Create the source file ——C yes C++ A subset of languages ,C++ The powerful functions contained in C File formats are also compatible , So choose C++ File creation format .

     2. The basic composition of the code

① Introduction of header file : The header file contains a lot of C Library functions , These library functions are C The language compiler itself exists C The language library function is used by the underlying developers in the development , Higher frequency functions are encapsulated , Put it in a header file in advance , For all programmers . When using, the format is usually used at the beginning of the source file #include< file name .h> Import header file , Common header files and their functions :

⑴#include<stdio.h>: The standard I / O header file defines three variable types ( Unsigned integer size_t, file type FILE, Any location or type where files are stored fpos_t), Some macros ( Null value NULL, End of file flag EOF etc. ) And various functions to perform input and output ( If the file is opened fopen, Format output fprintf, Stream buffer definition setbuff etc. ).

⑵#include<stdlib.h>: The utility header file defines some common macros and tool functions ( Such as dynamic development of memory malloc, Free memory free, Random number generation rand etc. )

⑶#include<string.h>: Defines how to manipulate the header file of the character array .

⑷#include<math.h>: Header files related to mathematical operations and operations .

⑸#include<windows.h>: Header files related to operating system instructions .

wait

② The main function —— Declaration of main function int main(void), yes C Language agreed program execution entry , Parentheses are void Omission . The standard format is int main(int argc, char* argv[]), The former is the number of parameters passed , The latter is the parameter value , But it's temporarily empty void that will do .int Is an integer value , Indicates that the return value of the main function of the code is an integer variable int, The general main code is the main carrier for the integration and function realization of user-defined functions , So the return value at the end is usually return 0, That is, the main function does not need the user to return any value . It should be noted that ,main There can be only one function in a program code . It's best to add... At the end of each main function code getchar();, In this way, the program will not flash after each code run , Especially when running .exe Document and release When you release a version, you need to pay more attention to , debugging debug Version will automatically reserve the console observation effect for the test function in the debugging stage, which can be ignored getchar() Use .

  notes :debug To debug a 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 , Compiled into this version of .exe Program files can be shared with others , And the actual size ratio debug Lower version .

③ User defined functions —— Personalized demand function defined according to actual function , Placed in the main function can realize various functions , But the return value of each function can only have one , The format is : return type + Function name + parameter list .

     3. edit + compile + link + function

① edit —— Editing code is to describe the specific implementation of function functions , By defining variables line by line , The input character , Insert the function , By file, etc C The text entered by the grammar rules is finally edited to .c For the suffix C Source code file .

② compile : Compilation is edited by the compiler .c The source code file of the suffix is translated into an assembly language instruction expressed in .obj Target file for suffix , Usually a source code file corresponds to a target file ( An object file contains multiple assembly code files, so compilation can be divided into precompile , Compile and assemble ), In this process, the syntax of the source program is automatically checked and the error type and location are marked for users to modify .

③ link : The target file formed in the compilation stage .obj Link and summarize the assembly code files in , Finally, it is generated with .exe Executable program file with suffix .

example : Programming Hello World

The basic situation of program editing and compilation is clarified , You can start the simple writing of personal programs .

  1. Create new projects and new project files , Project selection C++ Empty item and need to make your own name ( No spaces and special characters , It's best to underline between English words _ Separate ) And choose the path .
  2. Create a new source file and select C++ type , Enter the code editing link , There are several aspects to pay attention to at this stage :

 ① If you use 2013 Version above Microsoft VS compiler , It's best to install the root directory of the compiler by default vs\Common7\IDE\VC\VCProjectitems Find below newc++file, This file is the format file of the new source file , That is, the default source code template generated every time a new project is created . Use notepad++ Or notepad ( You need to copy it to the desktop and then copy it back to overwrite it, so as to avoid insufficient permissions ) Open and add a sentence #define _CRT_SECURE_NO_WARNINGS 1, Then save . The purpose of this statement is to add preprocessing definitions to the compiler , For such as scanf and strcpy or getchar,gets And other older versions ( Input type )C When using library functions or macros, it may cause problems such as buffer overflow and memory out of bounds , Because such functions do not check boundaries when reading data , It's an unsafe function , Therefore, adding the predefined instruction is compatible with this kind of function .

 ② Edit code —— After importing standard I / O header file , Define the main function main, Insert in C Standard library functions printf, This function is a formatted output function , Its function declaration is built into the imported header file stdio.h in , The format is printf("< Formatted string >", < parameter list >), The string selected in double quotation marks and the formatted parameters assigned by the parameter list can be output to stdout, That is, print to the screen . Type in “Hello World” It should be noted that double quotation marks should be English characters , Chinese characters are not received and will report errors . Notice that the quotation mark also contains the escape character “\n”, Escape character in C The definition of language can show ASCII Characters that cannot be displayed in the code ,“\n” It's one of them , Its function is to move the cursor pointer to the first grid of the next line after outputting the string of characters , Enter immediately .

 

Code and result display

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