简体   繁体   中英

printf doesn't print on the screen

I downloaded this file ToyVpnServer.cpp and executed the instructions in the file header. then i complied it with gcc ToyVpnServer.cpp and then it created a.out, and i ran it (as said in the file header) ./a.out tun0 8000 test -m 1400 -a 10.0.0.2 32 -d 8.8.8.8 -r 0.0.0.0 0 .before i compiled it, as below, i added one line printf("%d",1000); in the beginning of the main function which the means it should print 1000 on the screen as soon as executed. but nothing shows and the program keeps running. only when the count of parameters is less than 5, the printf function under if (argc < 5) works!
I test it on both Ubuntu14 and 16.

what's wrong with it?

...
//-----------------------------------------------------------------------------

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.?

No , printf() is a library function and its job is to put the data into stdout buffer not directly on console and stdout stream is line buffered ie it will display its content only once it reaches the new line.

Here

printf("%d",1000);

printf() doesn't clear/flush the stdout stream default, programmer need to do. One way to solve this is use fflush(stdout) like

printf("%d",1000);
fflush(stdout);

or use new line character like

printf("%d\n",1000); /* new line character clears the stdout buffer here */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM