What is the difference between printf and puts in c




















Generally, C is a high level, general-purpose programming language. It is used to develop operating systems, embedded systems, compilers, databases and many other applications. Furthermore, most programming languages are based on this language.

Overall, printf and puts are two functions available in C language to display a string on the console. What is printf -Definition, Functionality 2. What is puts -Definition, Functionality 3. Difference Between printf and puts -Comparison of key differences. The syntax of printf function is as follows. Moreover, it is also possible to print the string as it is, without formatting it.

An example is as follows. Figure C program with printf. In line 7, the programmer directly encloses the string inside the printf function. Therefore, after displaying the Hello World, the cursor moves to the next line.

Line 8 displays the value of integer values a and b on the screen. Likewise, the programmer can print a formatted string. When comparing puts and printf , even though their memory consumption is almost the same, puts takes more time compared to printf. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is the difference between printf and puts in C? Ask Question. Asked 11 years, 8 months ago.

Active 1 year, 4 months ago. Viewed k times. Improve this question. RobertS supports Monica Cellio Just a note on using printf instead of puts: never, ever do a printf variable to print a string.

There's a security risk in using a variable format string: if the variable can be written by an attacker they can attack the program by using format strings.

Add a comment. Active Oldest Votes. Improve this answer. Michael Kristofik Michael Kristofik I think it's also important to mention the additional arguments printf take for adding additional variables into the outputted string. In what way would using printf be less efficient? At run time? At compile time? However, this should normally be irrelevant.

Further, a clever compiler could optimize this, and replace the printf with call to puts — leonbloy. When I've tested this, puts returns the number of characters in the string including the terminating character, whereas printf returns the number of characters printed without the terminating character.

Which is technically consistent with your definition, as the former is a non-negative integer, but I'm not sure whether that's what you meant to say. For example, the following code will be compiled to the assembly code I show next. Hannu Balk Hannu Balk 1 1 silver badge 4 4 bronze badges. And what about new line that puts places in stdout? It should have been printf "Hello world! Since it's an old message, I will edit it myself. How did you read the assembly code after compiling the C code?

KorayTugay : the -save-temps option for gcc does that — schaiba. You could also use a tool like gdb to disassemble a binary. I'll use this opportunity to ask you to read the documentation. Koray Tugay Thomas Matthews Thomas Matthews AntonyHatchkins It's typically not "a lot" faster.

That's pretty much the only real contributer to a performance difference. This answer is incorrect. If you want to print 1 less byte, just use 1.



0コメント

  • 1000 / 1000