Home  >  Article  >  Backend Development  >  In C language, both puts() and printf() can be used to print strings. The puts() function automatically adds a newline character to the end of the string and prints the string to standard output. The printf() function can format the output string as needed, and can insert variables or other characters into the string. The main difference between the two is that puts() can only print strings, while printf() can print various types of data.

In C language, both puts() and printf() can be used to print strings. The puts() function automatically adds a newline character to the end of the string and prints the string to standard output. The printf() function can format the output string as needed, and can insert variables or other characters into the string. The main difference between the two is that puts() can only print strings, while printf() can print various types of data.

WBOY
WBOYforward
2023-08-27 13:57:02858browse

In C language, both puts() and printf() can be used to print strings. The puts() function automatically adds a newline character to the end of the string and prints the string to standard output. The printf() function can format the output string as needed, and can insert variables or other characters into the string. The main difference between the two is that puts() can only print strings, while printf() can print various types of data.

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

puts()

The function puts() is used to print a string on the output stream with the newline character '

' appended. It moves the cursor to the next line. Puts() is easier to implement than printf().

The following is the syntax of puts() in C language,

puts(“string”);

If you do not want the cursor to move to a new line, please use the following syntax.

fputs(string, stdout)

This is an example of put() in C language,

Example

Live demonstration

#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 long text with variable values. The implementation of printf() is more complex, which is why it is more expensive than puts().

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

printf(&ldquo;string&rdquo;);

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

Example

Live demonstration

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

Output

Hello world!
The value of a : 10

The above is the detailed content of In C language, both puts() and printf() can be used to print strings. The puts() function automatically adds a newline character to the end of the string and prints the string to standard output. The printf() function can format the output string as needed, and can insert variables or other characters into the string. The main difference between the two is that puts() can only print strings, while printf() can print various types of data.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete