F********g 发帖数: 475 | 1 请高人解释一下为啥这个输出总是"HELLO-ERR"
#include
#include
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
} | X****r 发帖数: 3557 | 2 stdout不会自动flush
【在 F********g 的大作中提到】 : 请高人解释一下为啥这个输出总是"HELLO-ERR" : #include : #include : int main() : { : while(1) : { : fprintf(stdout,"hello-out"); : fprintf(stderr,"hello-err"); : sleep(1);
| F********g 发帖数: 475 | 3 Thanks, 思考猪, now it's working
#include
#include
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fflush(stdout);
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
} | d****n 发帖数: 1637 | 4 if you dont mind change the output to adding '\n'
then it will work.
'\n' can flush, no need to call fflush()
#include
#include
int main()
{
while(1)
{
fprintf(stdout,"hello-out\n");
fprintf(stderr,"hello-err\n");
sleep(1);
}
return 0;
} | F********g 发帖数: 475 | 5 谢。再问
#include
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}
Just by looking at the program one "might" expect the output to be, the same
for both the printf statements. But on running the program you get it as:
bash$ ./a.out
12
f(1,2)
第二个MACRO输出为啥不是12 | d****n 发帖数: 1637 | 6 g(f(1,2)) -> g(a) 把 f(1,2) stringfy 了, stringfy 了就不能再扩展了
int main()
{
printf("%s\n","12");
printf("%s\n","f(1,2)");
return 0;
}
###回答的不满意?还是没包子,QQ | F********g 发帖数: 475 | 7 这个SCAN和EXPANSION的RULE很TRICKY哈。
为啥第一个到这步之后可以继续EXPANSION呢? | F********g 发帖数: 475 | 8 太愚钝,还在思考中:(
【在 d****n 的大作中提到】 : g(f(1,2)) -> g(a) 把 f(1,2) stringfy 了, stringfy 了就不能再扩展了 : int main() : { : printf("%s\n","12"); : printf("%s\n","f(1,2)"); : return 0; : } : ###回答的不满意?还是没包子,QQ
| d****n 发帖数: 1637 | | t****t 发帖数: 6806 | 10 "A parameter in the replacement list, unless preceded by a # or ##
preprocessing token or followed by a ## preprocessing token (see below), is
replaced by the corresponding argument after all macros contained therein
have been expanded. Before being substituted, each argument’s preprocessing
tokens are completely macro replaced as if they formed the rest of the
preprocessing file; no other preprocessing tokens are available."
换句话说, 如果参数在后面没有前缀#或者是##的operand, 那么参数先被扩展. 所以在
h(a) -> g(a)里, a不是#或者##的一部分, 所以a先被扩展, 所以h(f(1,2))得到g(12)
-> "12". 但是在g(a)->#a里, 参数a是#的一部分, 不作扩展, 所以g(f(1,2))得到"f(1
,2)".
是很tricky, 但是第一个不是"继续"expand, 而是"先"expand了.
【在 F********g 的大作中提到】 : 这个SCAN和EXPANSION的RULE很TRICKY哈。 : 为啥第一个到这步之后可以继续EXPANSION呢?
| F********g 发帖数: 475 | 11 ORZ TRUST同学。终于想明白了。
C99那么DRY的你咋读下去的,传授经验哈。 | d****n 发帖数: 1637 | | F********g 发帖数: 475 | 13 不用,算电子书了哈哈
【在 d****n 的大作中提到】 : 完了, 我包子要退回去么?
|
|