Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: why is gcc telling me my for loop is wrong

  1. #1
    Join Date
    Jul 2008
    Beans
    1,706

    why is gcc telling me my for loop is wrong

    heres my for loop
    Code:
    for(int i = 2; i < x; i++)
    {
         //code here
    }
    heres the error

    Code:
     error: ‘for’ loop initial declaration used outside C99 mode

  2. #2
    Join Date
    Mar 2007
    Location
    Florida
    Beans
    Hidden!

    Re: why is gcc telling me my for loop is wrong

    Don't declare the variable in the loop, declare it before the loop.

  3. #3
    Join Date
    Nov 2007
    Beans
    706
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: why is gcc telling me my for loop is wrong

    You can't declare variables within the parenthesis following the for statement by C90 standards. I think C99 allows this. Rewriting it like this will work:
    PHP Code:
    int x;
    for (
    0yx++){
        
    // code here

    Programming is an art. Learn it. Live it. Love it.

  4. #4
    Join Date
    Jul 2008
    Beans
    1,706

    Re: why is gcc telling me my for loop is wrong

    Quote Originally Posted by Sinkingships7 View Post
    You can't declare variables within the parenthesis following the for statement by C90 standards. I think C99 allows this. Rewriting it like this will work:
    why doesnt gcc use the newest version of C?

  5. #5
    Join Date
    Jul 2008
    Beans
    1,706

    Re: why is gcc telling me my for loop is wrong

    and why am i getting floating point exceptions in here?

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>

    bool isPrimeint x );


    int main()
    {
        
    int x;
        
    printf("enter a number: \n"x);
        
    scanf("%d", &x);
        if (
    isPrime(x) == trueprintf("is prime \n");    
        if (
    isPrime(x) == falseprintf("is not primeprime \n");
        return 
    0;
    }

    bool isPrime(int x)
    {
        
    int i;
        if (
    2)
        {
            return 
    false;    
        }

        else
        {
            for(
    0xi++)
            {
                if (
    == 0)
                {
                    return 
    false;
                }
            }

            return 
    true;
        }

    while were here...does vim have auto indenting?

  6. #6
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: why is gcc telling me my for loop is wrong

    Quote Originally Posted by jimi_hendrix View Post
    and why am i getting floating point exceptions in here?

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>

    bool isPrimeint x );


    int main()
    {
        
    int x;
        
    printf("enter a number: \n"x);
        
    scanf("%d", &x);
        if (
    isPrime(x) == trueprintf("is prime \n");    
        if (
    isPrime(x) == falseprintf("is not primeprime \n");
        return 
    0;
    }

    bool isPrime(int x)
    {
        
    int i;
        if (
    2)
        {
            return 
    false;    
        }

        else
        {
            for(
    0xi++)
            {
                if (
    == 0)
                {
                    return 
    false;
                }
            }

            return 
    true;
        }

    You are computing x % 0 when i is 0, which is not allowed--it is like dividing by 0. Change the starting value of i in the loop to be 2. Not 1, because x % 1 is always 0.

    There are faster ways to determine if a number is prime, but that should fix your immediate problem.

  7. #7
    Join Date
    Mar 2007
    Location
    Florida
    Beans
    Hidden!

    Re: why is gcc telling me my for loop is wrong

    Quote Originally Posted by jimi_hendrix View Post
    and why am i getting floating point exceptions in here?

    PHP Code:
    ... 
    while were here...does vim have auto indenting?
    First of all, you're getting a floating point exception at
    PHP Code:
    if (== 0
    You can use the GNU DeBugger (GDB) to easily find the lines that are making your program crash, along with tons of other valuable information.
    You get it because it performs the operation (int) % 0. Your for loop should start with 2. 0 will raise floating point exceptions and 1 will return everything as composite.

    PHP Code:
        if (2)
        {
            return 
    false;    
        } 
    There are an infinite number of primes below the number two. That code makes your program return incorrect solutions. Perhaps you mean return true;. All numbers below two are prime, not composite.

    PHP Code:
        printf("enter a number: \n"x);
        
    scanf("%d", &x); 
    ... This is a fairly obvious mistake

    You should also look into the Sieve of Eratosthenes, it's not very complex, but even when you get your algorithm to work it will be very slow. It's easy to optimize it using elements of some well-established sieves and algorithms.

    And yes, vim has autoindenting. It's an option you can set in your ~/.vimrc or enable realtime.
    Last edited by Phenax; November 10th, 2008 at 03:38 AM.

  8. #8
    Join Date
    Jul 2008
    Beans
    1,706

    Re: why is gcc telling me my for loop is wrong

    now about that indenting thing...is there auto indenting in vim or are there lightweight editors with it?

  9. #9
    Join Date
    Jul 2008
    Beans
    1,706

    Re: why is gcc telling me my for loop is wrong

    thanks...i have to stop coding past 9 pm...

  10. #10
    Join Date
    Apr 2007
    Beans
    14,781

    Re: why is gcc telling me my for loop is wrong

    Quote Originally Posted by jimi_hendrix View Post
    now about that indenting thing...is there auto indenting in vim or are there lightweight editors with it?
    Yes, there is.

    Quote Originally Posted by jimi_hendrix View Post
    why doesnt gcc use the newest version of C?
    It does, but by default, it uses the most common version. You will have to specific the standard to use.

    Code:
    man gcc | grep "c99"

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •