我下载了这个文件ToyVpnServer.cpp并执行文件头中的指令。然后我遵守了 gcc ToyVpnServer.cpp
然后它创建了 a.out,我运行了它(如文件头中所述)./a.out tun0 8000 test -m 1400 -a 10.0.0.2 32 -d 8.8.8.8 -r 0.0.0.0 0
.在我编译它之前,如下所示,我添加了一行 printf("%d",1000);
在主函数的开头,这意味着它应该打印 1000
执行后立即在屏幕上显示。但没有任何显示并且程序继续运行。仅当参数数量小于 5 时,printf
功能在if (argc < 5)
下作品!
我在Ubuntu14和16上测试了它。
它有什么问题?
...
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
printf("%d",1000);
if (argc < 5) {
printf("Usage: %s <tunN> <port> <secret> options...\n"
"\n"
"Options:\n"
" -m <MTU> for the maximum transmission unit\n"
" -a <address> <prefix-length> for the private address\n"
" -r <address> <prefix-length> for the forwarding route\n"
" -d <address> for the domain name server\n"
" -s <domain> for the search domain\n"
"\n"
"Note that TUN interface needs to be configured properly\n"
"BEFORE running this program. For more information, please\n"
"read the comments in the source code.\n\n", argv[0]);
exit(1);
}
// Parse the arguments and set the parameters.
char parameters[1024];
build_parameters(parameters, sizeof(parameters), argc, argv);
...
最佳答案
it should print 1000 on the screen as soon as executed.?
否,printf()
是一个库函数,它的工作是将数据放入stdout
缓冲区中,而不是直接在控制台上,并且stdout
流是行缓冲的,即只有在到达新行时才会显示其内容。
这里
printf("%d",1000);
printf()
不会清除/刷新 stdout
流默认值,程序员需要这样做。解决此问题的一种方法是使用 fflush(stdout)
像
printf("%d",1000);
fflush(stdout);
或使用换行符,例如
printf("%d\n",1000); /* new line character clears the stdout buffer here */
关于c - printf 不打印在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56822489/