【正文】
C with Classes” language ? C language with objectoriented programming ? 1983, the “C with Classes” ? the C++ language ? 1998, 1st standard version: C++98 ? 2022, 2nd standard version: C++03 ? 2022, 3rd standard version: C++11 27 Programming and Coding ? 一段簡單的 C 程式 28 include include int main() { int x = 0。x)。 printf (%d\n, x)。 return 0。 // 變數(shù)宣告式 scanf(%d, amp。 // scanf: 將資料從至 標準輸入裝置 (standard input device)讀取出來的函數(shù) x = 1 + 2 * 3 / 4。 // printf: 將資料輸出至 標準輸出裝置 (standard output device)的函數(shù) system(pause)。 /* 告訴作業(yè)系統(tǒng)此程式將正常結(jié)束 */ } Programming and Coding ? include directive(指令 ) ? 將所指定的檔案內(nèi)容複製貼上 ? include SourceFile ? The path of SourceFile is ? Default_Include_Path/SourceFile ? The setting of Default_Include_Path ? Tools / Compiler Options / Directories / C includes ? include “SourceFile“ ? Compiler searches the SourceFile if it is an absolute path ? Compiler searches the SourceFile in the current directory if SourceFile is just a filename 30 Programming and Coding ? Comments ? Single line ment // ? c89 block ment /* */ ? c89 block ment 常如此誤用 : 31 include include int main(){ /* int x。 printf (%d\n, x)。 */ system(pause)。 } Programming and Coding ? 更好用的 block ment 32 include include int main() { if 0 int x。 printf (%d\n, x)。 endif system(pause)。 } Programming and Coding ? The main function ? 所 C 程式的主要起始點 ? main function 是唯一的 . 千萬不可寫兩個 main function在同一支程式 (即使他們的 return type 及 and the arguments list 不相同 ) ? Simple type of the main function: ? Complete types of the main function: 33 int main( ) { … } int main( void ) { // void means empty … } int main( int argc, char *argv[ ] ) { … } // For UNIX Programming and Coding ? 變數(shù) Variables ? 在記憶體佔有一席之地 ,用來存放資料以便進行各種運算。 ? 變數(shù)的宣告 ? Ex: ? 千萬不可以有兩個變數(shù)取相同名稱 34 int x。 // Declare a 32bit floatingpoint number double z。 // Declare an unsigned integer char c。 // Declare a 100 characters text string int x。 // Error! Redeclaration of x! float x。 // Declare three integers float x, y, z, w。 // a = 0 and b = 1 but c = ? float x = , y, z, w = 。 // All characters are zero Programming and Coding ? Statements and Expressions ? 運算式 Expressions ? 一個運算式是由 變數(shù) (variables), 常數(shù) (constants), 運算子 (operators)及 函數(shù)呼叫 (function calls) 所組成 ? 任何運算式皆會算出一個值 (value). ? 敘述 Statements ? 一句敘述是由符號 (tokens), 運算式 (expressions), 及其他敘述句所組成 . ? 以分號 (。 // A declaration statement x = 1 + 2 * 3 / 4。 /*An assignment statement printf(Hello\n)。 x = 1 + 2 * 3 / 4。 1+2*3/4: Rvalue y = x。 x: Rvalue 1+2*3/4 = y。 // error! x+1 cannot be a Lvalue int *p。 // error! p 與 x型態(tài)不匹配 C/C++ Operators 1 38 Precedence Operator Description Associativity 1(highest) :: Scope resolution (C++ only) Lefttoright 2 ++ Suffix increment Suffix decrement () Function call [] Array subscripting . Element selection by reference Element selection through pointer typeid() Runtime type information (C++ only) const_cast Type cast (C++ only) dynamic_cast Type cast (C++ only) reinterpret_cast Type cast (C++ only) static_cast Type cast (C++ only) 3 ++ Prefix increment Righttoleft Prefix decrement + Unary plus Unary minus ! Logical NOT ~ Bitwise NOT (type) Type cast * Indirection (dereference) amp。 Bitwise AND 11 ^ Bitwise XOR (exclusive or) 12 | Bit