C语言定义常量define的应用实例
导读:#include <stdio.h> #define PI 3.14159 #define MAX(x,y ((x > (y ? (x : (y int main( { const double r...
#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
来定义常量PI
和MAX
,分别表示圆周率和求两个数的最大值的宏函数。在main
函数中,我们使用const
关键字定义了常量radius
、a
和b
,分别表示圆的半径和两个整数。在计算圆的周长和两个数的最大值时,我们直接使用了PI
和MAX
来代表圆周率和求最大值的宏函数,使得代码更加简洁和易读。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: C语言定义常量define的应用实例
本文地址: https://pptw.com/jishu/683232.html