天天看點

Code::blocks C++宏,const,指針

今天接觸了小型的編譯器,但是功能非常強大,名字叫Code::blocks

功能強悍,可以編譯好多程式,建構好多工程,如QT, GUI, ARM,C++/C等等...

1.軟體程式設計截圖:

Code::blocks C++宏,const,指針

2.宏函數,宏定義,const和指針

#include <iostream>
#include <cmath>

#define MIN(A,B) ((A) <= (B) ? (A) : (B))
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)

using namespace std;

int main()
{
    // Hello world
    cout << "Hello world!" << endl;
    //宏函數
    int minv = MIN(8,10);
    cout << "min value: " << minv << endl;
    //宏定義
    cout << "Seconds per year is : "<< SECONDS_PER_YEAR << endl;

    //const 與 指針
    int b = 500, b4 = 400;
    const int* a1 = &b; // case1
    int const* a2 = &b; // case2
    int* const a3 = &b; // case3
    const int* const a4 = &b4; // case4

    //case 1 and case 2 : const 在 * 左側 const 修飾變量 b, 即 b 為常量
    //*a1 = 250; //wrong! G:\codeblock\llvm_cpp\test\main.cpp|27|error: assignment of read-only location '* a1'
    //*a2 = 250; //G:\codeblock\llvm_cpp\test\main.cpp|28|error: assignment of read-only location '* a2'
    b = 250;
    int c = 251;
    a2 = &c;
    cout << "*a1 = " << *a1 << " , " << "*a2 = " << *a2 <<endl;

    //case 3 : const 在 * 右側 const 修飾指針 a3, 即 a3 為const 指針
    // 定義的時候必須初始化
    *a3 = 600;
    cout << "*a3 = " << *a3 << endl;

    //case 4 : 第1個const 修飾 b, 即 b 為常量; 第2個const 修飾 a4, 即a4 為const 指針
    cout << "*a4 = " << *a4 << endl;

    return 0;
}