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
C Language, printf, puts
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.
Here, the programmer can enclose the value inside double quotes.
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.
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
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.
The syntax for printf is,printf(str) while the syntax for puts is puts(str). This is another .
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.
Generally, the implementation of puts is simpler than printf.
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.