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

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More