Knowledge of an array of characters

About gets function of usage:

Prototype: function only one parameter, the parameter of type char * type, str can be a pointer type variable name, or the name of a character array;

# include <stdio.h>
char* gets(char* str);

gets () function is the function to read a string of characters stored in the memory space pointed to by a pointer variable str from the input buffer.

Some differences with scanf function gets the ------- will empty the buffer?

When using the gets function, usually in the final knock the Enter key to end this function gets, if it gets back again to enter a character, you can directly input function gets the Enter key has been cleared (empty the buffer), but If it gets to the middle again necessary to add a getchar with scanf () function to store the return key knock

# include <stdio.h>
int main()
{
    int n;
    char x,a[10];
    scanf("%d",&n);
    x=getchar();    /*用getchar清空缓冲区*/
    gets(a);
    return 0;
}

Guess you like

Origin www.cnblogs.com/panghushalu/p/11818080.html