简体   繁体   English

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

[英]Why does my C program output this?

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 ; 输出应该是102但我得到20 ; why is it so? 为什么会这样?

#define SQUARE(x) x*x

should be 应该

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

Indeed, without the parentheses, 2*(su*t)/SQUARE(t) is expanded as 实际上,没有括号, 2*(su*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. 至于你的第二个问题, FUN(val1,2)将按照##运算符的语义扩展为val12 It is still not clear what your intent is: the printf line will be understood as 目前还不清楚你的意图是什么: printf系列将被理解为

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

which will print 20 . 这将打印20

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: 现在,既然我们在SQUARE的定义中没有() ,我们得到:

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(提醒:##“连接”定义的字符串 - 我找不到完美的单词以便解释它所以只需看看这个例子......):

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.. 我希望它有帮助..

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. 这是预处理器常见的错误。

i know i am late, but i am having the perfect answer. 我知道我迟到了,但我得到了完美的答案。

in c # at define is used to call the text as it is in the function parameter, 在c#at define中用于调用函数参数中的文本,

example, #define hai(s1) printf("%s=%s",#s1,s1); 例如,#define hai(s1)printf(“%s =%s”,#s1,s1);

       in main: i am calling as hai(tom); tom was initialized as "india" string.

the output for this is tom=india, the calling string tom is printed by help of #. 这个输出是tom = india,调用字符串tom是在#的帮助下打印的。

similarly ## is used to take the text from function argument and join them and return the value of the joined identifier. 类似地,##用于从函数参数中获取文本并将它们连接起来并返回连接标识符的值。

the above program has two argument va1 and 2. passed to i and j. 上面的程序有两个参数va1和2.传递给i和j。 then va1 and 2 is joined. 然后加入va1和2。 and form va12. 并形成va12。

va12 is the identifier available with value 20. that's why 20 is returned. va12是值20的可用标识符。这就是返回20的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM