C语言定义常量define的应用实例

   2024-10-08 5410
核心提示:#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

#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,分别表示圆周率和求两个数的最大值的宏函数。在main函数中,我们使用const关键字定义了常量radiusab,分别表示圆的半径和两个整数。在计算圆的周长和两个数的最大值时,我们直接使用了PIMAX来代表圆周率和求最大值的宏函数,使得代码更加简洁和易读。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号