puts() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The puts function is handy in the C programming language to print strings. This function writes strings or lines to stdout, i.e., the output stream. The passed string to the puts function in C is printed along with a newline, and an integer value is returned, and this return value is dependent on the success of writing the string passed.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of puts() Function in C

The syntax of the puts() function in C is simple. The function declaration is given below:

Where the string is the string you want to pass to stdout, this 'const' parameter helps guard against accidentally modifying the char array.

Parameters of puts() Function in C

The only parameter required to use the puts function in C is the string you want to print to the screen.

Parameters:

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Return Value of puts() Function in C

Return type: int

The puts() function in C returns a non-negative integer value that denotes successful execution. If there is any error in the execution of the puts() function, it returns EOF error.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example

Let's now look at a very simple example of the puts() function in C:

Output of the above code:

As we can see in this example, the puts() function has printed the input string to stdout.

The puts() function is preferred for printing strings to stdout because it is less expensive, i.e., implementing the puts function in C is simpler than that of printf(). If your input string contains formatting characters such as %, the printf() function will yield unexpected results.

More Examples

Let's look at a couple more examples of the puts() function in C.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Printing Multiple Strings Using puts() Function in C

Output:

The natural behavior of the puts() function is always to print the string in a new line. It essentially appends the new line character - \n at the end of every string while writing it to stdout.

So in this function, we initialized two strings - string1 and string2. When we used the puts() function, a newline character was appended at the end of string1, making string2 print in the next line. If there were another string, it would be printed in the line after string2.

Showcasing the Return Value of the puts() Function in C:

Output:

The value that is returned from the puts() function in C is the length of the string. If there is any error, EOF will be returned.

A Similar Function: fputs()

In the C programming language, there is a function similar to puts(), which is - fputs(). The difference between the two functions is that the fputs() function is used to write a stream or a file. Adding to that, the fputs() function does not add a new line character at the end of the stream or file like puts().

Conclusion

  • The puts() function in C is used to print ‘strings’ specifically.
  • The syntax of the puts() function in C is:
    • int puts(const char* string)
  • puts() function takes as parameters just the string required to be printed to stdout.
  • A non-negative integer value is returned by the function, which is usually the length of the string.
  • After the end of every string, the puts() function adds the new line character, thereby printing every string into a new line.