Skip to content

定义全局常量

  1. 语句位置

    作为全局常量,其定义位置要与主函数统计,一般情况下定义在编译语句的下面。

  2. 关键字:#define

  3. 代码:

    c
    #include <stdio.h>
    #include <stdlib.h>
    
    // 定义全局常量
    #define LIST_INNIT_LENTH 100
    #define LIST_CREMENT 20
    
    // 主函数
    int main()
    {
    	int a = LIST_INNIT_LENTH + LIST_CREMENT;
    	printf("全局常量的和为:%d\n",a);
    }
  4. 运行效果

typedef 用法

  1. 说明:typedef 是为现有的类型起一个别名,使使用起来更加的方便,注意一点,它并没有产生新的类型。

  2. 代码:

    c
    #include <stdio.h>
    #include <stdlib.h>
    
    // 定义全局常量
    #define LIST_INNIT_LENTH 100			// 总长度
    #define LIST_CREMENT 20					// 每次增加的长度
    
    typedef int let;
    
    // 主函数
    int main()
    {
    	let a = LIST_INNIT_LENTH + LIST_CREMENT;
    	printf("使用 typedef 语法给 int 类型起别名后运行结果为:%d\n",a);
    }
  3. 运行效果

基于 MIT 许可发布