r/cprogramming • u/Far-Note6102 • 15d 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?
25
Upvotes
r/cprogramming • u/Far-Note6102 • 15d ago
Sorry for the newvie question but Im interested what do you prefer to use between the two?
28
u/Confused-Armpit 15d ago
Mostly printf, since the compiler translates printf into puts when there are no variadic arguments anyways.
9
u/ACRM64 15d ago
I also tend to use printf() for everything after ~40 years of C programming.
6
u/sirjofri 15d ago
Nobody using
write? 😁 I usually useprint(and brothers) as it's idiomatic on Plan 9. Not sure ifputseven exists there...4
u/Key_River7180 15d ago
Puts, I don't have to add \n and no %s for user input
3
u/Dangerous_Region1682 15d 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.
11
u/ComradeGibbon 15d 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
u/[deleted] 15d 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
u/70Shadow07 15d ago
puts if its just a string, printf if its a string with format specifiers.
1
u/OutrageousCrab9224 15d ago
Wtf is puts
1
u/Puzzled-Extent7817 13d ago
Strings aren't real in C
3
u/Total-Box-5169 15d ago
puts because is shorter and when the bonus end line is desired.
3
u/johndcochran 15d 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 15d ago edited 15d 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 programmer2
u/vena_contracta 15d ago
Printf() because I am lazy.
2
u/tastygames_official 15d ago
printf. You can format the strings so it's almost always what you want:
2
u/adityazero 14d 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 11d 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
u/SheikHunt 15d ago
Neither, I do putc() in a loop like a real programmer /s
1
u/suboculus 15d ago
i just use pp
1
u/aiwprton805 15d ago
puts
1
u/beragis 15d 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
u/21Ali-ANinja69 15d ago
Printf, except if I need to print utf8, then fwrite
1
u/NAND404 11d ago
Im newbier than you... Learnt smthing new :3
1
u/mcsuper5 10d 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
u/rip_void_0 9d ago
write function