gpt4 book ai didi

c - 使用 define 指令的 Square 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:48 27 4
gpt4 key购买 nike

我实现了一个平方定义指令,如下所示:

#include <stdio.h>

#define SQR(y) ((y)*(y))

int main() {
int a;
printf("Enter : ");
scanf("%d",&a);
a = SQR(a);
printf("The square is: %d\n",SQR(a));
return 0;
}

但是当我执行它时,我没有收到任何错误,但每次都给出错误的答案。

为什么输入是 4 次方而不是 2 次方?

最佳答案

你将它平方两次。

在您的代码中:

a = SQR(a);
printf("The square is: %d\n",SQR(a));
  1. 这首先使得 a = a*a

  2. 现在打印SQR((a*a)*(a*a)),或者a*a*a*a

您可以将 printf 中的 SQR(a) 替换为 a、删除 a = SQR(a)

试试这个:

#include <stdio.h>
#define SQR(y) ((y)*(y))

int main() {
int a;
printf("Enter : ");
scanf("%d",&a);
printf("The square is: %d\n",SQR(a));
return 0;
}

关于c - 使用 define 指令的 Square 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30148835/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com