读书人

水木清华的一个帖子问问CSDN下的大人

发布时间: 2012-09-04 14:19:30 作者: rapoo

水木清华的一个帖子,问问CSDN上的大人们(关于printf函数)

C/C++ code
char *p="%s"; printf(p);



gcc下结果是这个:
[toor@zcm test]$ cc printf.c
[toor@zcm test]$ a.out
__libc_start_main[toor@zcm test]$

cygwin下没有结果输出

考虑printf函数是个变参函数,
C/C++ code
 #include <stdio.h>int printf(const char *format, ...);

应该是什么结果也没有啊
大家也讨论讨论吧

[解决办法]
我输出了一串乱码。

探讨

这样结果是不可预料的,与各种环境相关联。

[解决办法]
从变参函数入栈和取值来分析:
1)取值:
C/C++ code
va_list ap;type val;va_start(ap, last);val = (type)va_arg(ap, type);  ....va_end(ap);
[解决办法]
只贴标准:

7.19.6.1 The fprintf function

The fprintf function writes output to the stream pointed to by stream, under control
of the string pointed to by format that specifies how subsequent arguments are
converted for output. If there are insufficient arguments for the format, the behavior is
undefined

printf就等价于fprintf

读书人网 >C语言

热点推荐