. Advertisement .
..3..
. Advertisement .
..4..
I am working with c and getting the error message:
main.c: In function 'is_Prime':
main.c:29:1: error: expected declaration specifiers before 'for'
for(j = 2; j < k; j++)
^
main.c:29:12: error: expected declaration specifiers before 'j'
for(j = 2; j < k; j++)
^
main.c:29:19: error: expected declaration specifiers before 'j'
for(j = 2; j < k; j++)
^
main.c:42:1: error: expected declaration specifiers before '}' token
}
^
main.c:42:1: error: expected '{' at end of input
main.c: In function 'main':
main.c:42:1: error: expected declaration or statement at end of input
Here is the detail of the code that I ran:
#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("Printing 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 if(k%j == 0)
{
return 0;
break;
}
}
}
I need an explanation for the problems I’ve encountered. How to fix expected declaration specifiers or ‘…’ before?
The cause:
The error occurs due to is_Prime is specified inside the main body, but it’s impossible in C. Another reason is that you forget putting a closing curly brace to close
main
‘s body.Solution:
To fix this error, let’s close the
main
‘s body with a curly brace.For the
is_Prime
function, you are missing the curly brace opening{
.