由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请高人解释一下为啥这个输出总是"HELLO-ERR"
相关主题
大家帮我看看C文件输入函数fprintf的问题how can I get external program's result in C
再问个fork的题 (转载)菜鸟请教个hadoop streaming job 的问题 (转载)
c preprocess question50伪币:请教perl代码差错的问题!多谢啦!
又一个GDB的问题:关于显示数据一个 perl 的 print 的初级问题
搜狐一题,寻解法Python有什么好的方法建two-way pipe?
shm_open failednode.js multer: Recursive process.nextTick detected
A question related to pipenode.js child process: 怎样保证1个命令执行完了再执行下一个?
问一个vc++ 2008的问题请教一个python下面popen的问题
相关话题的讨论汇总
话题: hello话题: fprintf话题: err话题: include话题: stdout
进入Programming版参与讨论
1 (共1页)
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
9
http://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros
look the stringification section
谢谢你的包子,我笑纳了啊。
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
12
完了, 我包子要退回去么?
F********g
发帖数: 475
13
不用,算电子书了哈哈

【在 d****n 的大作中提到】
: 完了, 我包子要退回去么?
1 (共1页)
进入Programming版参与讨论
相关主题
请教一个python下面popen的问题搜狐一题,寻解法
PyCharm里的Python启动的Process在等待按键,如何继续shm_open failed
c++ template中如何判断类型A question related to pipe
请问如何恢复正常的IO?问一个vc++ 2008的问题
大家帮我看看C文件输入函数fprintf的问题how can I get external program's result in C
再问个fork的题 (转载)菜鸟请教个hadoop streaming job 的问题 (转载)
c preprocess question50伪币:请教perl代码差错的问题!多谢啦!
又一个GDB的问题:关于显示数据一个 perl 的 print 的初级问题
相关话题的讨论汇总
话题: hello话题: fprintf话题: err话题: include话题: stdout