Saturday, 6 October 2012

the way to getout the rust of getchar() function

#include<stdio.h>

int main()
{

char c;

c=getchar();
printf("%d",c);
c=getchar();
printf("%d",c);
}

output:     suppose if u enter charecter 'a' as input: followed by b
    the output will be 97

10(ascii value of enter)

reason:"getchar automatically inserats newline charecter after taking input hence  by second getchar c takes the innput charecter \n from input buffer"

____to avoid this add "fflush(stdin)" command after first printf statement which washes away all the input charecters presenent in input buffer

No comments:

Post a Comment