declaring a variable inside a loop

This is a discussion on declaring a variable inside a loop within the C Programming forums, part of the General Programming Boards category; Hi, I have the following code which works as expected, however, if i remove the variable declartion "int i = ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    15

    declaring a variable inside a loop

    Hi,

    I have the following code which works as expected, however, if i remove the variable declartion "int i = 0" and declare it inside the for loop by adding the keyword int, i am getting a parsed error before int and that i is undeclared. Could someone tell me what I am doing wrong please?

    Thanks

    Code:
    #include <stdio.h>
    
    int main()
    {
          int marks[10];
          int count = 10;
          int i = 0;
          long sum = 0L;
          float average = 0.0f;
          printf("Enter Student's Marks\n");
          for (i = 0; i < count; ++i)
          {
          printf("%2d>\t", i+1);
          scanf("%d", &marks[i]);
          sum += marks[i];
          }
          printf("The total sum is %ld", sum);
          average = (float) sum/count;
          printf("The average is %.2f", average);
          printf("\nThese are the grades entered\n");
          for (int i = 0; i < count; i++)
          printf("\nStudent %d has a grade of %d\n", i+1, marks[i]);
          system("PAUSE");
          return 0;
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I think it is completely dependent upon the compiler you're using. In MSVS I'm also getting error. But I guess according to C99 it is perfectly legal. Which compiler are you using by the way?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    Thank you for your reply. The compiler which i am using is Bloodshed Dev C++ version 4 (probably it is an old compiler) but since i am new to C, so far i did not have any problems using it.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I tried the code in TC(which is also an old IDE), and it also gave me errors regarding i, so I myself is dumb.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Emerald View Post
    Thank you for your reply. The compiler which i am using is Bloodshed Dev C++ version 4 (probably it is an old compiler) but since i am new to C, so far i did not have any problems using it.
    If it predates the 1999 standard then it will not allow that I guess. You have to use -std=c99 to do this with gcc.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > The compiler which i am using is Bloodshed Dev C++ version 4 (probably it is an old compiler)
    FYI, Bloodshed Dev C++ is not a compiler, it's an IDE. More than likely you're using that IDE with MinGW (aka gcc).

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    IINM this language feature is independent of the specification (c89 or c99). Even tho' C is not a block-structured language, yet an inner block variable masks out a same name variable in the outer block, as in
    Code:
    int j = 0xff;                   /* outer "j" */
    for (int j = 0; j < 10; ++j) {  /* inner "j" masks out the outer "j" */
        ...
    }

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    15
    yes that's right, it is the MinGW compiler.

    You have to use -std=c99 to do this with gcc
    I am compiling and executing my code from a menu in the IDE, so i am not sure where I should put your suggested command.

    Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-06-2009, 07:20 AM
  2. Replies: 5
    Last Post: 06-23-2009, 03:51 AM
  3. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  4. Does declaring a variable cause lag in the executable?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2005, 08:15 PM
  5. pointers in arrays... Declaring objects in a loop.
    By Guest852 in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2005, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21