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++ Structures

In the C/C++ programming language, the structure is user-defined data types that allow us to store the combination of different data types together. All the …
Read More

C++ Static Keyword

Static s a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a …
Read More

C++ Templates

Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions). A single class or function is …
Read More

C++ Friend Function

There are private and protected members in a class that is inaccessible from outside of the class, constituting one of the concepts of object-oriented programming …
Read More

this Pointer in C++

In C++, this is a keyword that refers to the current instance of a class and this pointer holds the address or points to the …
Read More

Inheritance in C++

Inheritance is one of the most important concepts of object-oriented programming. In C++, inheritance is the process of deriving the properties and behaviors of one …
Read More