Puts () and Printf (); when and which one to use?

5

Data entry requests are usually preceded by a text that indicates what you want to receive and usually do not have data to format; my question is this:

  If variable data is not being displayed in the request text, why do books and teachers always use printf () ?; does not the put () function serve to display simple 'text'?

** First time posting, the first of many!

    
asked by anonymous 18.08.2018 / 01:15

2 answers

3

The two functions are equal, but the puts() function adds a line break, equal to printf("\n")

But when using puts() we have to be aware that it just prints the string, we do not have control of printf() where we can print something like printf("int: %d, float: %f", x, y)

#include <stdio.h>
main() {
    printf("Hello world!");
    return 0;
}

In this Hello World example the compiler converts printf() to puts()

push rbp
mov rbp,rsp
mov edi,str.Helloworld!
call dword imp.puts
mov eax,0x0
pop rbp
ret

To conclude, printf() is a little slower because you need to format the variables and concatenate them to a string to be printed. But that difference is almost irrelevant.

    
18.08.2018 / 01:21
3

The basic difference is that printf() is formatted and has more control how to print the desired data, it is a complete printing solution. puts() is a very simple solution for playing characters on the console, you have to deliver exactly what he wants, he does not manipulate anything, so he can not send a number or a content with a text and wait for something visible as expected by a human. The only thing it does to more than printf() is to place a line break automatically.

I've already answered more about it in What's the difference between puts () and fputs ()? .

    
18.08.2018 / 01:23