Showing posts with label scanf. Show all posts
Showing posts with label scanf. Show all posts

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..............

Saturday, March 12, 2011

Reading and discarding characters in scanf()

Reading and discarding characters from the input stream

1.
int main()
{
int month,day,year;

printf( "Enter a date in the form mm-dd-yyyy: " );
scanf( "%d%*c%d%*c%d", &month, &day, &year );

printf( "month = %d day = %d year = %d\n\n", month, day, year );
return 0;
}

Output:
Enter a date in the form mm-dd-yyyy:03-12-2011
month = 03 day = 12 year = 2011

Explanation:
as we write the input:03-12-2011
scanf discards the character - because we used %*c to discard that character...there can be any character at the place of -...............

2.
int main()
{
int i,j;

printf( "Enter the numbers: " );
scanf( "%d/*/%d", &i,&j );

printf( "%d %d", i,j);
return 0;

}

output:
Enter the numbers:4/*/5
4 5

Explanation:
now we enter the numbers in this format 4/*/5 because scanf( "%d/*/%d", &i,&j ) consist /*/, and it discards this................ so i = 4 and j = 5..............

*****************************************

lets take one more example with field width and *

int main()

{

int a,b;

scanf("%d%*4c%d",&a,&b);

printf("a=%d,b=%d",a,b);

return 0;

}

/*before discussing its output.....firstly understand wht %*4c will do.......it will discard the 4 characters from the input string........*/

OUTPUT:

12abcd34

a=12

b=34

Explanation:

as the input string is 12abcd34.....the scanf start reading the characters 1..2..a.. as 'a' comes it start discarding the characters......n discard 4 characters continuously and then remaining characters i.e. 34 are in b variable....n suppose if d input string is 12abcde thn b has some garbage value........bcoz 'e' will not match with %d................

%[] explored

%[] consists a set of characters
The left bracket is followed by a set of characters and a right square bracket. The characters between the brackets define a set of characters making up the string. If the first character is not a circumflex (^), the input field consists of all subsequent input characters until the function finds a character not in the character set within the brackets.

void main()
{
char *s;
printf("enter the string:");
scanf("%[0123456789]",s);
printf("%s",s);
}

output:
enter the string:345y678
345

Explanation:
the var s consists 345 because y is not in the character set hence dont take no more characters from y. the above program shows the string can contain only the numbers.....

**********************
If the first character inside the square brackets is a circumflex, the input field consists of all subsequent input characters until the function finds a character that is in the bracketed character set. Thus
lets understand it with the help of an example:
void main()
{
char *s;
printf("enter the string:");
scanf("%[^0123456789]",s);
printf("%s",s);
}

output:
enter the string:sheetal7garg
sheetal

Explanation:
in the above program, string s can't contain any number......... it takes the characters until a number comes.......as in the example input is sheetal7garg and in character set there is [^0123456789] hence take the characters until 7 comes........... hence s contains sheetal.................
**************************************
One more example to understand it......

void main()
{
char *s1,*s2;
printf("enter the string:");
scanf("%5[^abc]%s",s1,s2);
printf("s1=%s\ns2=%s",s1,s2);
}

lets see different outputs....
1.
enter the string:welcome
s1=welco
s2=me

2.
enter the string:india
s1=indi
s2=a

Explanation:
in
scanf("%5[^abc]%s",s1,s2);
%5[^abc] means string s1 can store max 5 characters and does not take a,b or c characters as shown in 1st output s1 consist welco and also the remaining characters i.e. 'm','e' go in s2 string.........................similarly,in 2nd output s1 take characters i,n,d,i and stop when 'a' comes, then 'a' character is in s2 and if there are more characters after 'a' , all these also in s2...........

what scanf returns?????

scanf() returns the number of variables that were successfully assigned values means the number of successful inputs.

lets try to understand more clearly with the help of an example:

void main()
{
int i,j,k;
printf("Enter the values:");
i=scanf("%d%d",&j,&k);
printf("value of i=%d",i);
}

output:
Enter the values:12 13
value of i=2