【问题标题】:can not run the simple function program不能运行简单的功能程序
【发布时间】:2021-12-05 00:39:29
【问题描述】:
#include <stdio.h>
#include <conio.h>

int sum();

//find the sum of two numbers entered by the user.


int main(){
    int n, m;
    printf("Enter two numbers: \n");
    scanf("%d", &m);
    scanf("%d", &n);
    int result = sum(m, n);
    printf(result);
    getch();
    return 0;   
}




int sum(m, n){
    int c;
    c = m+n;
    return c;
}

我只是在写一个简单的函数程序,但我不知道为什么它没有运行它告诉我调试有人可以告诉我它有什么问题

【问题讨论】:

  • int sum()int sum(m, n) 更改为 int sum(int m, int n)。然后把printf(result);改成printf("%d", result);
  • 谢谢!!!完成
  • Ans 是什么问题?我认为任何最新的 C 编译器都会在该代码中发出 很多 警告,这应该可以帮助您找到before 此处询问的问题。你没有收到任何编译器警告吗?

标签: c function


【解决方案1】:
#include <stdio.h>
#include <conio.h>

int sum(int n, int m);

//find the sum of two numbers entered by the user.


int main(){
    int n, m;
    printf("Enter two numbers: \n");
    scanf("%d", &m);
    scanf("%d", &n);
    int result = sum(m, n);
    printf("%d",result);
    getch();
    return 0;   
}




int sum(int n, int m){
    int c;
    c = m+n;
    return c;
}

【讨论】:

  • 不鼓励仅使用代码的答案,因为它们很少解释问题所在。而且您的代码仍然存在可能导致崩溃的错误。
  • 现在可以测试提交以解决问题:-)
  • 它在那里测试过onlinegdb.com/online_c++_compiler
  • 在这里乞求声望是非常不受欢迎的,而且很可能只会为你赢得更多的反对票和删除标记。此外,您仍然只纠正了该程序中众多错误中的两个,并且您仍然没有对您所做的更改提供一个解释。
【解决方案2】:

改变 int sum() ;到 int sum(int, int) ; 将 printf(result) 更改为 printf("%d", result) ; 将 int sum(m, n) 更改为 int sum(int m, int n) ;(https://i.stack.imgur.com/Z07cx.jpg) 上面程序的另一种写法是 (https://i.stack.imgur.com/VOXRY.jpg)

【讨论】:

  • 请将代码和数据添加为文本 (using code formatting),而不是图像。图片:A)不允许我们复制粘贴代码/错误/数据进行测试; B) 不允许根据代码/错误/数据内容进行搜索;和many more reasons。除了代码格式的文本之外,只有在图像添加了一些重要的东西,而不仅仅是文本代码/错误/数据传达的内容时,才应该使用图像。
猜你喜欢
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多