交换新手很抱歉,如果格式化已关闭。 我收到错误的"预期的声明说明符"这是我在底部定义的is_prime函数。你能否解释一下这个错误以及如何纠正它?
#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;
}
}
}
这是输出错误
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
答案 0 :(得分:3)
你错过了{
函数的开头大括号is_Prime
。
答案 1 :(得分:1)