庸善德 发表于 2018-1-31 15:48:54

C语言基础代码打不对

#include <stdio.h>
main(),
{
        int a,b,sum;
        printf("please input two numbers:\n");
        scanf("%d%d",&a,&b);
        sum=add(a,b);
        printf("%d+%d=%d",a,b,sum);
}
int add(int a,int b)
{
int c;
c=a+b;
return c;
}

错误码:

        data definition has no type or storage class
expected identifier or '(' before '{' token
recipe for target 'main.o' failed

BngThea 发表于 2018-1-31 15:56:13

add函数应该放在main函数前面
#include <stdio.h>

int add(int a, int b)
{
        int c;
        c = a + b;
        return c;
}

int main(void)
{
        int a, b, sum;
        printf("please input two numbers:\n");
        scanf("%d%d",&a,&b);
        sum = add(a, b);
        printf("%d + %d = %d\n", a, b, sum);
      
    return 0;
}

boot 发表于 2018-1-31 15:59:58

本帖最后由 boot 于 2018-1-31 16:03 编辑

你书里的程序有错误
main后的逗号去掉
int add(int a,int b);这句加到main和include中间
或者 加在main函数里。你要调用函数,必须要声明函数。
最后问一句,你是不是要考二级呀
{:10_256:}{:10_256:}{:10_256:}

庸善德 发表于 2018-1-31 16:27:20

boot 发表于 2018-1-31 15:59
你书里的程序有错误
main后的逗号去掉
int add(int a,int b);这句加到main和include中间


没有,我毕业很久了,大脑开始退化了,刚好最近上班比较闲,当成兴趣读读看。

lapo_Mu 发表于 2018-1-31 21:11:11

没错

Happycsd 发表于 2018-1-31 21:48:35

一般函数写在主程序之前,否则需声明
页: [1]
查看完整版本: C语言基础代码打不对