Using The Puts() Function In C/C++ - DigitalOcean
Maybe your like
- Blog
- Docs
- Get Support
- Contact Sales
- Tutorials
- Questions
- Product Docs
- Search Community
Report this
What is the reason for this report?This undefined is spamThis undefined is offensiveThis undefined is off-topicThis undefined is otherSubmitTable of contents
- Introduction
- The puts function in CC
- Using the puts function in CC
- puts return value
- puts VS fputs functions in CC
- Conclusion
- References
- Tutorials
- C++
- Using the puts() function in C/C++
By Sneh
Table of contentsPopular topics 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.
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.
int puts(const char* str);Here, str is the constant string that is to be printed.
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++.
#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.
#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 KotlinHere, 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.
#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 = 0puts() 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.
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.
References
- C++ puts() - C++ References,
- C++ Tutorial,
- fgets() and gets() in C Programming,
- What is the difference between printf() and puts() in C? - Stack Overflow Question.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Learn more about our products
About the author
Still looking for an answer?
Ask a questionSearch for more helpWas this helpful?YesNoComments(1)Follow-up questions(0)What type of arguments can be used in puts() function in C?
- Snaf
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.Deploy on DigitalOcean
Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.Sign upPopular Topics
- AI/ML
- Ubuntu
- Linux Basics
- JavaScript
- Python
- MySQL
- Docker
- Kubernetes
- All tutorials
- Talk to an expert
Featured tutorials
- SOLID Design Principles Explained: Building Better Software Architecture
- How To Remove Docker Images, Containers, and Volumes
- How to Create a MySQL User and Grant Privileges (Step-by-Step)
- All tutorials
- All topic tags
Join the Tech Talk
Success! Thank you! Please check your email for further details.Please complete your information!
Become a contributor for community
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Sign Up
DigitalOcean Documentation
Full documentation for every DigitalOcean product.
Learn more
Resources for startups and AI-native businesses
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Learn more
Get our newsletter
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
SubmitSubmitNew accounts only. By submitting your email you agree to our Privacy Policy
The developer cloud
Scale up as you grow — whether you're running one virtual machine or ten thousand.
View all productsGet started for free
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
Get started*This promotional offer applies to new accounts only.
© 2026 DigitalOcean, LLC.Sitemap.Tag » What Is Puts In C
-
What Is Puts In C?
-
C Library Function - Puts() - Tutorialspoint
-
C Gets() And Puts() Functions - Javatpoint
-
Puts Function In C - Linux Hint
-
Puts() Function In C | C File Handling - Fresh2Refresh
-
C Language: Puts Function (Write String) - TechOnTheNet
-
Puts() — Write A String - IBM
-
Puts() Library Function With Examples - CodeSansar
-
Puts() Vs Printf() For Printing A String - GeeksforGeeks
-
Learn The Examples Of C Puts() Function - EduCBA
-
C Gets() & Puts() - W3schools.blog
-
[C언어/C++] Gets, Puts 문자열 입출력 함수에 대해서. - 개발자 지망생
-
C++ Puts() - C++ Standard Library - Programiz
-
Puts() Function In C Language With Example