...

View Full Version : How does scanf works?



pandol
06-01-2008, 06:35 PM
I can not understand what is wrong and how scanf works in this example? I would expect the program to allow the user insert 2 characters and 2 numbers, while the program does not do that!!! Why? How scanf works?, What is the difference between scanf("%c", &v); and scanf("%c\n", &v);

#include<stdio.h>

int main ()
{
char v;
int j,w;
for (j=0; j<2; j++)
{
scanf("%c", &v);
}

for (j=0; j<2; j++)
{
scanf("%d", &w);
}
}

tito_moklet
06-17-2008, 02:10 PM
im not find any wrong at your code.. but if u want see the result of that program, why u not include a "printf" sintax on that??

Codman
06-17-2008, 03:11 PM
Hi pandol

I am not getting where exactly you are stucked? I mean does the program gives you a wrong output or it does not excepts the 2 characters or 2 numbers or something else is the case.

And as far as your next statement


What is the difference between scanf("&#37;c", &v); and scanf("%c\n", &v);

I would say that the above two are altogether different.

"scanf("%c", &v);" this will accept the character including spaces as the input and stores it in "v" and the cursor will remain at the end of character while in latter the cursor will be at the next line.

Also, I want to bring to your notice that you have written a code which runs the for loop twice causing the value of "v" to be overwritten and same is applicable to the value of "w".

So, just check the code.

yatin.agrawal
09-20-2009, 06:08 PM
Simply use flushall after scanning characters that will flush all the characters like"\n","\t",etc and vill now not take them as a characters



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum