c - printf 不打印在屏幕上

标签 c

我下载了这个文件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/

相关文章:

c - 当来自另一个线程的消息时,Winapi 控件不适本地访问内存

C 问题及解决方案

c - 当我关闭命令提示符时,我的 C 基本交易程序不会存储任何修改

对网络字节顺序和主机字节顺序感到困惑

c - 如何在套接字上触发第一个recv消息以获取NETLINK状态

c - 将文件中的数字存储到数组中

c - 为什么我的程序执行扫描和打印两次?

c - 将整数字符串存储到数组中

c - SDL_Surface 进入 GTK 窗口

C套接字原子非阻塞读取