C ++ knowledge Review (1), knowledge Review

Source: Internet
Author: User

C ++ knowledge Review (1), knowledge Review

I feel that the World is always agreed, and the first program in every language is always Hello World! However, some books seem to be pursuing individuality and will use others, but they are Not Hello World! I need to learn more about C ++, so from the very beginning. Hello World:

1 // helloworld.cpp2 #include <iostream>3 using namespace std;4 5 int main()6 {7     cout << "Hello World!" << endl;8     return 0;9 }

This may be the simplest program, simply printing "Hello World!" to the screen !" This information. However, it includes many concepts:

  • Note
  • Preprocessing commands
  • Main () function
  • Input/output stream

1. Notes

  The first line of comments in the program is for the programmer to read. The compiler will call the comment. C ++ has two annotation methods:

1 // comment 2/* Comment */

Main reasons for using Annotations:

(1) Description

Explain the functions in the program code, including parameter meanings, return values, and other information.

(2) describe complex code

For simple programs on the console, the code reading may be relatively easy, but in some professional fields, the Code may be complex and esoteric, as shown in the following code:

 1 void sort(int inArray[], int inSize) 2 { 3     for (int i = 1; i < inSize; i++) 4     { 5         int element = inArray[i]; 6         int j = i - 1; 7         while (j >= 0 && inArray[j] > element) 8         { 9             inArray[j + 1] = inArray[j];10             j--;11         }12         inArray[j + 1] = element;13     }          14 }    

This is a sorting algorithm. adding annotations can explain some confusing internal code.

(3) Transfer metadata

Provides detailed information about the code to be created, but does not involve specific code behaviors, such as adding author information and creation date.

2. Preprocessing commands

There are three steps to generate a C ++ program. First, the code runs in the pre-processor, and the pre-processor recognizes the meta information in the code. The code is then compiled or converted to a computer-identifiable target file. Finally, it becomes an application through the link. The Preprocessing command starts with #. The # include <iostream> header file in the first program is extracted to the current file. If this header file is not included, the task of outputting text cannot be completed.

Common preprocessing commands:

Preprocessing commands Function
# Include [file]

Insert the specified file to the instruction location in the code

# Define [key] [value] Each specified key is replaced with a specified value.

# Ifdef [key]

# Ifndef [key]

# Endif

Code in the ifdef block or ifndef block is conditional

Include or discard, depending on whether to use # define

The specified key is defined.

# Prama [xyz]

Xyz varies with the compiler. If

A command usually displays a warning or error message

To avoid repeated inclusion, run the pre-processor command:

1 #ifndef HEADER_H_2 #define HEADER_H_3 // .........................4 #endif

If the compiler supports the # pragma once command, you can use the following code to rewrite the above Code:

1 #pragma once2 // ..........................

3. main () function

Main () is the entry of the program. The return value is an int type, indicating the final running status of the program. There can be parameters or two parameters:

int main(int argc, cha *argv[])

Argc provides the number of parameters passed to the program. argv contains these parameters.

4. Input/output streams

The basic output stream is std: cout, and std: cerr. <Insert data into the stream. Std: endl indicates the end of the sequence. When the output stream encounters std: endl, all content is output and transferred to the next row. And \ n. \ N is an escape character and a line break. The following are the most common escape characters:

\ N line feed

\ R press ENTER

\ T Tab

\ Backslash

\ "Quotation marks

The stream can also accept user input, the simplest is to use>. Std: The cin input stream is input by the user's keyboard.

Refer:

Marc Gregoire, Nicolas A. Solter, Scott J. Kleper, C ++ advanced programming, Tsinghua University Press, 2012, ISBN: 978-7-302-29897-7

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.