0
votes

I have tried to use gets() library function in C and found that printf() statement output is delayed and got displayed after gets() receiving the input from stdin (i.e. from keyboard). Please check below C code and its output.

 #include<stdio.h>
 #include<stdlib.h>
 int main()
 {
    int n, i, j;
    char ch, *str;

    printf("Enter size of input:\n");
    scanf("%d\n", &n);

    str = (char *) malloc(sizeof(char) * n);
    printf("Enter input string: \n");
    gets(str);

    printf("Given input string is : %s\n", str);
    return 0;
 }

Output:

Enter size of input: 9

R Raj Kumar <-- This name is given from input since program is waiting for input for gets() function even though printf("Enter input string\n") is present before gets() and printf() statement is not displayed on the console. It is getting printed after receiving gets() input from console.

Enter input string:

Given input string is : R Raj Kumar

2
The input is far bigger than you say, so UB ensues. gets is deprecated for a reason.Deduplicator
You should never, ever use gets() for the reasons described in the linked Q&A.Jonathan Leffler
The '\n' in scanf("%d\n", &n); "delays". scanf("%d\n", &n) does not return until non-white- space is entered after the number. @JonathanLeffler Disagree that the posted dupe is a good explanation of this - which is the first of many problems with this code.chux - Reinstate Monica
@chux: I agree with your disagreement...it's the wrong dup, but it is a dup. I missed the trailing white space in the format string.Jonathan Leffler
The problem is the trailing \n in the scanf() format "%d\n" as chux said. Remove it. Then add a 'gobble' loop: int ch; while ((ch = getchar()) != EOF && ch != '\n') ; (empty loop body). That reads the trailing newline.Jonathan Leffler

2 Answers

1
votes

When you use gets(...) you need to have a buffer large enough to hold the input. Your input has 9 printed characters, but the actual input also has 2 spaces, and a terminating null character, so the size of your input is actually 12.

You have designed your program such that it is trivial to perform a buffer overflow. I suggest you read upon buffer overflows now, and learn to program defensively, avoiding buffer overflows.

Even if you are in a rush, reading up on how to avoid buffer overflows will save you time. The costs to fix a buffer overflow add up quickly, and far outweigh the time spent learning to avoid them.

0
votes

I have made changes in my program based on the comments/suggestions here. Below is the program that gives the right & desired output

Program:

#include<stdio.h>
 #include<stdlib.h>
 int main()
 {
    int n;
    char ch, *str;

    printf("Enter size of input:\n");
    scanf("%d", &n);

    while ((ch = getchar()) != EOF && ch != '\n');

    str = (char *) malloc(sizeof(char) * n);
    printf("Enter input string: ");
    fgets(str, n, stdin);

    printf("Given input string is : %s\n", str);
    return 0;
 }

Output:

Enter size of input: 9

Enter input string: R Raj Kumar

Given input string is: R Raj Ku