当前位置:网站首页>[network] Introduction to C language

[network] Introduction to C language

2022-07-07 22:58:00 For the rest of Kali's life

Hello everyone , We all know C Language is the basic knowledge of entry
C Language is in 70 It came out in the early 's . At & T in 1978 (AT&T) Bell Labs has officially published C Language . At the same time by B.W.Kernighan and D.M.Ritchit Co authored the famous “THE C PROGRAMMING LANGUAGE” A Book . Usually abbreviated as 《K&R》, Some people call it 《K&R》 standard . however , stay 《K&R》 There is no complete standard defined in C Language , Later, the American National Standards Institute developed a C Language standards , Published in 1983 . Usually called ANSI C.
   The best programming language of our time    In the early C Language is mainly used for UNIX System . because C The powerful functions and advantages of language are gradually recognized by people , By the eighties ,C Start entering other operating systems , And soon in all kinds of big 、 in 、 Small and microcomputers are widely used . Become one of the best programming languages of the time .  C Characteristics of language   C Language is a structured language . It has a clear hierarchy , Easy to organize programs in a modular way , Easy to debug and maintain .C The expressive and processing ability of language is very strong . It's not only rich in operators and data types , It is easy to realize all kinds of complex data structures . It can also directly access the physical address of memory , Progressive bit (bit) First level operation . because C Language realizes the programming operation of hardware , therefore C Language integrates the functions of high-level language and low-level language . It can be used for the development of system software , It is also suitable for the development of application software . Besides ,C Language is also efficient , It has strong portability . Therefore, it has been widely transplanted to various types of computers , So many versions of C Language .  C Language version    At present, the most popular C There are several languages :   •Microsoft C Or called MS C   •Borland Turbo C Or called Turbo C   •AT&T C   these C The language version not only implements ANSI C standard , And on this basis, they have made some extensions , Make it more convenient 、 perfect .   Object oriented programming language    stay C On the basis of , In 1983, by Bell Laboratories Bjarne Strou-strup Launched C++. C++ It was further expanded and improved C Language , Become a face Object programming language .C++ The latest popular version is Borland C++4.5,Symantec C++6.1, and Microsoft VisualC++ 2.0.C++ Put forward some more in-depth concepts , These object-oriented concepts it supports are easy to map the problem space directly to the program space , It provides programmers with a different way of thinking and programming methods from the traditional structural programming . It also increases the complexity of the whole language , It's difficult to master .  C and C++   however ,C yes C++ The basis of ,C++ Language and C Languages are compatible in many ways . therefore , Mastered C Language , Further study C++ You can learn an object-oriented language with a familiar syntax , So as to achieve twice the result with half the effort .  C The structural characteristics of the source program    To illustrate C Characteristics of language source program structure , First look at the following procedures . These are several journeys From simple to difficult , It shows C The characteristics of language source program in composition and structure . Although the relevant contents have not been introduced yet , But you can see from these examples that a C The basic part and writing format of the source program .main(){printf("c The language world www.vcok.com, Hello! !\n");}  main Is the function name of the main function , It means that this is a main function . every last C All source programs must have , And there can only be one main function (main function ). Function call statements ,printf The function sends the output to the display .printf Function is a standard function defined by the system , Can be called directly in the program .#include#includemain(){double x,s;printf("input number:\n");scanf("%lf",&x);s=sin(x);printf("sine of %lf is %lf\n",x,s);}   Comments per line   include Called file containing command extension .h The file of is also called header file or header file    Define two real variables , To be used by later programs    Display prompt message    Get a real number from the keyboard x   seek x Sine of , And assign it to the variable s   Display the result of program operation   main End of the function      The function of the program is to input a number from the keyboard x, seek x The sine of , Then output the result . stay main() The first two lines are called preprocessing commands ( See below ). There are several other preprocessing commands , there include It's called a file containing command , The meaning is to put angle brackets "" Or quotation marks <> The files specified in are included in this program , Become part of this program . Included files are usually provided by the system , Its extension is .h. Therefore, it is also called header file or header file .C The header file of the language includes the function prototypes of each standard library function . therefore , When calling a library function in a program , Must contain the header file where the function prototype is located . In this case , Three library functions are used : Input function scanf, Sine function sin, Output function printf.sin Functions are mathematical functions , Its header file is math.h file , Therefore, before the main function of the program, use include The command contains math.h.scanf and printf The standard input and output functions are , Its header file is stdio.h, Also use before the main function include The command contains stdio.h file .   It should be noted that ,C The language regulation is right scanf and printf These two functions can eliminate the inclusion command of its header file . So in this example, you can also delete the containing command in the second line #include. Again , In case 1.1 Used in printf function , The include command is also omitted .   The main function body in the example is divided into two parts , One part is the explanation , Another part is the executive part . Description refers to the type description of the variable . No variables are used in the example , Therefore, there is no explanation .C Language policy , All variables used in the source program must be explained first , After use , Otherwise, there will be mistakes . This is a feature of compiled high-level programming language , And interpretive BASIC Language is different . The explanation is C A very important part of the source program structure . In this example, two variables are used x,s, Used to represent the input arguments and sin Function value . because sin The function requires that these two quantities must be double precision floating-point , So type specifier double To illustrate these two variables . The four lines after the description part are the execution part or the execution statement part , To complete the function of the program . The first line of the execution part is the output statement , call printf Function to output Prompt string on the display , Please input the argument x Value . The second line is input statement , call scanf function , Accept the number entered on the keyboard and store it in the variable x in . The third line is the call sin Function and send the function value to the variable s in . The fourth line is with printf Function output variable s Value , namely x The sine of . Program end .printf("input number:\n");scanf("%lf",'C10F10&x);s=sin(x);printf("sine of %lf is %lf\n",'C10F10x,s);   When running this program , First, give the prompt string on the display screen input number, This is done by the first line of the executive part . The user types a number from the keyboard at the prompt , Such as 5, Press enter , Then give the calculation results on the screen .   Input and output functions    Input and output functions are used in the first two examples scanf and printf, In Chapter 3, we will introduce in detail . Here we first briefly introduce their formats , For the following use .scanf and printf These two functions are called format input function and format output function respectively . Its meaning is to input and output values in the specified format . therefore , The parameter list of these two functions in brackets consists of the following two parts : “ Format control string ”, Parameter table   The format control string is a string , Must be enclosed in double quotation marks , It represents the data type of input and output quantity . See Chapter 3 for various types of format representations . stay printf Function can also appear non format control characters in the format control string , At this time, the original text will be copied on the display screen . The input or output quantities are given in the parameter table . When there are multiple quantities , Space with commas . for example :printf("sine of %lf is %lf\n",x,s);   among %lf Format character , Indicates that it is processed as a double precision floating-point number . It appears twice in the format string , Corresponding x and s Two variables . The rest of the characters are non formatted characters, which are output on the screen as is int max(int a,int b);main(){int x,y,z;printf("input two numbers:\n");scanf("%d%d",&x,&y);z=max(x,y);printf("maxmum=%d",z);}int max(int a,int b){if(a>b)return a;else return b;}   The function of this function is to input two integers , Output the large number ./ Function description // The main function // Variable description // Input x,y value // call max function // Output // Definition max function // Return the result to the calling function /   The function of the program in the above example is to input two integers by the user , After the program is executed, the larger number is output . This program consists of two functions , Main functions and max function . There is a parallel relationship between functions . Other functions can be called from the main function .max The function compares two numbers , Then return the larger number to the main function .max Function is a user-defined function . Therefore, the description should be given in the main function ( The third line of the program ). so , In the description part of the program , Not only can there be variable descriptions , There can also be function descriptions . The details of functions will be introduced in Chapter 5 . Use / and / The enclosed contents are the notes , The program does not execute the comment part .   The execution process of the program in the above example is , First, display the prompt string on the screen , Please enter two numbers , Enter by scanf The function statement receives these two numbers into the variable x,y in , And then call max function , And put x,y The value of is transmitted to max The parameters of the function a,b. stay max Compare a,b Size , Return the larger one to the variable of the main function z, Finally, output on the screen z Value .  C The structural characteristics of the source program   1. One C Language source program can be composed of one or more source files .  2. Each source file can consist of one or more functions .  3. No matter how many files a source program consists of , There is one and only one main function , The main function .  4. There can be preprocessing commands in the source program (include Commands are just one of them ), Preprocessing commands should usually be placed at the top of the source file or program .  5. Every description , Every statement must end with a semicolon . But preprocessing commands , Function headers and curly braces “}” You can't add a semicolon after that .  6. identifier , At least one space must be added between the keywords to indicate the interval . If there is an obvious separator , Also can not add space to space .   Rules to follow when writing procedures    From clear writing , Easy to read , understand , From the perspective of maintenance , When writing a program The following rules should be followed :  1. A description or statement takes up a line .  2. use {} The enclosed part , Usually represents a hierarchy of programs .{} It is usually aligned with the first letter of the structure statement , And take a single line .  3. A lower level statement or description can be indented by several boxes than a higher level statement or description . To make it look clearer , Increase the readability of the program . Try to follow these rules when programming , To develop a good programming style .  C Character set of language    Characters are the most basic elements of a language .C The language character set consists of letters , Numbers , Space , Punctuation and special characters . In character constants , Chinese characters or other representable graphical symbols can also be used in string constants and comments .  1. Letter    Lowercase letters a~z common 26 individual , Capital A~Z common 26 individual   2. Numbers   0~9 common 10 individual   3. Blank character   Space character 、 tabs 、 Line breaks and so on are collectively referred to as blank characters . Whitespace only works in character constants and string constants . When it appears elsewhere , Only for spacing , The compiler ignores them . Therefore, whether to use white space in the program , It does not affect the compilation of the program , But using blank characters in appropriate places in the program will increase the clarity and readability of the program .  4. Punctuation and special characters   C Language vocabulary    stay C Words used in language fall into six categories : identifier , keyword , Operator , Separator , Constant , Comments, etc .  1. identifier    The name of the variable used in the program 、 Function name 、 Labels and so on are collectively referred to as identifiers . Except that the function name of the library function is defined by the system , The rest is user-defined .C Regulations , Identifiers can only be letters (A~Z,a~z)、 Numbers (0~9)、 Underline () Composed string , And the first character must be a letter or an underline .   The following identifier is legal :a,x, 3x,BOOK 1,sum5   The following identifier is illegal :     3s Start with a number    sT Illegal character    -3x Start with a minus sign    bowy-1 Illegal character -( minus sign )   When using identifiers, you must also pay attention to the following points :  (1) standard C Do not limit the length of the identifier , But it is affected by various versions C Language compilation system limitations , At the same time, it is also limited by specific machines . For example, in a certain version C The first eight digits of the identifier specified in are valid , When the first eight digits of two identifiers are the same , Is considered to be the same identifier .  (2) In the identifier , Case is different . for example BOOK and book Are two different identifiers .  (3) Although identifiers can be arbitrarily defined by programmers , But an identifier is a symbol used to identify a quantity . therefore , The naming should have corresponding meaning as far as possible , In order to read and understand , Make it “ seeing the name of a thing one thinks of its function ”.  2. keyword    Keywords are created by C A string with a specific meaning specified by a language , It is also called reserved word . The user-defined identifier should not be the same as the keyword .C The keywords of a language fall into the following categories :  (1) Type specifier    Used for definition 、 Explanatory variable 、 The type of function or other data structure . As used in the previous example int,double etc.   (2) Statement definer    Used to represent the function of a statement . For example 1.3 Used in if else Is the statement definer of the conditional statement .  (3) Preprocessing command words    Used to represent a preprocessing command . As used in the previous examples include.  3. Operator   C There are many operators in the language . Operators and variables , Functions together form expressions , Represents various operational functions . Operators consist of one or more characters .  4. Separator    stay C There are two kinds of separators used in language, comma and space . Commas are mainly used in type descriptions and function parameter tables , Separate variables . Spaces are often used between words in a sentence , Make a space . In keywords , There must be more than one space between identifiers , Otherwise, there will be syntax errors , For example, put int a; It's written in inta;C The compiler will put inta Treat it as an identifier , The result is bound to go wrong .  5. Constant   C Constants used in languages can be divided into numerical constants 、 character constants 、 String constant 、 Symbolic constant 、 Escape characters and so on . In the second chapter, we will give a special introduction .  6. annotator   C The annotator of the language is “/” Start with “/” The ending string . stay “/” and “/” Between is the comment . Program compile time , Do nothing with comments . Comments can appear anywhere in the program . Annotations are used to prompt or explain the meaning of a program to the user . In the debugging program, statements that are not used for the time being can also be enclosed with comments , Make the translation skip without processing , Remove the comment after debugging .

原网站

版权声明
本文为[For the rest of Kali's life]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130601307426.html