Tutorials  /  Linux Basics

Mastering the puts() function in C/C++

Ccentron Redaktion · January 2024 ·4 min read ·Linux Basics, Tutorial

Introduction

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

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.

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.

The puts() function declaration is given below.

Code
int puts(const char* str);

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

Let us look at a small example.

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

Output:

Code
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++.

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

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.

Code
#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:

Code
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.

puts() return value

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

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

Code
#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:

Code
The puts() function
Returned Value Val = 0

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.

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

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Conclusion

So that's it for today. Hope you had a satisfying learning experience - Mastering the puts() function in C/C++: a Comprehensive Guide

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren