EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C puts() Function

Home » Software Development » Software Development Tutorials » C Programming Tutorial » C puts() Function

C-puts-()-Function

Introduction to C puts() Function

In any programming language, trying the output on the screen is a vital part. In c this is achieved with the help of puts function. Puts is an inbuilt function that writes a line of output to the screen. It returns the number of characters that are written to the console plus one as it prints a new line along with the output text thereby moving the cursor to the new line. Its return type is int. If the execution is successful, the non-negative value is returned. If the execution has an error, EOF is returned.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The syntax of the puts function is as follows

int puts(const char *str)

The parameter string is the text that should be printed on the console.

Example of puts

Below the given codes and outputs explain the example in puts:

Code:

#include <stdio.h>
#include <string.h>
void main () {
char op1[30];
char op2[45];
strcpy(op1, "this is first line of puts example");
strcpy(op2, "this is second line of puts example");
puts(op1);
puts(op2);
}

Output:

example

gets Function in C

The gets function is a built-in function that is used to read the characters from the console and store that in a string. It reads the characters till a new line is found or EOF is reached, whichever comes first.

Syntax:

char *gets(char *str)

str is the pointer in which the character read is stored.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Code:

#include<stdio.h>
void main ()
{
char test[30];
printf("Enter your name");
gets(test);
printf("You entered %s",test);
printf("\n the above is an example of gets function");
}

Output:

get function in c

Example of puts and gets

Below the given codes and outputs explain the example in puts and gets:

Code:

#include <stdio.h>
#include <string.h>
int main()
{
char name[50];
printf("Enter your name ");
gets(name);
int age[50];
printf("Enter your age ");
gets(age);
char address[50];
printf("Enter your address ");
gets(address);
int pincode[50];
printf("Enter your pincode ");
gets(pincode);
printf("Entered Name is: ");
puts(name);
printf("Entered age is: ");
puts(age);
printf("Entered address is: ");
puts(address);
printf("Entered pincode is: ");
puts(pincode);
getch();
return 0;
}

Output:

puts and gets

Functions in puts and gets

The following points explain the function sin puts and gets with examples:

1. Fgetc()

This is an inbuilt file handling function that reads a file. It reads the file single character at a time until the end of the file. The function returns a non-negative value for successful execution.

Syntax:

Int fgetc(FILE *fp)

The fp is the pointer to the file location.

Code:

# include <stdio.h>
int main( )
{
FILE *testfile ;
char c ;
printf( "demo of fgetc" ) ;
testfile = fopen ( "c:\test.txt", "r" ) ;
if ( testfile == NULL )
{
printf ( "\nCould not open file test.txt" ) ;
return 1;
}
printf( "file is present and is being read" ) ;
while ( 1 )
{
c = fgetc ( testfile ) ;
if ( c == EOF )
break ;
printf ( "%c", c ) ;
}
printf("Closing the file test.txt") ;
fclose ( testfile ) ;
return 0;
}

Output:

C puts() Function - 4

2. Fputc()

This is an inbuilt file handling function that writes text to a file. It writes one character at a time. The function returns a non-negative value for successful execution.

Syntax:

int fputc(int char, FILE *fp)

Where char is the character to be written and fp is the location of the file.

Code:

#include <stdio.h>
int main()
{
char ch;
FILE *source;
FILE *destination;
if (source = fopen("c:\source.txt", "r"))
{
ch = getc(source);
destination = fopen("c:\dest.txt", "w+")
while (ch != EOF)
{
fputc(ch, destination);
ch = getc(source);
}
fclose(source);
fclose(destination);
return 0;
}
return 1;
}

Output:

Fputc()

3. Fgets()

This function is similar to fgetc() except that this reads a file line by line.

Syntax:

char *fgets(char *string, int n, FILE *fp)

  • fp is the location from which the text must be read
  • *string is the location in which the read characters are stored

Code:

# include <stdio.h>
int main( )
{
FILE *sourcefile ;
char newdata[50] ;
printf( "Opening  file test.txt " ) ;
sourcefile = fopen( "c:\test.c", "r" ) ;
if ( sourcefile== NULL )
{
printf( "Could not open file test.txt" ) ;
return 1;
}
printf( "Reading  test.txt" ) ;
while( fgets ( newdata, 50, sourcefile ) != NULL )
printf("\nprinting the file content got using fgets()")
printf( "%s" , newdata ) ;
printf("Closing the file test.txt") ;
fclose(sourcefile) ;
return 0;
}

Output:

Fgets()

4. Fputs()

This function is similar to fputc() except that this writes to a file line by line.

Syntax:

int fputs(const char *str, FILE *stream)

  • *stream file to which the string must be written
  • Str is the string that is to be written

Code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
char input[50];
printf("\n user pls enter input");
scanf("%s",input);
FILE *destfile;
destfile = fopen("D:\test.txt", "w");
if(destfile == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("\n writing the received input to file");
while( gets(input) != NULL )
{
fputs(input, destfile);
}
printf("\n file is written, open and see");
fclose(destfile);
return 0;
}

Output:

C puts() Function - 7

C puts() Function - 8

Difference Between

  • puts() and fputs(): Puts() is used to write the text to the console, whereas fputs() is used to write the text to a file. Puts() will convert a null character in the input to a new line whereas fputs() will not handle the null character and stops execution.
  • gets() and fgets(): Gets reads the input from the console or stdin whereas fgets reads the input from a file. Fgets are safe to use as in case of overflowing it will throw an error. Fgets stops when a new line is encountered. Fgets doesn’t check for out of bound exception. It also reads until the new line is encountered.

Code:

#include<stdio.h>
int main()
{
char author[20];
printf("Enter your favorite author\n");
fgets(author,10,stdin);
printf("My favorite author is %s.\n",author);
FILE *destfile;
destfile = fopen("D:\existing.txt", "w+");
fputs("first line to be entered", destfile);
fputs("second line to be added", destfile);
fputs("thirdine to be added", destfile);
fclose(destfile);
return(0);
}

Output:

C puts() Function - 9

Conclusion – C puts() Function

Thus, the article covered in detail about puts functions along with its syntax and sample examples. It also covered in detail functions similar to it such as fputc() and fputs(). Also, functions like gets(), fgets() and fgetc() are discussed with appropriate examples. The differences between functions are also explained. To learn in-depth about the functions it is advisable to create sample programs and have fun working around them.

Recommended Articles

This is a guide to C puts() Function. Here we discuss detail about puts functions along with its syntax and sample examples, and function about fputc() and fputs(). You can also go through our other related articles to learn more –

  1. Arithmetic Operators in C
  2. Single Inheritance in C++
  3. strcmp() in C++
  4. if else Statement in C++

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More