The difference between declaring, defining, and initializing in the C language

Source: Internet
Author: User
Functions and variables (global variables) are declared and defined ,
For functions:
	In the same file, if the function fun_1 call fun_2, if fun_2 is defined after fun_1, declare fun_1 before Fun_2, as follows:	
void Fun_2 ()//Declaration
Void fun_1 () { 
...
Fun_2 ();
...
 } Call fun_2
void Fun_2 () {...}//Definition
In different files, when calling an external function, you need to first #include the corresponding header file (the header file contains the declaration of the function, you can import the source file directly, but not recommended), and then call the function.
For variables:
	Whether in the same file or in a different file, use must be declared in advance before the definition, and use the keyword extern when declaring:	
Conditions within the same file:
extern int B;
Main ()
{
    printf ("b =%d\n", b);
}
int b = 3;
In addition, its declarations are used to describe the properties of a variable (mainly the type of the variable) and can be declared multiple times, and the definition of a variable will cause the allocation of the storage and can only be defined once.
Note: definition is a special kind of declaration.
int sp;
Double Val[maxval];
These two statements define the variable SP and Val and allocate storage units for them, and the two statements can also be declared as the remainder of the source file.
extern int sp;
extern double val[];
These two statements are only declared and do not allocate storage space

Note: The length of the array must be specified in the definition, but the extern declaration does not necessarily specify the length of the array.
Variable Initialization : assign values at the same time as the definition of a variable, and then assign a value that is not initialized.

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.