The difference between the definition and declaration of a variable is detailed

Source: Internet
Author: User
speech: I am a junior, is a little interested in computer, but I've always been vague about the definition and statement of variables, and the first thing I read was this: define--> as int A; this defines a shape variable, and then it says that the definition of a variable can only be one time and the declaration is multiple times. , and then no, I also asked several teachers, they also say so, I search on the Internet to find the answer is not very thin, so I always feel that this is not the answer I want, until I saw the <<c traps and defects >> before I understand that there are still many worthy of deep digging in the place, So to share with everyone, my so examples are all under VC6.0 experiment, first look at the first example: 1:
void Main ()
{
	extern int a;//definitions and declarations cannot be placed in the same function
	int a=10;   Re-define 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
	cout<
3:
void Main ()
{
	extern int a;//Declaration
	cout<
4:
the extern int a=10;//Global declaration takes up memory space
void Main ()
{

	cout<<a<<endl;
	cout<
5:
void Main ()
{
	extern int a=10;//declared in the function body cannot be initialized
	cout<<a<<endl;//This is an incorrect expression
}
6:
an extern int a;//description Declaration can be declared multiple times, whereas a definition can only define an
extern int A;
extern int A;
void Main ()
{
	int a=10;
	cout<<a<<endl;
}	
Summary: Give me how many examples, if you've been a little vague about statements and definitions before, I'm sure you should know something about it, but I still have a bit of a reason why the global declaration can be assigned and have memory space, possibly the compiler has done something to us, But that's what the textbook says: the definition of a variable can only be defined once, and it's important to remember that the declaration may be multiple times.

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.