L
linq936
Hi,
I read in many places that the string to be outputted by printf()
must be ending with newline, for example, it should be
printf("Hello World.\n");
instead of
printf("Hello World.");
because the latter is "undefined" by standard.
But I tried the following code:
#include <stdio.h>
int main(){
printf("%d", 15);
printf("Type in some number:");
int i;
scanf("%d", &i);
printf("\nHey you typed in %d", i);
return 0;
}
You can see that I do not have '\n" in any of the printf, I see what
I expected:
$>./a.out
15Type in some number:9
Hey you typed in 9$>
I know this could be because the compiler implements the support
and also when program finishes the buffer gets flushed out.
My real question is like this, we see a lot of use cases that
printf() and scanf() are used together, as in my example, use printf()
to prompt user to type in something and then use scanf() to read it
in. It is best not to move the curer to a new line.
Then how that requirement that printf() should end with newline
comes to play?
I read in many places that the string to be outputted by printf()
must be ending with newline, for example, it should be
printf("Hello World.\n");
instead of
printf("Hello World.");
because the latter is "undefined" by standard.
But I tried the following code:
#include <stdio.h>
int main(){
printf("%d", 15);
printf("Type in some number:");
int i;
scanf("%d", &i);
printf("\nHey you typed in %d", i);
return 0;
}
You can see that I do not have '\n" in any of the printf, I see what
I expected:
$>./a.out
15Type in some number:9
Hey you typed in 9$>
I know this could be because the compiler implements the support
and also when program finishes the buffer gets flushed out.
My real question is like this, we see a lot of use cases that
printf() and scanf() are used together, as in my example, use printf()
to prompt user to type in something and then use scanf() to read it
in. It is best not to move the curer to a new line.
Then how that requirement that printf() should end with newline
comes to play?