Thread: Scanf behaves weirdly in for loop.Please help.

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    5

    Question Scanf behaves weirdly in for loop.Please help.

    If I remove scanf the code works perfectly.Even the value of i is a garbage value.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main()
    {
        int arr[9],i;
    
    
        for(i=0;i<9;i++)
        {
            printf("Enter the %d th number:\n");
            scanf("%d",arr[i]);
            
    }
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    you missed the address operator in scanf and also there will be warning for printf.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    I think you should have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int arr[9],i;
    
        for(i=0;i<9;i++)
        {
            printf("Enter the %dth number:\n",i);
            scanf("%d",&arr[i]);
        }
    
    return 0;
    }
    or to make it linguisicaly correct:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
     
    int main()
    {
        int arr[9],i;
    
            i=1;
            printf("Enter the %dst number:\n",i);
            scanf("%d",&arr[i]);
     
            i=2;
            printf("Enter the %dnd number:\n",i);
            scanf("%d",&arr[i]);
             
            i=3;
            printf("Enter the %drd number:\n",i);
            scanf("%d",&arr[i]);
     
        for(i=4;i<9;i++)
        {
            printf("Enter the %dth number:\n",i);
            scanf("%d",&arr[i]);
        }
         
    return 0;
    }
    Last edited by nerio; 12-23-2015 at 05:22 AM.

  4. #4
    Registered User ARYAA's Avatar
    Join Date
    Dec 2015
    Location
    India
    Posts
    56
    What is the program about?

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    5

    @aryaa

    Quote Originally Posted by ARYAA View Post
    What is the program about?
    The program to read and print out the elements of the array. The code is for the read part.

  6. #6
    Registered User
    Join Date
    Dec 2015
    Posts
    5
    Thank You. Your code works good.

    I missed the ampersand symbol in the scanf statement.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    5
    Thank You.

  8. #8
    Registered User
    Join Date
    Sep 2015
    Posts
    5
    in case of i has a garbage value, you alrealdy initialized it to 0 in for loop...so there won't be a problem...
    Last edited by infinite_loop; 12-24-2015 at 01:17 AM.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by nerio View Post
    or to make it linguisicaly correct:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
     
    int main()
    {
        int arr[9],i;
    
            i=1;
            printf("Enter the %dst number:\n",i);
            scanf("%d",&arr[i]);
     
            i=2;
            printf("Enter the %dnd number:\n",i);
            scanf("%d",&arr[i]);
             
            i=3;
            printf("Enter the %drd number:\n",i);
            scanf("%d",&arr[i]);
     
        for(i=4;i<9;i++)
        {
            printf("Enter the %dth number:\n",i);
            scanf("%d",&arr[i]);
        }
         
    return 0;
    }
    Unfortunately, in making it "linguistically correct", a new logical problem has been introduced. Arrays in C are zero based. The code fills arr cells 1, 2, 3, and so on, but the first cell, 0, is not filled.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Although you certainly can make it linguistically correct.

    Code:
    #include <stdio.h>
    const char *GetOrdinalSuffix(long n)
    {
        n %= 100;
        if (n < 10 || n > 20)
        {
            n %= 10;
            if (n == 1) return "st";
            else if (n == 2) return "nd";
            else if (n == 3) return "rd";
        }
        return "th";
    }
    int main()
    {
        int i;
        for (i = 1; i <= 105; ++i)
            printf("%ld%s loop\n", i, GetOrdinalSuffix(i));
    
        return 0;
    }
    
    
    1st loop
    2nd loop
    3rd loop
    4th loop
    5th loop
    6th loop
    7th loop
    8th loop
    9th loop
    10th loop
    11th loop
    12th loop
    13th loop
    14th loop
    15th loop
    16th loop
    17th loop
    18th loop
    19th loop
    20th loop
    21st loop
    22nd loop
    23rd loop
    24th loop
    ...
    Personally, I'd just save the world some computing cycles...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program using char and string is working weirdly
    By kase20 in forum C Programming
    Replies: 3
    Last Post: 04-22-2012, 12:23 AM
  2. Getch behaves as key is previous key.
    By #define whatevr in forum C Programming
    Replies: 13
    Last Post: 03-13-2011, 04:15 PM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. ReadConsoleInput behaves bad.
    By antex in forum Windows Programming
    Replies: 2
    Last Post: 10-25-2005, 12:02 PM
  5. Math statement parsed weirdly
    By jverkoey in forum Tech Board
    Replies: 1
    Last Post: 06-05-2005, 05:32 AM

Tags for this Thread