in Education by
This question already has an answer here: C/Unix Strange behaviour while using system calls and printf (1 answer) Closed 3 years ago. I just started using the system() function in c, and I thought of starting the same executable from within it self using the system function, so I wrote the following program #include #include int main() { printf("some string"); system("./a.out"); } -I used gcc to compile it- when I ran the program it did not print anything, it just kept going until I used the shortcut ctrl-c to stop the execution,then it started printing the output(it did not print anything until I stopped it) I believe the statements should execute sequentially, why did it not print anything until I stopped it? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
By default, when stdoutis connected to a terminal, it is line-buffered. printf("some string"); doesn't have a '\n' in it and you aren't calling fflush(stdout); after it either, so all this printf("some string"); does is copy "some string" into your stdout's output buffer. The buffer is flushed as the end of main. printf("some string\n"); would flush the buffer immediately, provided stdout is connected to a terminal and you didn't change stdout's buffering. printf("some string"); fflush(stdout); will flush the buffer immediately regardless of context and without the need for the '\n'.

Related questions

0 votes
    This question already has an answer here: C/Unix Strange behaviour while using system calls and printf (1 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: C/Unix Strange behaviour while using system calls and printf (1 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Creating a simple server-client program using rpcgen. I'm writing this .x file: program REMOTE_PROG { version MSG_RCV { int ... xdr_int; local = (char *(*)()) strlen_1_svc; //...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I am a newbie trying to code a serial driver(PCI based ) and I don't want to use the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    In my signal.h file I have added a signal handler like this: #define SIG_WPG ((__sighandler_t)3) and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Where does Friday come before Thursday?...
asked Feb 11, 2021 in Education by JackTerrance
0 votes
    Q.32 :-What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile? ... y); return 0; Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Which of these is the method which is executed first before execution of any other thing takes place in a ... questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    Positional parameters are coded first before any Keyword parameter and parameters are separated by ‘Commas’ only. State whether true or false? A - False B - True...
asked Dec 4, 2020 in Technology by JackTerrance
0 votes
    I am trying to understand the usage of private const in the class. My understanding is that private const is used to make ... (void) { int const MyExample::x = 3; std::cout...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Before starting to enter data in a table, you must create the: a. Design of the table c. Database from which to ... d. All of these Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    Someone (from outside) who tests security issues for bugs before launching a system or application, and who is not ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 5, 2021 in Education by JackTerrance
...