溫馨提示×

C語言定義常量define的應(yīng)用實(shí)例

小樊
86
2024-06-20 01:33:23
欄目: 編程語言

#include <stdio.h>

#define PI 3.14159
#define MAX(x,y) ((x) > (y) ? (x) : (y)

int main() {
    const double radius = 5.0;
    const int a = 10;
    const int b = 20;

    double circumference = 2 * PI * radius;
    int max_num = MAX(a, b);

    printf("The circumference of the circle with radius %.1f is %.2f\n", radius, circumference);
    printf("The max number between %d and %d is %d\n", a, b, max_num);

    return 0;
}

在上面的示例中,我們使用了#define來定義常量PIMAX,分別表示圓周率和求兩個(gè)數(shù)的最大值的宏函數(shù)。在main函數(shù)中,我們使用const關(guān)鍵字定義了常量radiusab,分別表示圓的半徑和兩個(gè)整數(shù)。在計(jì)算圓的周長和兩個(gè)數(shù)的最大值時(shí),我們直接使用了PIMAX來代表圓周率和求最大值的宏函數(shù),使得代碼更加簡潔和易讀。

0