Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-01-2008, 05:35 PM   PM User | #1
pandol
New to the CF scene

 
Join Date: Jul 2006
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
pandol is an unknown quantity at this point
How does scanf works?

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);
}
}
pandol is offline   Reply With Quote
Old 06-17-2008, 01:10 PM   PM User | #2
tito_moklet
New to the CF scene

 
Join Date: May 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
tito_moklet is an unknown quantity at this point
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??
tito_moklet is offline   Reply With Quote
Old 06-17-2008, 02:11 PM   PM User | #3
Codman
New to the CF scene

 
Join Date: Jun 2008
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Codman is an unknown quantity at this point
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

Quote:
What is the difference between scanf("%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.
Codman is offline   Reply With Quote
Old 09-20-2009, 05:08 PM   PM User | #4
yatin.agrawal
New to the CF scene

 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
yatin.agrawal is an unknown quantity at this point
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
yatin.agrawal is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:15 PM.


Advertisement
Log in to turn off these ads.