C语言中怎么定义一个常量

今天就跟大家聊聊有关C语言中怎么定义一个常量,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

通过在C语言里面定义常量,可以避免代码出现魔数。

#include <stdio.h>

int main()
{

    const int AMOUNT = 100;
    int price = 0;
    printf("please input amount of money:");
    scanf("%d",&price);
    int change = AMOUNT - price;
    printf("give you back %d yuan.\n",change);
    return 0;

}

定义double类型变量

#include <stdio.h>
int main()
{
    printf("height in foot and inch please :");
    double foot;
    double inch;
    scanf("%lf %lf",&foot,&inch);
    printf("height is:%f meters\n",((foot + inch / 12)* 0.3048));
    return 0;
//    printf("%f\n",10.0/3*3);
//    return 0;
//
    
}

看完上述内容,你们对C语言中怎么定义一个常量有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。