// 算術運算子
int a = 21;
int b = 10;
int c;
c = a + b;
cout << "c = " << c << endl;
c = a - b;
cout << "c = " << c << endl;
c = a * b;
cout << "c = " << c << endl;
c = a / b;
cout << "c = " << c << endl;
c = a % b; // 取餘運算子
cout << "c = " << c << endl;
int d = 10;
c = d++; // 自加
cout << "c = " << c << endl;
d = 10;
c = d--; // 自減
cout << "c = " << c << endl;
// 關係運算子
int aa = 21, bb = 10;
int cc;
if (aa == bb)
cout << "aa == bb\n";
else
cout << "aa != bb\n";
if (aa > bb)
cout << "aa > bb\n";
else
cout << "aa < bb\n";
if (aa < bb)
cout << "aa < bb\n";
else
cout << "aa > bb\n";
if (aa != bb)
cout << "aa != bb\n";
else
cout << "aa == bb\n";
aa = 5;
bb = 20;
if ( aa <= bb )
cout << "aa小於或等於bb" << endl ;
if ( bb >= aa )
cout << "bb大於或等於aa" << endl ;
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
unsigned int a2 = 60;
unsigned int b2 = 13;
int c2 = 0;
c2 = a2 & b2;
cout << "a2 & b2: " << c2 << endl;
c2 = a2 | b2;
cout << "a2 | b2: " << c2 << endl;
c2 = a2 ^ b2;
cout << "a2 ^ b2: " << c2 << endl;
c2 = ~a2;
cout << "~a2: " << c2 << endl;
c2 = a2 << 2;
cout << "a2 << 2: " << c2 << endl;
c2 = a2 >> 2;
cout << "a2 >> 2: " << c2 << endl;
// 賦值運算子
int a3 = 21;
int c3;
c3 = a3;
cout << "c3 = " << c3 << endl;
c3 += a3;
cout << "c3 = " << c3 << endl;
c3 -= a3;
cout << "c3 = " << c3 << endl;
c3 *= a3;
cout << "c3 = " << c3 << endl;
c3 /= a3;
cout << "c3 = " << c3 << endl;
c3 = 200;
c3 %= a3;
cout << "c3 = " << c3 << endl;
c3 <<= 2;
cout << "c3 = " << c3 << endl;
c3 >>= 2;
cout << "c3 = " << c3 << endl;
c3 &= 2;
cout << "c3 = " << c3 << endl;
c3 ^= 2;
cout << "c3 = " << c3 << endl;
c |= 2;
cout << "c3 = " << c3 << endl;
語法格式:sizeof(資料型別/變數名);
cout << "size of char : " << sizeof(char) << endl;
cout << "size of int : " << sizeof(int) << endl;
cout << "size of short int : " << sizeof(short int) << endl;
cout << "size of long int : " << sizeof(long int) << endl;
cout << "size of float : " << sizeof(float) << endl;
cout << "size of double : " << sizeof(double) << endl;
cout << "size of wchar_t : " << sizeof(wchar_t) << endl;
int main ()
struct employee;
employee emp;
strcpy(emp.first_name, "zara"); 點運算子
// 如果 p_emp 是乙個指標,指向型別為 employee 的物件,則要把值 "zara" 賦給物件 emp 的 first_name 成員,需要編寫如下**:
strcpy(p_emp->first_name, "zara"); 箭頭運算子
7.5 c++ 指標運算子(& 和 *)
c++ 提供了兩種指標運算子,一種是取位址運算子 &,一種是間接定址運算子*。指標是乙個包含了另乙個變數位址的變數,可以把乙個包含了另乙個變數位址的變數說成是"指向"另乙個變數。變數可以是任意的資料型別,包括物件、結構或者指標。
取位址運算子 &
間接定址運算子*
int main ()
// 運算子優先順序
int a4 = 20;
int b4 = 10;
int c4 = 15;
int d4 = 5;
int e4;
e4 = (a4 + b4) * c4 / d4; // ( 30 * 15 ) / 5
cout << "(a4 + b4) * c4 / d4 的值是 " << e4 << endl ;
e4 = ((a4 + b4) * c4) / d4; // (30 * 15 ) / 5
cout << "((a4 + b4) * c4) / d4 的值是 " << e4 << endl ;
e4 = (a4 + b4) * (c4 / d4); // (30) * (15/5)
cout << "(a4 + b4) * (c4 / d4) 的值是 " << e4 << endl ;
e4= a4 + (b4 * c4) / d4; // 20 + (150/5)
cout << "a4 + (b4 * c4) / d4 的值是 " << e4 << endl ;
C 中 運算子 和 運算子
運算子定義在將可空型別分配給非可空型別時返回的預設值。int?c null 若 c 為 null,則 d 為 1,否則把 c 值賦予 d int d c 1 型別名稱後加問號,如 int?i null double?d null bool?b null 可指定可為 null 的 int double...
C 中的 運算子
是運算子中等級最高的,它分為三種 1 global scope 全域性作用域符 用法 name 2 class scope 類作用域符 用法 class name 3 namespace scope 命名空間作用域符 用法 namespace name 他們都是左關聯 left associativ...
C 中的 運算子
原文出處 一兩行以上的成員函式最好被定義在類體之外。這要求乙個特殊的宣告語化來標識一 個函式是乙個類的成員 成員函式名必須被它的類名限定修飾 qualified 也就是告訴編譯器,這個成員函式在我這類的類域內,直到該成員函式結束。by the way,類體就定義了乙個類域,類體外通過 擴充類域吧 比...