puts() vs printf() for printing a string in C language


The function puts() and printf() are declared in stdio.h header file and are used to send the text to the output stream. Both have different usages and syntax.

puts()

The function puts() is used to print the string on the output stream with the additional new line character ‘
’. It moves the cursor to the next line. Implementation of puts() is easier than printf().

Here is the syntax of puts() in C language,

puts(“string”);

If you do not want the cursor to be moved to the new line, use the following syntax.

fputs(string, stdout)

Here is an example of puts() in C language,

Example

 Live Demo

#include<stdio.h>
int main() {
   puts("This is a demo.");
   fputs("No new Line.", stdout);
   puts(" Welcome!");
   getchar();
   return 0;
}

Output

This is a demo.
No new Line. Welcome!

printf()

The function printf() is used to print the text long with the values of variables. The implementation of printf() is complex, that is why it is expensive than puts().

Here is the syntax of printf() in C language,

printf(“string”);

Here is an example of printf() in C language,

Example

 Live Demo

#include<stdio.h>
int main() {
   int a = 10;
   printf("Hello world! 
");    printf("The value of a : %d",a);    getchar();    return 0; }

Output

Hello world!
The value of a : 10

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements