C++‎ > ‎

變數與關鍵字

變數 Variable

int mAge = 18; 指定 mAge 為 int 資料形態的變數,並給於 18 的值
  • 良好的命名
  • 盡量先給予初始值
  • 評估資料型態

變數的範圍

#include <iostream>
using namespace std;

//Global variables
int Integer;
char aCharacter;
char string [20];

int main ()
{
  //Local variables
  unsigned short Age;
  float ANumber, AnotherOne;

  cout << "enter your age:";
  cin >> Age;
  ...
}

全域變數 Global variables

宣告後就可以用在程式碼的任何地方

區域變數 Local variables

只能作用在所屬的 { } 區域內

關鍵字 Keyword

不能拿來當變數使用的字

alignas (since C++11) 
alignof (since C++11) 
and 
and_eq 
asm 
auto(1) 
bitand 
bitor 
bool 
break 
case 
catch 
char 
char16_t(since C++11) 
char32_t(since C++11) 
class 
compl 
const 
constexpr(since C++11) 
const_cast 
continue 
decltype(since C++11) 
default(1) 
delete(1) 
do 
double 
dynamic_cast 
else
enum 
explicit 
export 
extern 
false 
float 
for 
friend 
goto 
if 
inline 
int 
long 
mutable 
namespace 
new 
noexcept(since C++11) 
not 
not_eq 
nullptr (since C++11) 
operator 
or 
or_eq 
private 
protected 
public 
register 
reinterpret_cast
return 
short 
signed 
sizeof 
static 
static_assert(since C++11) 
static_cast 
struct 
switch 
template 
this 
thread_local(since C++11) 
throw 
true 
try 
typedef 
typeid 
typename 
union 
unsigned 
using(1) 
virtual 
void 
volatile 
wchar_t 
while 
xor 
xor_eq

Identifiers with special meaning

override(C++11)
final(C++11)


Comments