#include <iostream.h> // preprocessing directive /* traditional first program author: S.Y. Lee, 9/17/96 */ int main() // main program { // body cout << "Hello world" << endl; // output statement return 0; // return statement }
// main program template #includeint main() { statements }
cout << ExprOrString << ExprOrString ...;
cin >> name;
v = exp;
v 為變數, 而 exp 為陳式(expression)。 陳式是由運算元(operand)和運算子(operator)組成。 運算元可以是變數或常數。 變數在使用前,必須先宣告其資料型式。
C++ 提供的基本資料型式 (Data types)有
字元值char unsigned char signed char
例如: char Letter = 'A'; 整數值
short int unsigned short int signed short int int unsigned int signed int long int unsigned long int signed long int
例如: int i = 3; 實數值
float double long double
例如: float Rate = 0.125;
int i; float x; i = 4.8; x = i + 4.8;
i = int(4.8); x = float(i + 9) + 5.7;
運算 | 運算子 | 例子 |
---|---|---|
加 | + | i + 9 |
減 | - | a - c |
乘 | * | a * b |
除 | / | x / y |
整數除 | % | r % s |
() * / % + -
if (g > 60) { cout << "Passed"; Passed="1;" }
#include <iostream.h> int main(void) { cout << "\nThis program converts a temperature\n" << "\tfrom Fahrenheit to Celsius.\n"; double FahrenheitTemp; cout << "\nPlease enter a Fahrenheit temperature: "; cin>> FahrenheitTemp; double CelsiusTemp = (FahrenheitTemp - 32.0) / 1.8; cout << "\n\t" << FahrenheitTemp << " in Fahrenheit is equivalent to " << CelsiusTemp << " in Celsius.\n\n"; return 0; }
#include <iostream> // preprocessing directive using namespace std; int main() // main program { // body cout << "Hello world" << endl; // output statement return 0; // return statement }
>edit xmp1.cc
>gcc -c xmp1.cc
>gxx xmp1.cc -o xmp1
>xmp1