关于C语言宏的问题 [英] Question about macro in C language
本文介绍了关于C语言宏的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好
任何人都可以告诉我ans 11如何进入以下计划
Hello
can any one tell me the how the ans 11 come in following program
#include <stdio.h>
#include <conio.h>
#define SQR(x) (x * x)
void main()
{
int a , b=3;
clrscr();
a=SQR(b + 2);
printf("The value of B is %d",b);
printf("\nThe ans of function is %d ",a);
getch();
}
我的尝试:
当我更改a = SQR(b + 3)中的值时输出为15
和
我更改a = SQR中的值( b + 4)输出是19
如何输出???
What I have tried:
when I change the value in a=SQR(b+3) the output is 15
and
I change the value in a=SQR(b+4) the output is 19
how that out put come???
推荐答案
#define
不是宏:它是一个文本替换处理器。
所以当你这样写:
#define
is not a macro: it's a text substitution processor instead.
So when you write this:
#define SQR(x) (x * x)
a=SQR(b + 2);
文本完全取代,你得到这个:
The text is subsituted exactly, and you get this:
a=(b + 2 * b + 2);
其中包含
which works out as
a = b + (2 * b) + 2 = 3 + (2 * 3) + 2 = 3 + 6 + 2 = 11
这就是为什么在 #define
中过度使用括号非常重要声明!
That's why it's vitally important to overuse brackets in #define
statements!
#define SQR(x) ((x) * (x))
void main()
{
int a , b=3;
clrscr();
a=SQR(b + 2);
会给你你想要的东西。
这篇关于关于C语言宏的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文