c++ puts函数_在C / C ++中使用puts()函数

c++ puts函数

介绍 (Introduction)

Hello reader! Today in this tutorial we are going to discuss about the vastly used puts() function in for both C and C++ programming languages.

读者好! 今天,在本教程中,我们将讨论C和C ++编程语言中广泛使用的puts()函数

Even though the printf() and cout functions in both C and C++ are prominent for printing variables, numbers, lines, etc. they ultimately lack behind while printing strings especially printf(). The puts() function comes handy in that case.

即使CC ++中的printf()cout函数在打印变量,数字,行等方面都很突出,但是在打印字符串(尤其是printf()时,它们最终会落后。 在这种情况下, puts()函数很方便。

C / C ++中的puts()函数 (The puts() function in C/C++)

The puts() function in C/C++ is used to write a line or string to the output(stdout) stream. It prints the passed string with a newline and returns an integer value. The return value depends on the success of the writing procedure.

C / C ++中puts()函数用于将一行或字符串写入output( stdout )流。 它用换行符打印传递的字符串,并返回一个整数值。 返回值取决于写入过程的成功。

The puts() function declaration is given below.

puts()函数声明如下。


int puts(const char* str);

Here, str is the constant string that is to be printed.

此处, str是要打印的常量字符串。

Let us look at a small example.

让我们看一个小例子。


#include<stdio.h>
int main()
{
	//string initialisation
    char Mystr[] = "C and C++";
    
    puts(Mystr); //writing the string to stdout
    
    return 0;
}

Output:

输出:


C and C++

As you can see, our string Mystr has been successfully printed to the stdout. The below-given code snippet also yields the same output in C++.

如您所见,我们的字符串Mystr已成功打印到stdout 。 下面给出的代码段在C ++中也产生相同的输出。


#include<iostream>
using namespace std;
int main()
{
	//string initialisation
    char Mystr[] = "C and C++";
    
    puts(Mystr); //writing the string to stdout
    
    return 0;
}

在C / C ++中使用puts()函数 (Using the puts() function in C/C++)

We have mentioned earlier, that the puts() function appends a newline character at the end while writing a string/line.

前面我们已经提到, puts()函数在编写字符串/行时在末尾添加换行符。


#include<stdio.h>
int main()
{
	//string initialisation
    char Mystr1[10] = "Python";
    char Mystr2[10] = "Kotlin";
    
    puts(Mystr1);
    puts(Mystr2); //not specifically adding a newline
    
    return 0;
}

Output:

输出:


Python
Kotlin

Here, we have initialized two strings Mystr1 and Mystr2. While printing these strings using the puts() method in either C or C++, we do not need to particularly add a "\n"(newline) as the function already appends one.

在这里,我们初始化了两个字符串Mystr1Mystr2 。 在CC ++中使用puts()方法打印这些字符串时,由于该函数已经附加了一个"\n" ,所以不需要特别添加"\n" (换行符)。

puts()返回值 (puts() return value)

The puts() function returns an non-negative integer number for successful execution. Otherwise returns EOF for any error.

puts()函数返回一个非负整数以成功执行。 否则,返回EOF表示任何错误。

The below-given example illustrates the return value for the puts() function.

以下示例说明了puts()函数的返回值。


#include<stdio.h>
int main()
{
	//string initialisation
    char Mystr[] = "The puts() function";
    
    int val = puts(Mystr);
    printf("Returned Value Val = %d", val);
    
    return 0;
}

Output:

输出:


The puts() function
Returned Value Val = 0

C / C ++中的puts()VS fputs()函数 (puts() VS fputs() functions in C/C++)

As we have learned earlier, the puts() function writes a line or string to the stdout stream. Whereas, the fputs() function is used to write to any stream or a file. Hence, the biggest difference between the two functions is the fact that with fputs(), the user can specify the stream to which he/she wants to write.

如前所述, puts()函数将一行或字符串写入stdout流。 而fputs()函数用于写入任何文件 。 因此,这两个函数之间的最大区别是事实,即用户可以使用fputs()来指定他/她要写入的流。

Moreover, the fputs() function doesn’t append a newline character("\n")at the end of the passed string/line.

此外, fputs()函数不会在传递的字符串/行的末尾附加换行符( "\n" )。

结论 (Conclusion)

So thats’s it for today. Hope you had a satisfying learning experience.

就是今天。 希望您有令人满意的学习经验。

For any further questions related to the puts() function in C/C++, feel free to use the comments below.

对于与C / C ++中的puts()函数有关的任何其他问题,请随时使用以下注释。

参考资料 (References)

翻译自: https://www.journaldev.com/40899/puts-function-c-plus-plus

c++ puts函数

  • 16
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值