Expected declaration specifier in for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcoll2204
    New Member
    • Mar 2015
    • 1

    Expected declaration specifier in for loop

    #include <stdio.h>
    #include <math.h>
    main()
    {
    int n;
    int k;
    int j;

    //gets user input for length of string
    printf("Enter the value of n:");
    scanf("%d", &n);
    //stores user input as n

    printf("Printin g primes less than or equal to %d: \n", n);

    for(k = 2; k <= n; k++)
    {
    if(is_Prime(k) == 1)
    {
    printf("%d,", k);
    }
    }


    //here is the is_Prime function
    int is_Prime (int k)


    for(j = 2; j < k; j++)
    {
    if(k%j != 0)
    {
    return 1;
    }

    else(k%j == 0)
    {
    return 0;
    break;
    }
    }

    When I compile my code I get an error saying that I need declaration specifiers in my is_prime subroutine in the for loop. Can anyone explain what this means or how to fix it? Here is the line of code it references.

    for(j = 2; j < k; j++)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are calling is_prime before you have defined it. The compiler doesn't know whether is_prime is a valid call.

    Either move the definition of the function above main() or place a function prototype above main to reasuure the compiler.

    BTW: main() is returning default int. main() should return an int.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      You need to declare variable j before you use it.

      You have a parenthesized expression in the else statement in is_Prime. But else does not have a condition argument. Perhaps you mean this to be a comment.

      Comment

      Working...