What is the Difference Between printf and puts

The main difference between printf and puts is that printf does not move the cursor to the new line by default while puts moves the cursor to the new line by default.

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.

Key Areas Covered

1. What is printf
     -Definition, Functionality
2. What is puts
    -Definition, Functionality
3. Difference Between printf and puts
    -Comparison of key differences

Key Terms

C Language, printf, puts

Difference Between printf and puts - Comparison Summary

What is printf

printf is a function that helps to give an output in a formatted way to a display device such as a computer screen. The syntax of printf function is as follows.

printf(“formatted string”, “list of variables”);

Moreover, it is also possible to print the string as it is, without formatting it. An example is as follows.

printf(“Learn programming”);

Here, the programmer can enclose the value inside double quotes.

Difference Between printf and puts

Figure 01: C program with printf

In line 7, the programmer directly encloses the string inside the printf function. The \n indicates the newline. 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. Here, the %d indicates to insert an integer. Therefore, the printf assigns the value of a to the first %d and assigns the value of b to the next %d. Likewise, the programmer can print a formatted string. Similarly, line 10 displays a character. Likewise, it is also possible to display strings, floating point values using printf function.

What is puts

puts is a function that helps to display a string to the standard output device such as a monitor. It appends a newline character to the output. 

Main Difference - printf vs puts

Figure 2: C program with puts

The word is a character type variable which is stored with the value “Hello World”. Finally, in line 6, the puts function displays the value stored in the variable word on the console. Moreover, puts moves the cursor to the new line. If the programmer does not require that, printf or fputs function.

Difference Between printf and puts

Definition

printf is a C function to print a formatted string to the standard output stream, which is the computer screen. In contrast, “puts” is a C library function that writes a string to stdout or standard output. Thus, this is the fundamental difference between printf and puts.

Syntax

The syntax for printf is,printf(str) while the syntax for puts is puts(str). This is another .

Next line

Moreover, the main difference between printf and puts is that printf does not move the cursor to the next line, but the programmer can use \n to bring the cursor to the next line. On the other hand, puts moves the cursor to the next line.

Implementation

Generally, the implementation of puts is simpler than printf.

Conclusion

In brief, both printf and puts helps to display a string or a set of characters on to the standard output which is usually the computer screen.  The main difference between printf and puts is that printf does not move the cursor to the new line by default while puts moves the cursor to the new line by default.

References:

1.“C Printf and Scanf Functions with Example.” fresh2refresh.Com, Available here.
2.“Puts() Function in C.” fresh2refresh.Com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply