tags:

views:

766

answers:

6

First up, I should let you know that I am learning C, so my apologies if this question seems stupid to a more advanced developer.

I know you can print with printf() and puts(). I can also see that printf() allows you to embed variables inside and do some stuff like formatting.

Is puts() merely a primitive version of printf(). Should it be used for every possible printf() without string interpolation?

Thanks

+4  A: 

puts() vs printf() - C/C++ Answers

Anthony Forloney
Interesting info on that page - ugly forum layout though. Thanks for your answer.
alex
Your welcome, and yeah the layout of the form is not that great looking, but I figured it had a few different responses to the question and wouldn't hurt.
Anthony Forloney
+3  A: 

Right, printf could be thought of as a more powerful version of puts. `printf' provides the ability to format variables for output using format specifiers such as %s, %d, %lf, etc...

Justin Ethier
+2  A: 

puts is simpler than printf but be aware that the former automatically appends a newline. If that's not what you want, you can fputs your string to stdout or use printf.

Kristo
+1 Thanks, the newline thing is good to know!
alex
A: 
int puts(const char *s);

puts() writes the string s and a trailing newline to stdout.

int printf(const char *format, ...);

The function printf() writes write output to stdout, under the control of a format string that specifies how subsequent arguments are converted for output.

I'll use this opportunity to ask you to read the documentation.

Mustapha Isyaku-Rabiu
A: 

In my experience, printf() hauls in more code than puts() regardless of the format string.

If I don't need the formatting, I don't use printf. However, fwrite to stdout works a lot faster than puts.

static const char my_text[] = "Using fwrite.\n";
fwrite(my_text, 1, sizeof(my_text) - sizeof('\0'), stdout);
Thomas Matthews
A: 

the printf() function is used to print both strings and variables to the screen while the puts() function only permits you to print a string only to your screen.

Wesley Nyandika