标签: variables declaration definition extern
为什么我们可以在定义用其他类型声明的变量时更改数据类型?
#include <iostream> using namespace std; extern int ab; main() { float ab=10.2; cout<<ab; return 0; }
如果可以,那么宣言的用途是什么?
Though (almost) everyone knows the meaning of declaration and definition of a variable/function yet for the sake of completeness of this post, I would like to clarify them. Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function.