Most of these answers explain what %n does (which is to print nothing and to write the number of characters printed thus far to an int variable), but so far no one has really given an example of what use it has. Here is one:
int n;
printf("%s: %nFoo\n", "hello", &n);
printf("%*sBar\n", n, "");
will print:
hello: Foo
Bar
with Foo and Bar aligned. (It's trivial to do that without using %n for this particular example, and in general one always could break up that first printf call:
int n = printf("%s: ", "hello");
printf("Foo\n");
printf("%*sBar\n", n, "");
Whether the slightly added convenience is worth using something esoteric like %n (and possibly introducing errors) is open to debate.)
Most of these answers explain what %n does (which is to print nothing and to write the number of characters printed thus far to an int variable), but so far no one has really given an example of what use it has. Here is one:
int n;
printf("%s: %nFoo\n", "hello", &n);
printf("%*sBar\n", n, "");
will print:
hello: Foo
Bar
with Foo and Bar aligned. (It's trivial to do that without using %n for this particular example, and in general one always could break up that first printf call:
int n = printf("%s: ", "hello");
printf("Foo\n");
printf("%*sBar\n", n, "");
Whether the slightly added convenience is worth using something esoteric like %n (and possibly introducing errors) is open to debate.)
Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
#include <stdio.h>
int main()
{
int val;
printf("blah %n blah\n", &val);
printf("val = %d\n", val);
return 0;
}
The previous code prints:
blah blah
val = 5
why do we write "\n" inside of our printf?
printf("\n"); (C)
New to Programming [C]. What do the "/n" mean in the "printf" functions?
Is there a function to printf() n characters in a string?
How to use printf in C?
What is the printf() function in C?
How to format printf in C?
Videos
It will make no difference which one you chose if you're using a modern compiler[1]. Take for example the following C code.
#include <stdlib.h>
#include <stdio.h>
void foo(void) {
putchar('\n');
}
void bar(void) {
printf("\n");
}
When compiled with gcc -O1 (optimizations enabled), we get the following (identical) machine code in both foo and bar:
movl $10, %edi
popq %rbp
jmp _putchar ## TAILCALL
Both foo and bar end up calling putchar('\n'). In other words, modern C compilers are smart enough to optimize printf calls very efficiently. Just use whichever one you think is more clear and readable.
- I do not consider MS's
clto be a modern compiler.
Are there any best practices for using one over the other?
Let style drive the decision.
Since efficiency of execution is the same or nearly identical, use the style that best conveys the larger code's function.
If the function had lots of printf(), stay with printf("\n").
Likewise for putchar('\n') and puts("")
A good compiler will emit the same efficient code for both putchar('\n'); and puts(""); (when return value not used).
sorry, im new to programming and im just in the second lecture of cs50.
when write printf("smth\n"); in here, what role \n is playing?
Is there a way to actually print out "\n", as the actual characters, rather than just a new line. If you do:
printf("\n");
It just prints a new line, but is there a way to actually print "\n" so the user will see it?
Thank you.
I'm taking a class on Programming in C and I have to admit that it is way harder than expected. I'm just trying to learn the basics so I'll be sure to ask more questions in the future, but I really want to know what the purpose "/n" is exactly.