122Ml 发表于 2023-9-18 22:40:44

关于宏定义函数

函数目的:对给定数组,求其中最小值

以下是宏定义程序:
#include <stdio.h>
#define min(a,b) a>=b?b:a
#define length 10
//int min(int a, int b);
//求最小值
int main()
{
        int temp1=1000,temp2=999,i;
        int arr{5,21,96,8,42,61,345,14,96,45};

        for(i=1;i<=length-1;i++)
        {
                //printf("debug1:%d\n", min(arr, arr));
                if (min(arr, arr) == arr)
                {
                        temp1 = arr;
                        printf("debug/1:%.3d    ", temp1);
                }
                else
                {
                        temp1 = arr;
                        printf("debug/2:%.3d    ", temp1);
                }
                temp2 = min(temp1, temp2);
                printf("debug/3:temp1:%.3d    temp2:%.3d\n",temp1,temp2);
        }

        printf("result:%d", temp2);
        return 0;
}

/*int min(int a, int b)
{
        return(a >= b ? b : a);
}
*/

对应输出:
debug/1:021    debug/3:temp1:021    temp2:021
debug/1:096    debug/3:temp1:096    temp2:021
debug/1:008    debug/3:temp1:008    temp2:008
debug/1:042    debug/3:temp1:042    temp2:008
debug/1:061    debug/3:temp1:061    temp2:008
debug/1:345    debug/3:temp1:345    temp2:008
debug/1:014    debug/3:temp1:014    temp2:008
debug/1:096    debug/3:temp1:096    temp2:008
debug/1:045    debug/3:temp1:045    temp2:008
result:8

下面是用函数处理的版本:
#include <stdio.h>
//#define min(a,b) a>=b?b:a
#define length 10
int min(int a, int b);
//求最小值
int main()
{
        int temp1=1000,temp2=999,i;
        int arr{5,21,96,8,42,61,345,14,96,45};

        for(i=1;i<=length-1;i++)
        {
                //printf("debug1:%d\n", min(arr, arr));
                if (min(arr, arr) == arr)
                {
                        temp1 = arr;
                        printf("debug/1:%.3d    ", temp1);
                }
                else
                {
                        temp1 = arr;
                        printf("debug/2:%.3d    ", temp1);
                }
                temp2 = min(temp1, temp2);
                printf("debug/3:temp1:%.3d    temp2:%.3d\n",temp1,temp2);
        }

        printf("result:%d", temp2);
        return 0;
}

int min(int a, int b)
{
        return(a >= b ? b : a);
}

下面是对应输出:
debug/2:005    debug/3:temp1:005    temp2:005
debug/2:021    debug/3:temp1:021    temp2:005
debug/1:008    debug/3:temp1:008    temp2:005
debug/2:008    debug/3:temp1:008    temp2:005
debug/2:042    debug/3:temp1:042    temp2:005
debug/2:061    debug/3:temp1:061    temp2:005
debug/1:014    debug/3:temp1:014    temp2:005
debug/2:014    debug/3:temp1:014    temp2:005
debug/1:045    debug/3:temp1:045    temp2:005
result:5

问题:
为什么第一个程序跑不对,第二个程序跑得对?{:10_280:}

求大佬解答

isdkz 发表于 2023-9-18 22:40:54

第一个程序的问题在于宏定义函数中的参数不是常量,而是变量,导致在使用时会出现问题。例如在第一个程序中,当执行min(arr[ i], arr)时,实际上会被展开为arr[ i]>=arr?arr:arr[ i],这样就会出现问题,因为arr[ i]和arr都是变量,展开后的结果可能不是我们期望的。而第二个程序中使用的是函数,参数是常量,不存在这个问题,所以结果是正确的。

122Ml 发表于 2023-9-18 22:56:37

isdkz 发表于 2023-9-18 22:40
第一个程序的问题在于宏定义函数中的参数不是常量,而是变量,导致在使用时会出现问题。例如在第一个程序中 ...

感谢!
页: [1]
查看完整版本: 关于宏定义函数