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

CProgrammingServer Side Programming

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 ‘\n’. 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! \n");
   printf("The value of a : %d",a);
   getchar();
   return 0;
}

Output

Hello world!
The value of a : 10
raja
Published on 08-Oct-2018 06:25:07

Advertisements