如何在 C++ 中声明变量?
在 C++ 中,经常将声明和定义混淆不清。声明(在 C 中)意味着,你在告诉编译器任何变量或用户自定义类型或程序中函数的类型、大小和(在函数声明的情况下)其参数的类型和大小。在声明的情况下,并未在内存中为任何变量预留空间。
另一方面,定义意味着除了声明中的所有内容之外,还会在内存中进一步预留空间。你可以说“定义 = 声明 + 空间预留”。
以下是声明示例 −
extern int a; // Declaring a variable a without defining it
struct _tagExample { int a; int b; }; // Declaring a struct
int myFunc (int a, int b); // Declaring a function以下是定义示例 −
int a;
int b = 0;
int myFunc (int a, int b) { return a + b; }
struct _tagExample example;
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP