Home > Backend Development > C++ > 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
Release: 2023-08-27 13:57:02
forward
912 people have browsed it

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”);
Copy after login

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

fputs(string, stdout)
Copy after login

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;
}
Copy after login

Output

This is a demo.
No new Line. Welcome!
Copy after login

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;);
Copy after login

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;
}
Copy after login

Output

Hello world!
The value of a : 10
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template