using gets after scanf ...!!!

#include
using namespace std;

int main() {

int num; char str[20];

scanf("%d",&num);
gets(str);
return 0;

}

// in the code above .gets is not taking input from keyboard rather it is accepting the input from above scanf command

Scanf() ends taking input on \n or ’ ’ or EOF and gets() considers a whitespace as a part of the input string and ends the input upon encountering newline or EOF. Thus when you give some input like:

2abcd or
2
abcd   

In the first case scanf didn’t end taking input and in the secod case, input buffer has ‘\n’ before gets starts reading anything and the first character gets read is ‘\n’ and thus it ends there itself without reading further.

Give input like this

2 abcd  // notice the space

Or You may use Use this and it’ll work:

scanf("%d ",&num);
gets(str);
OR:
scanf("%d",num);
int c=getchar();
gets(str);
1 Like

thanx i got you …!!! it helped

Accept and close the thread if it clears… It helps other knowing the best possible answer which is correct… Happy to help… :slight_smile: