[C ++ Note] 2. Variable declaration and definition, and variable Declaration

Source: Internet
Author: User

[C ++ Note] 2. Variable declaration and definition, and variable Declaration

It is impossible to put all C ++ programs in one file. Therefore, C ++ supports the separate compilation mechanism, which allows programs to be divided into several files, each file can be compiled independently. This shows that when your program has many files, you do not need to recompile all the files after modifying one of the files. You only need to compile and modify the files, then link them all.

How does C ++ support separate compilation? Separate the Declaration and definition.

Declaration (declaration) makes the name known to the program. If a file wants to use a name defined elsewhere, it must contain the declaration of the name.

Definition is responsible for creating entities associated with names.

A declaration statement is composed of a basic data type and a declarator list followed by it.

You also need to apply for a bucket or assign an initial value to the variable.

If you want to declare a variable rather than define it, assign an initial value to the variable marked with the keyword extern before the variable name, but this will offset the effect of extern. If an extern statement contains an initial value, it is no longer a declaration but a definition.

Extern int I; // declare I instead of defining iint j; // declare and define j

In the function body, if you try to initialize a variable marked by the extern keyword, an error is thrown.

// Error int main () {extern int I; int I = 10; return 0 ;}

Variables can be defined only once, but can be declared multiple times.

// You can obtain extern int I; int main () {int I = 10; return 0 ;}

To use the same variable in multiple files, you must separate the Declaration and definition. However, the definition of a variable must appear only in one file. Other files that use the variable must be declared, but cannot be defined repeatedly.

In addition, C ++ checks the type at the compilation stage. Therefore, C ++ is also known as statically typed. The more complex the program is, the more the static type persistence helps to identify problems. Then, the premise is that the compiler must know the type of each object, which requires that we must declare its type before using a variable.

Thank you for your visit and hope to help you. Welcome to your attention, favorites, and comments.

My more blog posts: NoMasp blog Introduction

For this article to get an axe and a question, please indicate the source:
Http://blog.csdn.net/nomasp

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.