using namespace std; // 載入標準函式庫
#include <iostream>  // 載入與輸出輸入相關 
#include <cmath>     // 載入傳統的C語言函式庫標頭檔
int main ()
  double a;
  a = 1.2;
  a = sin (a);
  cout << a << endl;
  return 0;
   for (int i = 0; i < 4; i++)  // Local declaration of i 
      cout << i << endl;        // This outputs 0, 1, 2 and 3
   cout << i << endl;           // This outputs 487
   return 0;

[03:52 user@ws ch3]$ g++ -std=gnu++11 newVarInitial2.cpp
newVarInitial2.cpp: In function ‘int main()’:
newVarInitial2.cpp:8:15: warning: narrowing conversion of ‘5.5e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing]
newVarInitial2.cpp:9:13: warning: narrowing conversion of ‘6.5e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing]
newVarInitial2.cpp:10:14: warning: narrowing conversion of ‘332’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
newVarInitial2.cpp:10:14: warning: overflow in implicit constant conversion [-Woverflow]
[03:53 user@ws ch3]$ ./a.out 
[03:53 user@ws ch3]$ 
<note important> ++++ 型態也可以縮寫? | 我們在宣告整數型態的變數時,可以將int省略,例如:可以short來代表short int、以unsigned代表unsigned int、以long代表long int、long long代表long long int;以及unsigned short代表unsigned short int、unsigned long代表unsigned long int、unsigned long long代表unsigned long long int。 </note> 在整數型態的數值範圍方面,都是取決於其所使用的記憶體空間。由於不同平台上可能會有差異,C++語言僅提供規範,實際情形由各平台上的實作決定。因此,在一個平台上撰寫程式時,我們通常會使用以下的程式,先行瞭解各型態所佔的空間:

#include <iostream>
#include <climits> // use limits.h 
int main()
  using namespace std;
  int n_int = INT_MAX;       // initialize n_int to max int value
  short int n_short = SHRT_MAX;  // symbols defined in climits file
  long int n_long = LONG_MAX;
  long long int n_llong = LLONG_MAX;
  // sizeof operator yields size of type or of variable
  cout << "int is " << sizeof (int) << " bytes." << endl;
  cout << "short is " << sizeof n_short << " bytes." << endl;
  cout << "long is " << sizeof n_long << " bytes." << endl;
  cout << "long long is " << sizeof n_llong << " bytes." << endl;
  cout << endl;
  cout << "Maximum values:" << endl;
  cout << "int: " << n_int << endl;
  cout << "short: " << n_short << endl;
  cout << "long: " << n_long << endl;
  cout << "long long: " << n_llong << endl << endl;
  cout << "Minimum int value = " << INT_MIN << endl;
  cout << "Bits per byte = " << CHAR_BIT << endl;
  return 0;
  • float: 單精確度浮點數(single-precision floating-point)
  • double: 倍精確度浮點數(double-precision floating-point)
  • long double: 擴充精確度符點數(extended-precision floating-point)
    在不同的系統中,字元的數值可能會代表不同意義,視其所採用的字元集(character set)而定。現行最常見的字元集為ASCII(American Standard Code for Information Interchange),請參考Wikipedia關於ASCII的說明。 既然char型態就是整數,那可不可以再配合unsigned使用呢?因為char型態的整數數值是用以對應特定的字元集(如ASCII),而每個字元集都有其可表達的字元個數要求,C++語言會自動將char定義為singed或unsigned以符合字元集的需求。因此我們通常不會特別在char前加上unsigned。但是,如果您有某些較小的整數資料要處理,就可以考慮使用char來代替int。因為int為32 bits,甚至short int也要使用到16 bits,若您只需要處理一些介於-128到127之間的數值,那您就可以考慮改用char來代替int;或是宣告為unsigned char來處理那些介於0到255的正整數資料。 等號「=」被稱為C/C++語言的指定運算子(assignment operator),用以將等號右方的值指定(assign)給等號左方,我們將其稱為是右關聯(right associativity)的運算子。例如:
  • i = 5;
  • j = i;
  • k = 10 * i + j;
    其結果是先進行等號右邊的運算,得到結果為5後,將數值5給定到等號左邊的變數i,因此,最後i的值等於5。針對這種情形,C/C++語言提供複合指定(compound assignment)運算子,例如 「+=」,上面的運算式可重寫為:
  • i += 2;
    char *month[]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; cout << "Size of char type = " << sizeof(char) << " byte." << endl; cout << "Size of month array = " << sizeof month << " bytes." << endl; return 0; cout << test (14, 5) << endl; // Displays 14 - 5 cout << test (14) << endl; // Displays 14 - 7 return 0; if (a == 0) throw zero; if ((a / 2) * 2 == a) throw pair; for (int i = 3; i <= sqrt (a); i++) if ((a / i) * i == a) throw notprime; throw prime; catch (char *conclusion) cout << "The number you typed is "<< conclusion << endl; cout << endl; return 0;
  •