預處理指令
文件包含
條件編譯
#include<iostream>
using namespace std;
int main()
{
extern int x; //外部變量
cout << "外部變量x=" << x << endl;
return 0;
}
int x = 8;
#include<iostream>
#include "printdiamond.h"
using namespace std;
int main()
{
int row;
cout << "請輸入菱形的行數(奇數):";
cin >> row;
if (row % 2 != 0)
printdiamond(row);
else
cout << "應該輸入一個奇數行數,不能是偶數" << endl;
return 0;
}
#include <iostream>
using namespace std;
void printdiamond(int n)
{
int i, j, k;
for (i = 1; i <= (n + 1) / 2; i++) //上半部分
{
for (j = 1; j <= (n + 1) / 2 - i; j++)
cout << " ";
for (k = 1; k <= 2 * i - 1; k++)
cout << "*";
cout << endl;
}
for (i = 1; i <= (n - 1) / 2; i++) //下半部分
{
for (j = 1; j <= i; j++)
cout << " ";
for (k = n - 2 * i; k >= 1; k--)
cout << "*";
cout << endl;
}
}
#include<iostream>
using namespace std;
#define HELLO //定義標識符
int main()
{
int x = 8, y = 10;
//條件編譯
#ifdef HELLO
cout << x << "+" << y << "=" << x + y << endl;
#else
cout << x << "*" << y << "=" << x * y << endl;
#endif
//條件編譯
#if x>y
cout << x << ">" << y << endl;
#else
cout << x << "<" << y << endl;
#endif
return 0;
}