Monday, March 14, 2011

successful input means...........

as i said in my what scanf() returns????? post................. scanf returns the number of successful inputs.................... here i am going to explain what is the meaning of successful inputs..........

It return the number of input items successfully matched (means read attempt is unsuccessful if the input characters cannot be converted according to the given format code) and assigned......................
the return value does not include fields that were read but not assigned.....
A return value of zero indicates that no fields were assigned.

lets understand with the help of some examples............

int main()
{
int i;
char *s;
printf("Enter the string:");
i=scanf("%[012356789]",s);
printf("i=%d",i);
return 0;
}

Output:
Enter the string:hello
i=0

Explanation:
in the above example the string s can take only numbers and not any other character...............so when we input hello ............ scanf reads the characters but nothing is assigned to string s, hence scanf returns 0 at the place of 1......................


int main()
{
int i,j;
printf("Enter the number:");
i=scanf("%d",&j);
printf("i=%d",i);
return 0;
}

Output:
Enter the number:h
i=0

Explanation:
scanf is trying to read an int, but it receives the letter 'h' at the place of some number, hence it will return 0 at the place of 1............... and it doesn't mean nothing is assigned to i..............

No comments:

Post a Comment

Feel free to comment......