Feel the world is agreed, the first program of each language is always Hello world! but also some books seem to be in pursuit of individuality, will use some other, but is not Hello world! I need to learn C + +, so from the most basic start. All Things Hello World:
1 //Helloworld.cpp2#include <iostream>3 using namespacestd;4 5 intMain ()6 {7cout <<"Hello world!"<<Endl;8 return 0;9}
This is probably the simplest program, just to print to the screen "Hello world! "This information. But it contains a lot of concepts:
- Comments
- preprocessing directives
- Main () function
- Input/output stream
1. Notes
The first behavior of the program is a line of comments for programmers to read, and the compiler will call. There are two kinds of annotation methods in C + +:
1 // Notes 2 /* Notes */
The main reasons for using annotations are:
(1) Description use
Explain the function of the program code, including the parameter meaning, return value and other information.
(2) Description of complex code
For a simple program in the console, code reading can be relatively easy, but in some areas of specialization, the code can be complex and esoteric, as in the following code:
1 voidSortintInarray[],intinsize)2 {3 for(inti =1; i < insize; i++)4 {5 intelement =Inarray[i];6 intj = i-1;7 while(J >=0&& Inarray[j] >Element)8 {9Inarray[j +1] =Inarray[j];Tenj--; One } AInarray[j +1] =element; - } -}
This is a sort algorithm that can explain some of the confusing code inside by adding annotations.
(3) Passing meta-information
Provides detailed information about creating code, but does not involve specific behavior of the code, such as adding author information, creating dates, and so on.
2. Pre-processing instructions
There are three steps to building a C + + program. First, the code runs in the preprocessor, and the preprocessor recognizes the meta-information in the code. The code is then compiled or converted to a computer-recognizable target file. Finally, the link becomes an application. The pre-processing command starts with # # <iostream> in the first program, meaning it extracts the contents of the <iostream> header file to the current file. If you do not include this header file, you cannot even complete the task of outputting the text.
Common pre-processing directives:
preprocessing directives |
Function |
#include [file] |
The specified file is inserted into the code where the instruction is located |
#define [Key][value] |
Each key being formulated is replaced with the specified value |
#ifdef [Key] #ifndef [Key] #endif |
The code in the Ifdef block or ifndef block is conditionally Include or discard, depending on whether a # define is used Defines the specified key |
#prama [XYZ] |
XYZ varies by compiler. If the preprocessing period is performed to this An instruction that usually displays a warning or an error message |
The following are the use of preprocessor directives to avoid repeating inclusions:
1 #ifndef Header_h_ 2 #define Header_h_3// ........ .... ...... 4 #endif
If the compiler supports #pragma once directives, you can rewrite the code above using the following code:
1 #pragma once2// ... ....... .........
3.main () function
Main () is the entrance to the program. The return value is an int type that indicates the final running state of the program. can have parameters, or there can be two parameters:
int Main (int argc, cha *argv[])
Where ARGC gives the number of arguments passed to the program, argv contains these parameters.
4. Input/output stream
The basic output stream is std::cout, in addition to the Std:cerr. << inserts data into the stream. Std::endl represents the end of the sequence, and when the output stream encounters Std::endl, all the content is output and transferred to the next line. and \ nyou can do it. \ n is an escape character, which is a line break. The following are the most common escape characters:
\ nthe line break
\ r Enter
\ t tab
\ \ backslash Character
\ "quotation marks
Flow can also accept user input, the simplest is to use >>. Std::cin input stream accepts user keyboard input.
Reference:
Marc Gregoire, Nicholas A. Solter, Scott J. Kleper, advanced Programming in C + +, Tsinghua University Press, 2012,isbn:978-7-302-29897-7
A review of C + + knowledge (I.)