C语言中对象式宏

 

001、不使用对象式宏

复制代码
[root@localhost test]# ls
test.c
[root@localhost test]# cat test.c                 ##  测试程序
#include <stdio.h>

int main(void)
{
        int i, sum = 0;
        int v[5] = {3, 8, 2, 4, 6};               ## 定义int【5】 型数for(i = 0; i < 5; i++)
        {
                sum += v[i];
        }

        printf("sum = %5d\n", sum);
        printf("mean = %.5f\n", (double)sum/5);                     ## 求和和均return 0;
}
[root@localhost test]# gcc test.c                                  ##  编译
[root@localhost test]# ls
a.out  test.c
[root@localhost test]# ./a.out                                      ## 执行程
sum =    23
mean = 4.60000
复制代码

 

002、使用对象式宏

复制代码
[root@localhost test]# ls
test.c
[root@localhost test]# cat test.c                  ## 测试程序
#include <stdio.h>
#define NUMBER 5                                   // 定义对象式宏

int main(void)
{
        int i, sum = 0;
        int v[NUMBER] = {3, 8, 2, 4, 6};             //在程序中使用NUMBER宏

        for(i = 0; i < NUMBER; i++)
        {
                sum += v[i];
        }

        printf("sum = %5d\n", sum);
        printf("mean = %.5f\n", (double)sum/NUMBER);

        return 0;
}
[root@localhost test]# gcc test.c -o kkk                           ## 编译程序
[root@localhost test]# ls
kkk  test.c
[root@localhost test]# ./kkk         
sum =    23
mean = 4.60000
复制代码

 。

对象式宏不就是个变量替换嘛?

 

003、替代方法

复制代码
[root@localhost test]# ls
test.c
[root@localhost test]# cat test.c                     ## 测试程序
#include <stdio.h>

int main(void)
{
        int i, sum = 0;
        int NUMBER;
        NUMBER = 5;
        int v[NUMBER] = {3, 4, 5, 8, 3};           // 数组数量用变量无法进行初始化,为什么?  此处必须使用对象式宏, 优势?

        for(i = 0; i < NUMBER; i++)
        {
                sum += v[i];
        }

        printf("sum = %5d\n", sum);
        printf("mean = %.5f\n", (double)sum/NUMBER);

        return 0;
}
[root@localhost test]# gcc test.c -o kkk                             ## 无法正常编译
test.c: In function ‘main’:
test.c:8:9: error: variable-sized object may not be initialized
    8 |         int v[NUMBER] = {3, 4, 5, 8, 3};
      |         ^~~
复制代码

 。

 

004、测试

复制代码
[root@localhost test]# ls                    ## 三个测试程序
test2.c  test3.c  test.c
[root@localhost test]# cat test.c             ## 程序1
#include <stdio.h>
int main(void)
{
        int v[5] = {1,2,4,3,4};
        return 0;
}
[root@localhost test]# cat test2.c            ## 程序2
#include <stdio.h>
int main(void)
{
        int NUMBER = 5;
        int v[NUMBER] = {1,2,4,3,4}; 
        return 0;
}
[root@localhost test]# cat test3.c            ## 程序3
#include <stdio.h>
#define NUMBER 5
int main(void)
{
        int v[NUMBER] = {1,2,4,3,4};
        return 0;
}
[root@localhost test]# gcc test.c -o aaa
[root@localhost test]# gcc test2.c -o bbb       ## 只有程序2无法正常编译,变量初始化数组造成的?  这是对象式宏存在的原因?
test2.c: In function ‘main’:
test2.c:5:9: error: variable-sized object may not be initialized
    5 |         int v[NUMBER] = {1,2,4,3,4};
      |         ^~~
[root@localhost test]# gcc test3.c -o ccc
[root@localhost test]# ls
aaa  ccc  test2.c  test3.c  test.c
复制代码

 。

 

posted @   小鲨鱼2018  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2023-10-04 linux 中设置用户间针对单一目录的权限
2023-10-04 python 中map函数
2023-10-04 samtools线程数对 sam文件转换为bam文件效率的影响
2023-10-04 bwa 线程数目对比对速度的影响
2023-10-04 python 中 re模块
2021-10-04 文本文件和二进制文件有什么区别?
2020-10-04 R语言中duplicated函数:去重复,取重复
点击右上角即可分享
微信分享提示