CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – What is the use of gets() and puts() library functions?

Standard C Library provides tons of functions which are very useful and saves a lot of our development time. Through this article we are going to discuss gets() & puts() library functions.

Before we start, I would like to mention one important note here which is related to gets() function. gets() function is not recommended to use and this function may be removed in future releases of C. Why? We will discuss this at end of the article. To understand this, first you need to understand these functions. Lets start;

C Library function – gets()

gets() library function is used to read the string from the standard input stream which is stdin. That means, it is useful to take inputs from the user. It read the input string from stdin, and store the string into an argument which we need to pass through it. Upon success, it returns the string it read. The syntax of the function looks like below;

char *gets(char *str);

Observe that, gets() function takes a single argument which is a pointer to a string. We need to allocate the memory to store the input string, and pass the address of it through it’s argument, str. How much memory we need to allocate? That depends on your program requirement. Usually it will be length of the maximum string value you expect and plus 1 for the string null character.

gets() function takes the input until it read a new line character or an end-of-file condition met. It will not store the new line character in the input string, but it terminates the string with null character. Hence we need to allocate the memory for the input string as size of the string and plus 1.

Upon success, this function returns the address of the input string what we passed through it’s argument. Otherwise, it returns the null pointer & set the error code in errno to indicate the error.

C Library function – puts()

puts() library function is used to display the string on standard output stream which is stdout. The syntax of the function like below;

int puts(const char *str);

We need to pass the address of the string through its argument, str. Notice that the argument is const, that means the value of the argument can not be changed inside the function. Upon success, it returns the non negative value; otherwise, it returns the error code.

gets() & puts() – Complete working code example

This is time to put all the things what we learned so far about gets() & puts() functions. Here is the complete working example;

#include <stdio.h>

void main()
{
    char str[255] = "\0";
    char *ret_str = 0;
    int ret_code = 0;
    
    puts("Enter input string: ");
    ret_str = gets(str);
    
    puts("Given string is: ");
    ret_code = puts(str);
    
    printf("Return string from gets() is : %s & Return code from puts() is : %d", ret_str, ret_code);
}

Why gets() function is not recommended to use?

I believe now you understand how the gets() function works. Notice that, it takes an argument to store the input string. And it read the input string until it read new line character or an end-of-file condition met. This way we could clearly see a buffer overflow. That means, if the size of the variable to hold the input string is less than the actual input string size, it will throw a buffer overflow error. This is the reason, it is NOT recommended to use gets() function. Try the above program with changing the str variable size from 255 to some very small number for example 2 and run the program. And enter little bit larger input string and observe the error the C compiler will throw.

// Malin

C Programming – What is the use of gets() and puts() library functions?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top