Basic concepts of C + + program development

Source: Internet
Author: User

The development of a C + + program requires the following steps: Edit, compile, connect, run debugging. More specific is: source code → preprocessor → compiler → target code → linker → executable program.

1, Source code: Source program is not compiled according to a certain programming language specification written text files. In C + +, the file. cpp file that stores the source code.

2. Pre-processor: C Preprocessor is a macro-preprocessor (preprocessor), the main function of which is the inclusion of the header file (including files), the macro substitution (macros expansion), the conditional compilation ( conditional compilation), row control (line contral).

(1) source file contains (including files)

The file contains instructions to insert the contents of a file into the # include section.

Common file-containing formats:

A, #include <filename>: Find the filename file in the standard compiler include directory;

B, #include "filename": Now the current source file directory to find, if not found, and then to the standard directory to find.

To avoid multiple inclusions (double inclusion), use #include命令常常被强制同 # include guard or #pragma once.

For example:

File Grandfather.h

struct Foo {    int  menber;};

File Father.h

" Grandfather.h "

File child.c

" Grandfather.h "  "father.h"

At this point, the Grandfather.h file is included two times, and because the struct foo is defined two times, it will cause a compilation error. Solution to this problem:

A. Using # include Guards

Modify the Grandfather.h to:

#ifndef Grandfather_h #define Grandfather_h    struct  foo {      int  member;    }; #endif /*grandfather_h*/

B, using #pragma once

Modify the Grandfather.h to:

#pragma oncestruct  foo {    int  member;};

(2) Conditional compilation

By # If, #ifdef, #ifndef开始, then 0-n a #elif, followed by 0-1 #else, and finally with #endif. When # If, #ifdef, #ifndef后面的条件表达式为真时, the code is compiled with the same control logic as the If-else statement.

For example:

#include <iostream>usingnamespace  std; #define A 2int  main () {    #ifdef a    "Yes" << Endl;     #else       "No" << Endl;      #endif }

The running result of the program is "yes", and when the macro definition of a is commented out, the result is "No".

(3) macro definition

There are two types of macro definitions: Object-like and Function-like, the simple macro definition that we often call and the macro definition with parameters.

A, simple macro definition

#define <identifier> <replacement token list>

For example:

#define PI 3.1415

B, macro definition with parameters

#define <identifier> (<parameter list>) <replacement token list>

For example:

#define Squre (R) (3.14*R*R)

There are also line controls and user-defined errors in other preprocessing.

Reference documents:

[1] http://en.wikipedia.org/wiki/C_preprocessor;

[2] http://www.th7.cn/Program/cp/201403/182079.shtml.

Basic concepts of C + + program development

Related Article

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.