Click here to Skip to main content
15,914,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int _tmain(int argc, _TCHAR* argv[])
{
    int a;
    float b;
   
    scanf("\n%d %f ",&a,&b);
    printf("\n%d %f",a,b);
    return 0;
}



In the above program , Because of space given after %f scanf will wait for third value before printing a and b . My doubt is where will the third value stored? Will space be allocated for it
Posted
Updated 29-Oct-12 22:17pm
v2
Comments
CPallini 30-Oct-12 5:21am    
Note, always check scanf return value, namely:
if ( scanf("%d %f", &a, &b) != 2 )
{
// handle error
}

What third variable? What third value?
When you call scanf it allocates space for the user input on the stack, reads from the keyboard up to an ENTER, then processes the input string according to the format you provide. There is no third variable involved, as you only supply two addresses and a constant format string.
 
Share this answer
 
Comments
steven8Gerrard 30-Oct-12 7:25am    
Since space is given after "%f" . Cursor wait till third value is typed before printing a & b . Check it
"Spaces" in the format string for scanf, an input function, are there to direct the scan to "discard" characters in the scanned string. So your format calls for a number (%d), discard a space (the space in the format string), another number (%f), discard another space, then no more format specifiers.

Scanf is not waiting for you to type another number, it's waiting for you to type another space.

Since you did not provide any additional variables (beyone a and b), anything you typed after that last space is ignored, discarded, thrown into the plasma pool, evaporated into the ether. No storage is allocated and it doesn't go anywhere (like Ocean Breeze Soap - extra credit if you know the movie reference) :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900