using gets after scanf ...!!!

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