When I execute the c file
#include
#include
int main(){
printf("before the system call. I am too excited!\n");
system("ps");
printf("after the system call. Look what you have done!\n");
}
I get the output
before the system call. I am too excited!
PID TTY TIME CMD
140 pts/0 00:00:03 zsh
1613 pts/0 00:00:00 system
1614 pts/0 00:00:00 sh
1615 pts/0 00:00:00 ps
after the system call. Look what you have done!
but when I remove the '\n' from the first printf(), I get
PID TTY TIME CMD
140 pts/0 00:00:03 zsh
1590 pts/0 00:00:00 system
1591 pts/0 00:00:00 sh
1592 pts/0 00:00:00 ps
before the system call. I am too excited!after the system call. Look what you have done!
So what I am asking is, how the '\n' character changes the printf()
output, and it led me to thinking, does the program stores the text in some buffer before printing, or just prints with the go?
I am getting the expected output for the second if I add fflush(stdout)
for before the system("ps")
, but I want to understand the exact mechanism. When I add fflush(stdout)
, it cleared the buffer of the first printf()
and the output of system("ps")
was appended with it, but without it, what is exactly happening?
Asked by KeShAw
(13 rep)
Mar 6, 2025, 08:48 AM
Last activity: Mar 6, 2025, 09:07 AM
Last activity: Mar 6, 2025, 09:07 AM