C 家庭作业是关于使用宏的

C 家庭作业是关于使用宏的,c,C,这些问题是关于我的家庭作业。 此主题需要使用,如: #define GENERIC_MAX(type)\ type type##_max(type x, type y)\ {\ return x > y ? x : y;\ } 问题的内容是使此代码正常运行: #include <stdio.h> GenerateShowValueFunc(double) GenerateShowValueFunc(int) int main() { double i =

这些问题是关于我的家庭作业。
此主题需要使用,如:

#define GENERIC_MAX(type)\
type type##_max(type x, type y)\
{\
    return x > y ? x : y;\
}
问题的内容是使此代码正常运行:

#include <stdio.h>

GenerateShowValueFunc(double)
GenerateShowValueFunc(int)

int main()
{

    double i = 5.2;
    int j = 3;

    showValue_double(i);
    showValue_int(j);

}
这段代码是我目前的进展,但有一些问题:

#include <stdio.h>

#define printname(n) printf(#n);

#define GenerateShowValueFunc(type)\
type showValue_##type(type x)\
{\
    printname(x);\
    printf("=%d\n", x);\
    return 0;\
}

GenerateShowValueFunc(double)
GenerateShowValueFunc(int)

int main()
{

    double i = 5.2;
    int j = 3;

    showValue_double(i);
    showValue_int(j);

}

请将定义GenerateShowValueFunc宏的代码插入ShowValue.c程序中的适当位置,以便该程序能够顺利编译和运行。

一个快速而肮脏的解决方案是:

type showValue_##type(type x)\
{\
    const char* double_fmt = "=%f\n";\
    const char* int_fmt = "=%d\n";\
    printname(x);\
    printf(type##_fmt, x);\
    return 0;\
}
编译器将优化未使用的变量,因此不会影响性能。但它可能会产生警告“变量未使用”。您可以添加空语句,如
(void)double\u fmt使其静音


无论如何,这都是非常脆弱和容易出现错误的,从来没有建议过编写这样的宏。这并不是你在现代C语言中如何进行泛型编程。你可以通过向老师展示以下示例来教他们如何进行泛型编程:

#include <stdio.h>

void double_show (double d)
{
  printf("%f\n", d);
}

void int_show (int i)
{
  printf("%d\n", i);
}

#define show(x) _Generic((x),\
  double: double_show,       \
  int:    int_show) (x) // the x here is the parameter passed to the function

int main()
{
  double i = 5.2;
  int j = 3;

  show(i);
  show(j);
}
#包括
无效双U显示(双d)
{
printf(“%f\n”,d);
}
无效int_显示(int i)
{
printf(“%d\n”,i);
}
#定义show(x)_Generic((x)\
双人:双人秀\
int:int_show)(x)//这里的x是传递给函数的参数
int main()
{
双i=5.2;
int j=3;
表演(一);
秀(j);;
}

这使用了现代的C11/C17标准
\u Generic
关键字,它可以在编译时检查类型。宏选择要调用的适当函数,并且它是类型安全的。调用方不必担心调用哪个“显示”函数,也不必通过正确的类型。

< P>不改变所显示的C代码(即只做宏),这是我的要求,下面的代码有所需的输出:

#include <stdio.h>

#define showValue_double(input) \
showValueFunc_double(#input"=%.4f\n" , input)

#define showValue_int(input) \
showValueFunc_int(#input"=%d\n" , input)



#define GenerateShowValueFunc(type) \
void showValueFunc_##type(const char format[], type input)\
{\
    printf(format, input); \
}

/* ... macro magic above;      */
/* unchangeable code below ... */

GenerateShowValueFunc(double)
GenerateShowValueFunc(int)

int main()
{

    double i = 5.2;
    int j = 3;

    showValue_double(i);
    showValue_int(j);

}
注意,我为特定类型的格式说明符创建了一个查找表。也就是说,对于要支持的每种类型,您需要添加一个宏
#define showValue…
。将变量的名称输入到输出中也需要这样做

这使用了两个“字符串”由C编译器连接的事实,即A“B”与AB“A”相同。其中
“A”
#输入的结果

其余部分,即所需的函数定义与教师提供的示例非常相似,使用
##
运算符

注意,如果必须在输出中正确提及变量名,则会出现这种情况。

如果没有
i=
,事情会变得更容易,并且会更优雅地使用生成的函数,而不需要调用
showValue\u double(i)是显式宏。也就是说,生成的函数是从
main()
调用的函数的1:1。我想这可能是真正的问题。如果您需要该版本,请告诉我。

C语言不提供使其易于通用处理类型的支持。对于C入门课程来说,这不是一个好的家庭作业。这门课叫什么名字?您可以使用
\u Generic
创建一些类型灵活的代码。如果是这样的话,本课程应该教授关于
\u Generic
;你不需要在作业中发现它。也许作业只是为了制作
通用的\u MAX
宏,你应该使用
int
double
的特定单独代码来演示和测试该宏,不是为演示和测试编写通用代码吗?您可能需要一个宏来使用变量的名称来显示和使用Strug化运算符。这是一个不明智的使用宏IMO。为什么当没有专业开发人员认为这是“正常”的时候,他们为什么要教这些东西呢?@Armali OAOGenerateShowValueFunc(double)
GenerateShowValueFunc(int)
是任务的相关部分。但这可能只是我的阅读。否则,这个解决方案给我留下了一个我以前不知道的特性。我做了一些类似于您的
#define printname(n)printf(#n)但是在内部使用printf并没有真正起作用…我尝试了很多次,但都没有起作用。我认为你的答案与问题最接近。我也这么认为(毫不奇怪…)。但是对于任务之外的学习,我建议也学习其他答案。
type showValue_##type(type x)\
{\
    const char* double_fmt = "=%f\n";\
    const char* int_fmt = "=%d\n";\
    printname(x);\
    printf(type##_fmt, x);\
    return 0;\
}
#include <stdio.h>

void double_show (double d)
{
  printf("%f\n", d);
}

void int_show (int i)
{
  printf("%d\n", i);
}

#define show(x) _Generic((x),\
  double: double_show,       \
  int:    int_show) (x) // the x here is the parameter passed to the function

int main()
{
  double i = 5.2;
  int j = 3;

  show(i);
  show(j);
}
#include <stdio.h>

#define showValue_double(input) \
showValueFunc_double(#input"=%.4f\n" , input)

#define showValue_int(input) \
showValueFunc_int(#input"=%d\n" , input)



#define GenerateShowValueFunc(type) \
void showValueFunc_##type(const char format[], type input)\
{\
    printf(format, input); \
}

/* ... macro magic above;      */
/* unchangeable code below ... */

GenerateShowValueFunc(double)
GenerateShowValueFunc(int)

int main()
{

    double i = 5.2;
    int j = 3;

    showValue_double(i);
    showValue_int(j);

}
i=5.2000
j=3