C Programming Tutorial

C Programming Tutorial

gets() and puts() functions in C Programming

Hello Everyone, In this article, we will cover the concept of gets() and puts() functions in C Programming. These functions are used for input and output operations, making it easy to read strings from the user and display them on the screen.

In this chapter we will cover how they work, provide examples, and highlight important considerations when using them in your programs. Whether you are new to C or have some experience, this guide will help you to understand basics of Strings in C effectively with examples. Let’s get started.

gets() and puts() are functions in the C programming language used for reading input and displaying output, respectively. However, it’s important to note that using gets() is considered unsafe due to its potential to cause buffer overflow. Instead, use fgets() for safer input handling. Fgets() ensures that no more characters are read than the set maximum. In this explanation, I’ll use fgets() and puts() to demonstrate the functionality.

1. fgets(): This function is used to read a string from the standard input (stdin). It reads up to n-1 characters and appends a null terminator (‘\0’) at the end of the string. The function has the following syntax:

    char *fgets(char *str, int n, FILE *stream);
    • str: A pointer to the buffer that will store the string.
    • n: The maximum number of characters to read, including the null terminator.
    • stream: The input stream from which to read the string (usually stdin for standard input).

    2. puts(): This function is used to display a string to the standard output (stdout) followed by a newline character. The syntax is as follows:

      int puts(const char *str);
      • str: A pointer to the null-terminated string to be displayed.

      Example code:

      #include <stdio.h>
      int main() {
          char input[100];
      
          printf("Please enter your full name: ");
          fgets(input, sizeof(input), stdin);
      
          printf("Your full name is: ");
          puts(input);
      
          return 0;
      }
      

      Output:

      Please enter your full name: John Doe
      Your full name is: John Doe

      Real-life example:

      Imagine you’re developing a program that calculates the area of a rectangle. You can use fgets() to read the user’s input for length and width, and puts() to display the result.

      #include <stdio.h>
      int main() {
          char length_str[20], width_str[20];
          float length, width, area;
      
          printf("Enter the length of the rectangle: ");
          fgets(length_str, sizeof(length_str), stdin);
          sscanf(length_str, "%f", &length);
      
          printf("Enter the width of the rectangle: ");
          fgets(width_str, sizeof(width_str), stdin);
          sscanf(width_str, "%f", &width);
      
          area = length * width;
      
          printf("The area of the rectangle is: ");
          printf("%.2f\\n", area);
      
          return 0;
      }
      

      Output:

      Enter the length of the rectangle: 5
      Enter the width of the rectangle: 10
      The area of the rectangle is: 50.00
      

      As mentioned earlier, it’s important to note that using gets() is considered unsafe due to its potential to cause buffer overflow. Instead, use fgets() for safer input handling. In this explanation, I’ll provide more examples using fgets() and puts().

      Example 1: Reversing a string

      This program reads a string from the user and reverses it using fgets() and puts().

      #include <stdio.h>
      #include <string.h>
      void reverse_string(char *str);
      
      int main() {
          char input[100];
      
          printf("Enter a string to reverse: ");
          fgets(input, sizeof(input), stdin);
      
          // Remove newline character if present
          input[strcspn(input, "\\n")] = '\\0';
      
          reverse_string(input);
      
          printf("Reversed string: ");
          puts(input);
      
          return 0;
      }
      
      void reverse_string(char *str) {
          int length = strlen(str);
          for (int i = 0; i < length / 2; i++) {
              char temp = str[i];
              str[i] = str[length - i - 1];
              str[length - i - 1] = temp;
          }
      }
      

      Output:

      Enter a string to reverse: Hello, World!
      Reversed string: !dlroW ,olleH

      Example 2: Reading and displaying multiple lines

      This program reads multiple lines from the user and displays them using fgets() and puts().

      #include <stdio.h>
      int main() {
          char input[10][100];
          int num_lines;
      
          printf("Enter the number of lines: ");
          scanf("%d", &num_lines);
          getchar(); // To consume newline character after scanf
      
          for (int i = 0; i < num_lines; i++) {
              printf("Enter line %d: ", i + 1);
              fgets(input[i], sizeof(input[i]), stdin);
          }
      
          printf("\\nYou entered:\\n");
          for (int i = 0; i < num_lines; i++) {
              puts(input[i]);
          }
      
          return 0;
      }
      

      Output:

      Enter the number of lines: 3
      Enter line 1: This is line 1.
      Enter line 2: This is line 2.
      Enter line 3: This is line 3.
      
      You entered:
      This is line 1.
      This is line 2.
      This is line 3.
      
      Share This Article