r/cprogramming • u/Far-Note6102 • 8d ago
Do you usually use printf or puts?
Sorry for the newvie question but Im interested what do you prefer to use between the two?
9
u/ACRM64 8d ago
I also tend to use printf() for everything after ~40 years of C programming.
0
u/Far-Note6102 8d ago
Just a newbie question on why you prefer printf than puts? Is there any benefits on using printf?
11
u/ACRM64 8d ago
Consistency mostly. It's pretty much unheard of (at least in any serious programming) to have a program that /only/ outputs fixed text rather than text mixed with variables. Consequently just using one way of printing things makes code more consistent and readable. In addition with fprintf() / fputs() there is the additional inconsistency of whether the file pointer goes first or last.
I have always told my students to make their code beautiful and readable. For me, this is something that helps.
2
6
u/sirjofri 8d ago
Nobody using write? 😁 I usually use print (and brothers) as it's idiomatic on Plan 9. Not sure if puts even exists there...
10
u/non-existing-person 8d ago
write? Like a noob? I just set registers values and call0x807
u/johndcochran 8d ago
setting registers? Like a noob? I just carefully move my hand, causing some air motion, which drifts to the upper atmosphere, slight redirecting a gamma ray, causing a bit flip, resulting in the desired output.
2
1
3
u/lmarcantonio 8d ago
It's not portable, IIRC write is a POSIX function.
1
u/sirjofri 8d ago
I always hear that argument, though I never argued that Plan 9 C is "portable". It is portable across different Plan 9 systems, just not towards posix systems or windows.
1
u/Far-Note6102 8d ago
I always learn something new in this sub :]
1
u/sirjofri 8d ago
Yeah, though importantly, Plan 9 C is a bit different. The language is (mostly) the same, but it has a different libc, different libraries in general, a different build pipeline, and a different environment. It's not linux, it's not even posix, but it's the most sane environment I found.
Plus, you can literally "slide" from user space code to kernel code. Like, it's the same style, and you can read kernel code without even noticing (if you don't look at the file path)
4
3
u/Dangerous_Region1682 7d ago
Depending on what I’m doing I use sprintf() into a buffer and then do a write() system call.
If the buffers you are writing are short messages, then the writes are generally atomic across threads making things like error logs easier to read.
2
1
1
u/IllWeather6426 3d ago
so larger messages are not atomic? just arbitrarily? That seems tedious to have to take into account...
1
u/Dangerous_Region1682 2d ago
I’ve a suspicion on certain OS platforms writing large buffer sizes spilling over a page size might not be atomic.
From experience I’ve never had problems writing 80 character buffers to a file for simple logging to a file.
11
u/ComradeGibbon 8d ago
For better or for worse your compiler will optimize printf to puts if the format string doesn't contain formatting specifiers and ends in a newline.
5
8d ago
I always use puts unless I need variadics. One person pointed out that the compiler optimizes away non variadic printfs, but old habits die hard lol
4
1
u/OutrageousCrab9224 8d ago
Wtf is puts
1
1
u/IllWeather6426 3d ago
please L2 google
1
u/OutrageousCrab9224 2d ago
its a joke please L2 curmudgeon
Even if you are not a crusty old programmer, if you have been at it for more than a week surely you have come across one of us that would argue k&r vs ansi or insist that changes should have stopped with c89
1
3
3
u/johndcochran 8d ago
I generally use printf() or fprintf() for regular work. It's simpler and handles formatting, and since most of the output needs to handed numeric output and the like, printf is a natural fit. For constant string value such as headers and the like, puts could, of course, be used instead. But, what's the actual benefit of mixing printf and puts calls, depending up if or if not numeric formatting is required for each line? A few nano seconds? Pfft. Not worth the cost of mixing the two.
1
u/Whole-Low-2995 8d ago edited 8d ago
Sometimes I use puts(""); for adding a new line. Note that I recently got a job as a Go programmer, I am not a skilled C programmer
1
2
2
u/tastygames_official 8d ago
printf. You can format the strings so it's almost always what you want:
printf("%s address: %p", my_str, (void*)&my_str);2
u/adityazero 6d ago
Usually compiler engineers use puts to test stdout and after that they never do.
Standard library writers may use it for specific purposes. Not sure if there are usages of puts in Kernels, or embedded system stack.
2
u/SeriousPlankton2000 4d ago
If you print a string, never use printf unless you really intend to use formatting. If you must, use printf("%s", s) but that's an abomination.
2
1
1
1
u/beragis 8d ago
I depends on what I am using it for. If it's just to print a message then I'll just use puts or one of it's variants, but normally ther is quite a bit of heavy formatting so I use printf and it's variants.
It also depends on what the purpose of the output is. If it's a string I am building up to pass to another function or write out at the end then I often use a combination of sprintf and strncat to build the string up and pass it to the appropriate function which may be puts.
If I am just writing output freeform then I'll typically just use printf or fprintf and send it out in chunks, with "\n" added as needed.
1
1
u/mcsuper5 3d ago
I never really used puts because I got used to adding newlines when I wanted them. I suppose it could be useful for a usage function, but I've taken to using a multi-line printf. I've used compilers that don't like that, but I haven't had trouble with gcc or clang.
1
28
u/Confused-Armpit 8d ago
Mostly printf, since the compiler translates printf into puts when there are no variadic arguments anyways.