3

I was trying to execute system calls from c. When the following code is executed, the date is printed first followed by " Todays date is ..........:" on a new line. When I replaced printf by puts, it executed as I intended.(the objdump showed puts@plt in place of the second printf). Can anybody tell me why it is so?

  #include <stdlib.h>

    int main() { printf(" Todays date is ..........:");

    system("/bin/date");
    printf("\n This is your exclusive shell\n");  
    system("/bin/sh");
    return 0; 
    }

Thanks in advance.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
user146297
  • 79
  • 8

4 Answers4

4

The printf() put your string in a buffer, and once you go down a line it write it to the screen. that's why when you do

printf(" Todays date is ..........:");

system("/bin/date");

You might get the date printed first.

The stdout stream is buffered, so will only display what's in the buffer after it reaches a newline (or when it's told to). You have a few options to print immediately:

  • Print to stderr instead using fprintf:

    fprintf(stderr, "I will be printed immediately");
    
  • Flush stdout whenever you need it to using fflush:

    printf("Buffered, will be flushed");
    fflush(stdout); // Will now print everything in the stdout buffer
    
  • or you can also disable buffering on stdout by using setbuf:

    setbuf(stdout, NULL);
    
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • Suppose if OP adds `\n` in first `printf("Today......\n")` then he should get uniform behavior, because `\n` force to `fflush`. Am I correct? – Grijesh Chauhan Aug 20 '13 at 11:47
  • @GrijeshChauhan it does not work on my system. but all other suggestions work – user146297 Aug 20 '13 at 12:10
  • @user146297 Thanks for informing. Though NoIdeaForName's answer perfectly clears what happened and how to resolve. I think `\n` fflush is implementation defined if I get any link then I will share with you. – Grijesh Chauhan Aug 20 '13 at 12:18
  • It depends if the stream is fully buffered (_IOFBF) or line buffered (_IOLBF). The standard does not state anything about how stdout should behave. But usually it is defaultly set to line buffered (or even unbuffered) in interactives modes. – NoWiS Aug 20 '13 at 12:53
3
printf(" Todays date is ..........:");

==>

printf(" Todays date is ..........:\n");

Or add a fflush(stdout); after the printf line;

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
  • `\n` always fflush in C **?** , because [this post](http://stackoverflow.com/questions/213907/c-stdendl-vs-n) says it doesn't – Grijesh Chauhan Aug 20 '13 at 11:50
  • I believe it flushes if stdout is connected to a line-buffered terminal, but it is implementation- (and system-) dependent. – Vatine Aug 20 '13 at 11:52
  • @GrijeshChauhan In line buffer `\n` flush the buffer. Terminal is line buffer by default – Lidong Guo Aug 20 '13 at 12:24
  • @LidongGuo that I also observe in past. But is it well defined behavior in C? – Grijesh Chauhan Aug 20 '13 at 12:25
  • @GrijeshChauhan No , just some kind if easy way. – Lidong Guo Aug 20 '13 at 12:48
  • 1
    @GrijeshChauhan No, it's not specified in the C standard. It may be defined in POSIX, though. This is one of those cases where it crucially depends on what stdout actuall means ("stdout" is, I believe, well-defined as SOMETHING in a C program compiled in a hosted environment, but it could be a regular file, a terminal or even network I/O and if the newline `printf("foo\n")` flushes or not is outside the scope of the C standard (and it probably does different things under Unix, Windows or VMS anyway). – Vatine Aug 22 '13 at 10:33
2

printf use a buffer.
If you want to print the text immediately you have to call fflush

printf(" Todays date is ..........:");
fflush(stdout);
system("/bin/date");
Michael M.
  • 2,556
  • 1
  • 16
  • 26
1
  #include <stdlib.h>  
  #include <stdio.h>

    int main() { printf(" Todays date is ..........:\n"); //add \n at the end. this is same behavior as puts. now date will print after this

    system("/bin/date");
    printf("\n This is your exclusive shell\n");  
    system("/bin/sh");
    return 0; 
    }  

or else you can use fflush(stdout); after printf("Todays date is ....:"); statement

Gangadhar
  • 10,248
  • 3
  • 31
  • 50