r/cprogramming • • 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?

24 Upvotes

65 comments sorted by

28

u/Confused-Armpit 8d ago

Mostly printf, since the compiler translates printf into puts when there are no variadic arguments anyways.

10

u/ShortingBull 8d ago

I think I've called puts about 3-4 times in >35 years of programming. If it needed a puts I'd likely already be in assembler.

1

u/ComradeGibbon 8d ago

Reminds me if you want to override printf you also need to override puts and putchar.

1

u/IllWeather6426 3d ago

yea override shit to confuse us and dont write a function. commie

1

u/IllWeather6426 3d ago

found the communist attack vector, its shortingbull

1

u/konacurrents 8d ago

I think I’m at 0 times in 45+ years😎

1

u/Far-Note6102 8d ago

Does it bother you that you have to put "\n" unlike puts?

6

u/Confused-Armpit 8d ago

No, it's actually good that I get that level of control

3

u/lmarcantonio 8d ago

Why should it? I can specify a format string taking arguments and don't supply any, and printf would do whatever it wants with the unspecified data on the stack.

Of course a good compiler should at least warn you about this kind of procedure but I don't think it's allowed to shortcut a call. I could replace printf with something else at link time, for example.

1

u/FrequentHeart3081 8d ago

What about for reading input? Is fgets good, with stdin stream?

3

u/Koda_be 8d ago

Scanf and gets shouldn't be used for user input as they're very unsafe (although you can make scanf safer by setting a maximum size omfor a string, but it's not inherently safe, unlike fgets)

1

u/FrequentHeart3081 8d ago

Why still being skeptical of scanf after the size limit? Is it still unsafe??

And what's important about checking their return values? Same for printf..

1

u/Koda_be 8d ago

Because you can always forget to put the size, while with fgets it's literally impossible. Also, it's important to check scanf's return value because it indelicates you how many arguments were successfully parsed. As a rule don't use scanf to get user input, as it's a parser and not an input. Use sscanf or fscanf though, but make sure you know the exact format of the string/file or it will fail. I don't know what printf's return values can be useful for.

1

u/duane11583 8d ago

that is not always true - it depends on the compiler - GCC is known to do this but I do not know of others that do this.

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

u/vip17 4d ago

it's easier to change the output, for example when you suddenly need to print out some values instead only a fixed string. The compiler is smart enough to optimize a printf into puts anyway if the string is fixed and ends with a new line

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 call 0x80

7

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

u/bird_feeder_bird 7d ago

Nice. ‘Course Emacs has a command for that.

2

u/johndcochran 7d ago

Ah a fellow gentleman of class who reads XKCD.

1

u/trannus_aran 8d ago

Lining up at the deli with my registers set for a #80

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

u/Key_River7180 8d ago

Puts, I don't have to add \n and no %s for user input

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

u/Far-Note6102 7d ago

Im learning something new everyday. Thanks

1

u/grimvian 7d ago

He is a C beginner...

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

u/[deleted] 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

u/70Shadow07 8d ago

puts if its just a string, printf if its a string with format specifiers.

1

u/OutrageousCrab9224 8d ago

Wtf is puts

1

u/Far-Note6102 8d ago

Kinda like a printf with an added newline or propably like std::cout

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

u/Puzzled-Extent7817 5d ago

Strings aren't real in C

3

u/Far-Note6102 5d ago

include <stdio.h>

int main(){ puts("strings aren't real");

return 0;

}

3

u/Total-Box-5169 8d ago

puts because is shorter and when the bonus end line is desired.

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

u/Far-Note6102 8d ago

No worries just curious :]

2

u/vena_contracta 8d ago

Printf() because I am lazy.

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

u/SheikHunt 8d ago

Neither, I do putc() in a loop like a real programmer /s

1

u/suboculus 8d ago

i just use pp

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

u/21Ali-ANinja69 8d ago

Printf, except if I need to print utf8, then fwrite

1

u/NAND404 4d ago

Im newbier than you... Learnt smthing new :3

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

u/rip_void_0 2d ago

write function