plz分析o / p? [英] plz analyze the o/p ?
问题描述
#include< conio.h>
#include< stdio.h>
#define swap(a,b)temp = a; A = B; b = temp;
void main()
{
int i,j,temp;
clrscr();
i = 5;
j = 10;
temp = 0;
if(我> j)
swap(i,j);
printf("%d%d%d",i,j,temp);
getch();
}
为什么o / p-> 10 0 0
#include<conio.h>
#include<stdio.h>
# define swap(a,b) temp=a; a=b; b=temp;
void main( )
{
int i, j, temp;
clrscr() ;
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
getch() ;
}
why o/p->10 0 0
推荐答案
Sweety在31/07/04写道:
Sweety wrote on 31/07/04 :
#define swap(a,b)temp = a; A = B; b = temp;
if(i> j)
swap(i,j);
# define swap(a,b) temp=a; a=b; b=temp;
if( i > j)
swap( i, j );
宏很棘手。这扩展到:
if(i> j)
temp = a;
a = b;
b = temp;
感觉更好?
你可能想要:
#define swap(a,b)\
do \
{\
int temp = a; \
a = b; \
b = temp;
} \
而(0)
因此你可以摆脱外部''临时'' (仅适用于''int'')。
-
Emmanuel
C-FAQ: http://www.eskimo.com/~scs/C-faq /faq.html
C是一个锋利的工具
Macros are tricky. This expands to:
if( i > j)
temp=a;
a=b;
b=temp;
Feel better?
You probably want:
# define swap(a,b)\
do \
{ \
int temp=a; \
a=b; \
b=temp; \
} \
while (0)
hence you can get rid of the external ''temp'' (works for ''int'' only).
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
巨魔/白痴(接受你的选择)Sweety再一次证明他没有关注新闻组,查看常见问题解答,甚至打开一本
小学C教科书后发布了
:
The troll/idiot (take your pick) Sweety proved once again that he post
without following the newsgroup, checking the FAQ, or even opening an
elementary C textbook:
#include< conio.h>
^^^^^^^^^你还不知道那是'偏离主题吗?
#include< stdio.h> ;
#define swap(a,b)temp = a; A = B; B =温度;
你还没有检查常见问题解答,找到正确的多语句方式
宏?
void main()
^^^^
你还没想到那是文盲和非法的吗?
{
int i, j,temp;
clrscr();
^^^^^^
你不知道比这更好吗?
i = 5;
j = 10;
temp = 0;
if(i> j)
swap(i,j);
printf("%d%d%d",i,j,temp) ;
getch();
^^^^^^^
来吧。如果你只是发布垃圾并且不注意,当你被视为巨魔或白痴(接受你的选择)时,不要抱怨
你明显是。
}
为什么o / p-> 10 0 0
#include<conio.h>
^^^^^^^^^ You still don''t know that''s off-topic?
#include<stdio.h>
# define swap(a,b) temp=a; a=b; b=temp;
Haven''t you checked the FAQ for the right way to write multistatement
macros?
void main( ) ^^^^
Haven''t you figured out yet that that is illiterate and illegal?
{
int i, j, temp;
clrscr() ; ^^^^^^
Don''t you know better than this yet?
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
getch() ; ^^^^^^^
Come, on. If you just post crap and pay no attention, don''t complain
when you are treated like the troll or idiot (take your pick) that you
clearly are.
}
why o/p->10 0 0
走开。
Go away.
sw ****** *****@yahoo.co.in (Sweety)写道:
sw************@yahoo.co.in (Sweety) writes:
#include< conio.h>
#include< stdio.h>
#define swap(a,b)temp = a; A = B; b = temp;
void main()
{i / j,temp,
clrscr();
i = 5;
j = 10;
temp = 0;
if(i> j)
swap(i,j);
printf("%d%d%d" ;,i,j,temp);
getch();
}
为什么o / p-> 10 0 0
#include<conio.h>
#include<stdio.h>
# define swap(a,b) temp=a; a=b; b=temp;
void main( )
{
int i, j, temp;
clrscr() ;
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
getch() ;
}
why o/p->10 0 0
*请在此处提出更多问题之前阅读常见问题解答。它在
< http://www.eskimo.com/~scs/C-faq/faq.html>。我们宁愿花时间回答真正的问题而不是一遍又一遍地覆盖
的基础知识。
声明void main()是错的;正确的声明是
" int main(void)" (或int main(int argc,char ** argv)如果你想
使用命令行参数)。
< ; CONIO.H> header,以及clrscr()和getch()函数,是非标准的
。你的程序无需使用它们
(尽管你可能需要类似最终的getch()来保持
输出窗口打开)。 br />
程序的输出应以换行符结尾(\ n);否则,
无法保证输出会显示出来。 (在你的系统上可能会发生
,但我们对这里的便携式代码很感兴趣。)
关于你的实际问题:你需要记住的是,宏扩展对文本起作用,而不是对语句或表达式(*)起作用。一个
宏调用可能看起来像一个函数调用,但除非宏写得很仔细,否则它不会像一个一样行为。你的
交换宏不是。
你有:
if(i> j)
swap(i,j);
(BTW,第二行应该缩进。)
预处理器将其扩展为:
if(i> j)
temp = i; I =焦耳; j = temp;
相当于:
if(i> j)
temp = i;
i = j;
j = temp;
只有第一项作业由if语句控制;
其他人无条件执行。
交换宏的改进版本可能是:
#define交换(a,b)\
做{\
int temp; \
temp =(a); (A)=(B); (B)=温度; \\ /
} while(0)
使用此定义,您可以消除临时声明。
如果您对此宏观定义有任何疑问,请不要通过阅读C常见问题解答第10部分来回答这些问题,请回过头来,我们将很高兴
很高兴帮助。如果您有问题* *由常见问题解答
,请阅读常见问题解答;不要浪费每个人的时间
在这里张贴它们。
更改你的声明
void main()
到
int main(无效)
将有助于在这里更加认真地对待。
(*)实际上预处理器适用于proprocessor
令牌的序列,但效果实际上是相同的。
-
Keith Thompson(The_Other_Keith) ks***@mib.org < http:// www。 ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
*Please* read the FAQ before asking more questions here. It''s at
<http://www.eskimo.com/~scs/C-faq/faq.html>. We''d much rather spend
time answering real questions than covering the basics that have
already been covered again and again.
The declaration "void main()" is wrong; the correct declaration is
"int main(void)" (or "int main(int argc, char **argv)" if you want to
use command-line arguments).
The <conio.h> header, and the clrscr() and getch() functions, are
non-standard. There''s no need for your program to use them anyway
(though you might need something like the final getch() to keep the
output window open).
The program''s output should end in a newline ("\n"); otherwise,
there''s no guarantee that the output will show up. (It might happen
to do so on your system, but we''re interested in portable code here.)
On to your actual problem: you need to keep in mind that macro
expansion works on text, not on statements or expressions (*). A
macro invocation might look like a function call, but it''s not going
to behave like one unless the macro is written very carefully. Your
swap macro isn''t.
You have:
if( i > j)
swap( i, j );
(BTW, the second line should be indented.)
The preprocessor expands this to:
if( i > j)
temp=i; i=j; j=temp;
which is equivalent to:
if (i > j)
temp = i;
i = j;
j = temp;
Only the first assignment is controlled by the if statement; the
others are executed unconditionally.
An improved version of your swap macro might be:
#define swap(a,b) \
do { \
int temp; \
temp=(a); (a)=(b); (b)=temp; \
} while(0)
With this definition, you can eliminate the declaration of temp.
If you have any questions about this macro definition that aren''t
answered by reading section 10 of the C FAQ, come back and we''ll be
glad to help. If you have questions that *are* answered by the FAQ
please answer them by reading the FAQ; don''t waste everybody''s time by
posting them here.
Changing your declaration
void main()
to
int main(void)
will help to to be taken far more seriously around here.
(*) Actually the preprocessor works on sequences of "proprocessor
tokens", but the effect is practically the same.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于plz分析o / p?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!