Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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);
    }
    }

  • #2
    New to the CF scene
    Join Date
    May 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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??

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •