Reputation: 255
I am confused about the role of '\n' in fprintf. I understand that it copies characters into an array and the \n character signals when to stop reading the character in the current buffer. But then, when does it know to make the system call write.
for example, fprintf(stdout,"hello") prints but I never gave the \n character so how does it know to make the system call.
Upvotes: 1
Views: 376
Reputation: 23218
The assumptions you've stated about the purpose and use of \n
are wrong. \n
is simply one of 255 ASCII characters that are read, i.e. they are not commands, or directives, that cause anything to happen, they are just passive characters that when used in various C functions (eg. sprintf(), printf(), fprintf(), etc.) are interpreted such that presentation of output is manipulated to appear in the desired way.
By the way, \n
(new line) is in good company with many other formatting codes which you can see represented HERE
Upvotes: 0
Reputation: 39807
The system call is made when the channel is synced/flushed. This can be when the buffer is full (try writing a LOT without a \n
and you'll see output at some point), when a \n
is seen (IF you have line buffering configured for the channel, which is the default for tty devices), or when a call to fflush()
is made.
When the path is closed, it will be flushed as well. When the process terminates, the operating system will close any open paths it has. Each of these events will lead to the system call to emit the output happening.
Upvotes: 5
Reputation: 3952
First of all, fprintf("hello");
isn't correct, it should be for instance fprintf(stdout, "hello");
. Next, \n doesn't implies to stop reading or writing characters, because \n is itself a character, the linefeed (10th in ascii table).
Upvotes: 0