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.

4 Answers 4

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);
    
Sign up to request clarification or add additional context in comments.

4 Comments

Suppose if OP adds \n in first printf("Today......\n") then he should get uniform behavior, because \n force to fflush. Am I correct?
@GrijeshChauhan it does not work on my system. but all other suggestions work
@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.
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.
3
printf(" Todays date is ..........:");

==>

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

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

6 Comments

\n always fflush in C ? , because this post says it doesn't
I believe it flushes if stdout is connected to a line-buffered terminal, but it is implementation- (and system-) dependent.
@GrijeshChauhan In line buffer \n flush the buffer. Terminal is line buffer by default
@LidongGuo that I also observe in past. But is it well defined behavior in C?
@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).
|
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");

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.