[英]My simple C program isn't compiling
我正在为我的C级做一个小练习,我遇到的困难我知道不应该真的发生,因为这些应该花费最多30分钟。 到目前为止,这是我的程序:
#include <stdio.h>
#include <stdbool.h>
#define LIMIT 1000000;
bool isPrime( int num ) {
for ( int factor = 2; factor * factor <= num; factor++ )
if ( num % factor == 0 )
return false;
return true;
}
int main() {
for ( int num = 2; num <= LIMIT; num++ ) {
if ( isPrime( num ) ) {
printf( num );
}
}
return 0;
}
这是我得到的错误:
primes.c: In function “main”:
primes.c:14: error: expected expression before “;” token
primes.c:16: warning: passing argument 1 of “printf” makes pointer from integer without a cast
/usr/include/stdio.h:361: note: expected “const char * restrict” but argument is of type “int”
你有一个额外的;
在#define LIMIT 1000000;
。
处理#define
,编译器只执行文本替换:它将LIMIT
替换为1000000;
。 所以你的for
循环看起来像
for (int num=2; num < 1000000 ;; num++)
^^
发生第二个错误是因为printf
期望第一个参数是字符串(格式字符串),而不是整数。 例如printf("%d is prime.\\n", num);
( %d
是整数值的占位符, \\n
是行尾)。
LIMIT
定义后没有分号。 处理器指令没有得到它们所以它实际上是复制"100000;"
进入for循环。
printf的第一个参数应该是格式字符串"%d"
所以printf("%d\\n", num)
你会习惯的简单东西(在不思考的时候仍然会陷入困境),但如果你只是在学习,它看起来很棒。 远远超过我的第一个C程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.