r/C_Programming • u/Adept-Energy179 • Jul 25 '26
does anyone else make functions like this
void newline(int times) {
for (int i; i < times; i++) {
printf(“\n”);
}
}
this is so you can do
newline(5);
instead of
printf(“\n\n\n\n\n”);
i feel like i shouldn’t ngl
mb if the syntax is wrong im not a pro
0
Upvotes
5
u/mc_pm Jul 25 '26
If you happen to need that functionality often, then it's fine. Congratulations, you've re-used code :)
20
u/mykesx Jul 25 '26 edited Jul 25 '26
It's fine, but you could just write a void repeat(char c, int times) function that is more versatile.
Not only is a newlines function useful, but also a stars, equal signs, dashes, spaces, zeros...
You could #define newlines(n) repeat('\n', n)
Also, use putc() or a variant over printf(), it will be much more optimal.
6
u/mrtlo Jul 25 '26
Could be worse, but I'd probably use a static string and fwrite to stdout
1
u/sexytokeburgerz Jul 25 '26
Learn markdown
1
u/Confused-Armpit Jul 25 '26
Yeah, probably shouldn't do that.
9
u/PseudoFrequency Jul 25 '26
Not a bad idea but it will be slower than one printf call in case you care.
Better implementation:
Note, there is nothing wrong with variable size arrays on the stack despite what some people say.
This isn't as good as you can get, but is a bit better. If you aren't formatting, fputs is better than printf.
2
u/WeAllWantToBeHappy Jul 25 '26
2
u/Independent_Art_6676 Jul 26 '26
you can probably just move the cursor directly faster with a library call. But this is fine, yes most of us ended up with some console assist functions during school esp older folks where 100% of their classroom work was console programs. I think I did maybe 3 GUI programs in my whole college career.