Thread: scanf problem

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    1

    Cool scanf problem

    When I use the following method to input numbers, it is seen that the compiler /linker takes 11 inputs from the user.

    for(i=0;i++;i<10)
    scanf("%d", &a[i]);

    However when i use the following way to scan the numbers, it is seen that the compiler/linker behaves as expected. i.e 10 inputs are taken from the user.
    i=10;
    while(i--)
    scanf(""%d", &a[i]);

    My question, is why the disparity. Why does the user have to input 11 numbers, despite running the loop 10 times.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    for(initializers; condition; aux)

    So you mean
    Code:
    for(i = 0; i < 10; i++)
        scanf("&#37;d", &a[i]);
    You may also want to read the manual regarding scanf(), use sscanf() instead.

    Other than that, the result is logical. while(i--) means basically: while((i - 1)) hence 10, instead of 11 as seen in the for loop... probably related to scanf.

    Consider:
    Code:
    int i = 10;
    do {
        printf("loop %d\n", i);
    } while(i--);
    Notice the loop is run before i is touched,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM