Thread: basic question

  1. #1
    Unregistered
    Guest

    Question basic question

    I'm new to C....

    Could someone explain why this won't work.....

    for (int=0; i<MAXSIZE;i++)

    if you place the int in the brackets you get a type error....


    The normal way works....
    int i;
    for (i=0;i<MAXSIZE;i++)

    Just wondering.....

    Thx for any help.....

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    You can't use int because it is a C keyword. If you declare a variable like this

    int int;

    how would the compiler know if you were referring to the variable int later in the program. long, double, float, char are keywords as well. There are many others, but you should get the idea.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >for (int=0; i<MAXSIZE;i++)
    You get a type error because there is a data type, but no identifier to go with it. int = 0 will flag an error but int i = 0 will work fine.

    However, C89 doesn't support declaring a variable in the for loop as C99 and C++ can do.
    for ( int i = 0; i < MAXSIZE; i++ ) /* This will flag an error */
    Until compilers start conforming to the new C standard you will have to declare your counter variable before the for loop at the start of the block.
    Code:
    int main ( void ) 
    {
      int i;
      for ( int = 0; i < MAXSIZE; i++ ) {
        /* Do loop stuff
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > int i;
    > for ( int = 0; i < MAXSIZE; i++ )

    You mean:

    for( i = 0; i < MAXSIZE; i++ )

    Don't you hate typos?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Don't you hate typos?
    Very much so

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Thumbs up

    yea, you can just do this in C++ not in C. in C you could only declare a variable at the very beginning of your whole code.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >in C you could only declare a variable at the very beginning of your whole code
    That's not entirely correct, in C you can declare a variable at the beginning of any block. The following code is perfectly legal.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i = 0;
      for ( i = 0; i < 5; i++ ) {
        int j = i;
        printf ( "%d, %d\n", i, j );
      }
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM