为什么我的C程序输出这个?

时间:2022-01-10 22:59:48

I am trying to solve two Preprocessor related questions but in both programs I am getting results that I am not able to figure out how. Below is my program:

我正在尝试解决两个预处理器相关的问题,但在这两个程序中,我都得到了我无法计算出的结果。下面是我的计划:

#include<stdio.h>
#define SQUARE(x) x*x
int main()
{
float s=10,u=30 ,t=2,a;
a=2*(s-u*t)/SQUARE(t);
printf("Result:%f\n",a);
return 0;
}

According to me, the output of this programme should be -25.000 but I am getting -100.000.

按照我的说法,这个项目的产出应该是-25.000,但我得到-100.000。

And in second program:

在第二个程序:

#define FUN(i,j) i##j
int main()
{
int val1 = 10;
int val12 = 20;
clrscr();
printf("%d\n",FUN(val1,2));
getch();
}

Output should be 102 but I am getting 20; why is it so?

输出应该是102,但我得到20;为什么如此?

3 个解决方案

#1


4  

the first one:

第一个:

a=2*(s-u*t)/SQUARE(t);

after replacing the define we get:

替换定义后,我们得到:

a=2*(s-u*t)/t*t;

now, since we don't have () in the definition of SQUARE we get:

由于平方的定义中没有()我们得到

a=2*(10-30*2)/2*2; --> a=2*(-50)/2*2; --> a=-100/2*2; --> a=-50*2; --> a=-100

if you want to get -25 you should define SQUARE(x) as (x*x).

如果你想要得到-25,你应该定义SQUARE(x)为(x*x)

Edit : add explanation regarding the second example.

编辑:添加第二个例子的说明。

printf("%d\n"FUN(val1,2));

once again, we first should replace the define (reminder: ## "concatenates" the string of the define - I can't find the perfect words in order to explain it so just take a look at the example...):

再一次,我们首先应该替换define(提示:## "concatenates"定义的字符串-我找不到完美的词来解释它,所以看看这个例子…):

printf("%d\n",val12);  [note: the comma (,) is missing - so it won't compile.]

since the value of val12 is 20 that's what you'll get.

因为val12的值是20,这就是你要得到的。

the point of those 2 examples is to remember that we should always deal with the defines first (since in "real life" the compiler (or pre-processor) does it before the run time)

这两个示例的要点是要记住,我们应该始终先处理定义(因为在“现实生活”中,编译器(或预处理器)在运行时之前执行定义)

I hope it helps..

我希望它可以帮助. .

#2


11  

#define SQUARE(x) x*x

should be

应该是

#define SQUARE(x) ((x)*(x))

Indeed, without the parentheses, 2*(s-u*t)/SQUARE(t) is expanded as

确实,没有括号,2*(s-u*t)/SQUARE(t)被展开为

2*(s-u*t)/t*t

which is interpreted as

这是解释为

(2*(s-u*t)/t)*t

As to your second problem, FUN(val1,2) will get expanded as val12 per the semantics of the ## operator. It is still not clear what your intent is: the printf line will be understood as

至于第二个问题,FUN(val1,2)将根据##操作符的语义扩展为val12。仍然不清楚您的意图是什么:printf行将被理解为

printf("%d\n", val12);

which will print 20.

这将打印20。

#3


3  

For the first case,

对于第一种情况,

a=2*(s-u*t)/SQUARE(t);

would translate to

将转化为

a=2*(s-u*t)/t*t;

at compile time. This is a common mistake made with preprocessors.

在编译时间。这是预处理器经常犯的错误。

#1


4  

the first one:

第一个:

a=2*(s-u*t)/SQUARE(t);

after replacing the define we get:

替换定义后,我们得到:

a=2*(s-u*t)/t*t;

now, since we don't have () in the definition of SQUARE we get:

由于平方的定义中没有()我们得到

a=2*(10-30*2)/2*2; --> a=2*(-50)/2*2; --> a=-100/2*2; --> a=-50*2; --> a=-100

if you want to get -25 you should define SQUARE(x) as (x*x).

如果你想要得到-25,你应该定义SQUARE(x)为(x*x)

Edit : add explanation regarding the second example.

编辑:添加第二个例子的说明。

printf("%d\n"FUN(val1,2));

once again, we first should replace the define (reminder: ## "concatenates" the string of the define - I can't find the perfect words in order to explain it so just take a look at the example...):

再一次,我们首先应该替换define(提示:## "concatenates"定义的字符串-我找不到完美的词来解释它,所以看看这个例子…):

printf("%d\n",val12);  [note: the comma (,) is missing - so it won't compile.]

since the value of val12 is 20 that's what you'll get.

因为val12的值是20,这就是你要得到的。

the point of those 2 examples is to remember that we should always deal with the defines first (since in "real life" the compiler (or pre-processor) does it before the run time)

这两个示例的要点是要记住,我们应该始终先处理定义(因为在“现实生活”中,编译器(或预处理器)在运行时之前执行定义)

I hope it helps..

我希望它可以帮助. .

#2


11  

#define SQUARE(x) x*x

should be

应该是

#define SQUARE(x) ((x)*(x))

Indeed, without the parentheses, 2*(s-u*t)/SQUARE(t) is expanded as

确实,没有括号,2*(s-u*t)/SQUARE(t)被展开为

2*(s-u*t)/t*t

which is interpreted as

这是解释为

(2*(s-u*t)/t)*t

As to your second problem, FUN(val1,2) will get expanded as val12 per the semantics of the ## operator. It is still not clear what your intent is: the printf line will be understood as

至于第二个问题,FUN(val1,2)将根据##操作符的语义扩展为val12。仍然不清楚您的意图是什么:printf行将被理解为

printf("%d\n", val12);

which will print 20.

这将打印20。

#3


3  

For the first case,

对于第一种情况,

a=2*(s-u*t)/SQUARE(t);

would translate to

将转化为

a=2*(s-u*t)/t*t;

at compile time. This is a common mistake made with preprocessors.

在编译时间。这是预处理器经常犯的错误。