gpt4 book ai didi

c - 我的简单 C 程序未编译

转载 作者:行者123 更新时间:2023-12-02 05:23:56 25 4
gpt4 key购买 nike

我正在为我的 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”

最佳答案

作为@Inspired表示在 LIMIT 宏定义中有一个额外的分号,该分号将由预处理器进行扩展

for ( int num = 2; num <= LIMIT; num++ ) {

像这样

for ( int num = 2; num <= LIMIT;; num++ ) {
/* ^^ 2 semicolons, now the num++ is extra */

但是你的程序还有另外一个问题

printf(num);

将不起作用,printf() 需要一个格式字符串,然后是参数,所以它应该是

printf("%d\n", num);

阅读this

关于c - 我的简单 C 程序未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com