C gets() and puts() functions in String2 min read

In this tutorial, you will learn about the two functions that are used in String, gets() and puts() function in C. Learn more on String.


How o read and write a line of text in C

gets() and puts() function are used for reading and writing a line of text or String in C. These both functions are included in a header file stdio.h.

gets() function in C

This gets() when used in a program allowed the user to enter the characters or line of text until the user encountered the new line or you can say until the user presses the enter key. It also allows the space-separated line of text or String.

It is used in the program in the following manner:

C Program for gets() function.

Output:

Although, gets() function is risky to use, the program might throw a warning when you used gets() because it does not check the array bound and keep on taking character until the new line is entered. This can be avoided by the use of fgets() function which only reads the character to the limit stated in the program.

C Program for fgets() function.

fgets() function takes three argument:

  • Name of the String,
  • The size that is the number of character to read,
  • stdin which is the standard input to take input from the keyboard.

Output:


puts() function in C

puts() function works in a manner similar to printf() function. It is used to print or display the string in C and moves the cursor to the first position.

C Program for puts() function.

Output:

C Program for fputs() function.

fputs() takes two argument:

  • Name of the String itself,
  • stdout which is the standard output to display on the screen.

Output:


MORE

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More