scanf behaviour

Discussion in 'C Programming' started by Mohd Azhar Hussain, May 15, 2007.

  1. I am new to C. I was trying to read input from key board using scanf,
    here's my program


    int main(void)
    {

    int i,j;

    scanf("hai");
    scanf("%d\n",&i);
    printf("i value %d\n",i);
    scanf("%d",&j);
    printf("j value %d\n",j);



    return 0;
    }

    its printing i and j values both at a time(desired should print i
    value first and j next).
    I am using gcc 4.2 on linux .
    what does scanf("hai") mean?
    Is there any special meaning if i give '\n' in scanf?
    Mohd Azhar Hussain, May 15, 2007
    #1
    1. Advertising

  2. Mohd Azhar Hussain

    Argento Lee Guest

    "Mohd Azhar Hussain" <> wrote in message
    news:...
    >
    > I am new to C. I was trying to read input from key board using scanf,
    > here's my program
    >
    >
    > int main(void)
    > {
    >
    > int i,j;
    >
    > scanf("hai");
    > scanf("%d\n",&i);
    > printf("i value %d\n",i);
    > scanf("%d",&j);
    > printf("j value %d\n",j);
    >
    >
    >
    > return 0;
    > }
    >
    > its printing i and j values both at a time(desired should print i
    > value first and j next).
    > I am using gcc 4.2 on linux .
    > what does scanf("hai") mean?
    > Is there any special meaning if i give '\n' in scanf?
    >


    I am a newbie too and I don't have all the answers to your questions but I
    guess this reading would help you understand more about the '\n' part.
    http://c-faq.com/stdio/scanfhang.html
    Argento Lee, May 15, 2007
    #2
    1. Advertising

  3. Mohd Azhar Hussain <> wrote:
    > I am new to C.


    Do you have a good reference book or tutorial?

    <snip>
    > scanf("%d\n",&i);

    <snip>
    > Is there any special meaning if i give '\n' in scanf?


    Yes. Any good reference will tell you.

    It's also a good idea to consult the FAQ before
    posting...

    http://c-faq.com/stdio/scanfhang.html

    --
    Peter
    Peter Nilsson, May 15, 2007
    #3
  4. On May 15, 8:26 am, Mohd Azhar Hussain <> wrote:
    > I am new to C. I was trying to read input from key board using scanf,
    > here's my program
    >
    > int main(void)
    > {
    >
    > int i,j;
    >
    > scanf("hai");
    > scanf("%d\n",&i);
    > printf("i value %d\n",i);
    > scanf("%d",&j);
    > printf("j value %d\n",j);
    >
    > return 0;
    >
    > }
    >
    > its printing i and j values both at a time(desired should print i
    > value first and j next).
    > I am using gcc 4.2 on linux .
    > what does scanf("hai") mean?
    > Is there any special meaning if i give '\n' in scanf?


    thanks for your answers..
    I didnot understand what happnens if i write somethin like

    scanf("%dabc",&i);

    thanks once again.
    Mohd Azhar Hussain, May 15, 2007
    #4
  5. Mohd Azhar Hussain

    Guest

    On 15 May, 04:26, Mohd Azhar Hussain <> wrote:
    > I am new to C. I was trying to read input from key board using scanf,


    A huge mistake, IMHO and I'm not alone in that opinion. See large
    parts of http://c-faq.com/stdio/index.html, notably
    http://c-faq.com/stdio/scanfprobs.html.

    > what does scanf("hai") mean?
    > Is there any special meaning if i give '\n' in scanf?


    If you read the manual (and perhaps the FAQ) carefully, I expect it
    will tell it, frankly I don't know and don't care.

    In common with strtok(), the number of places where scanf() is the
    correct solution to a problem can almost be counted on the fingers of
    one foot, and interactive input is definitely not in that set. It
    should be very low on the list of functions for a learner to try to
    work with...
    , May 15, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Thorpe

    Correct behaviour of scanf and sscanf

    Rob Thorpe, Mar 14, 2005, in forum: C Programming
    Replies:
    6
    Views:
    447
    Dan Pop
    Mar 15, 2005
  2. Lalatendu Das

    confused about behaviour of scanf

    Lalatendu Das, Dec 21, 2005, in forum: C Programming
    Replies:
    33
    Views:
    1,002
    Dave Thompson
    Jan 4, 2006
  3. Scanf Behaviour

    , Dec 30, 2005, in forum: C Programming
    Replies:
    7
    Views:
    404
    Keyser Soze
    Dec 30, 2005
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,386
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    673
    Richard Bos
    May 2, 2006
Loading...

Share This Page