The difference between definition and declaration

Source: Internet
Author: User

Testimonials: I am a little junior, and computer is a bit interested, but I have been on the definition of variables and statements are very vague, the first reading is to say that: the definition of the shape of an int A; this is defining an integer variable, and then saying that the variable can be defined only once and declared but multiple times, Then did not, I also asked several teachers, they are also said, I search the answer on the Internet also feel not very thin, so I always feel that this is not the answer I want, until I see the <<c traps and defects >> only then understand the original inside still have a lot of places worth digging, So share to everyone, my example is VC6.0 under the experiment, first look at the first example: 1:
void Main () {extern int a;//definition and declaration cannot be placed in the same function int a=10;   Redefine a=10 error Cout<
2:
extern int a;//This is the Declaration void Main () {int a=10;   (This is the definition) and this definition is the right cout<
3:
void Main () {extern int a;//declaration cout<
4:
extern int a=10;//Global declaration takes up Memory space void Main () {Cout<<a<<endl;cout<
5:
void Main () {extern int a=10;//declaration cannot be initialized in the function body cout<<a<<endl;//This is an incorrect expression}
6:
the extern int a;//Description Declaration can be declared more than once, whereas the definition can only be defined once extern int A;extern int a;void main () {int a=10;cout<<a<<endl;}
Summary: To cite how many examples, if you have been the same as me on the statement and the definition is a little vague, I believe you should have some understanding of it, but I actually have no idea why the global declaration can be assigned and have memory space, it is possible that the compiler gave us hands and feet, But that's what the textbook says: the definition of a variable can only be defined once, and the declaration may be multiple times, and it's important to remember that!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between definition and declaration

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.