Clearing stdin of extra characters. Using the fflush() function - C File

C examples for File:Standard Device

Description

Clearing stdin of extra characters. Using the fflush() function

Demo Code

#include <stdio.h>

int main( void )
{
    int age;/*from  w ww.j a  v  a2  s .c om*/
    char name[20];

    puts("Enter your age.");
    scanf("%d", &age);

    /* Clear stdin of any extra characters. */
    fflush(stdin);

    puts("Enter your first name.");
    scanf("%s", name);

    printf("Your age is %d.\n", age);
    printf("Your name is %s.\n", name);

    return 0;
}

Result


Related Tutorials