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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …