c - C : Declaration of Functions简介

标签 c function debugging declaration absolute-value

我想在我的代码中使用 ANSI C 声明一个函数。 我可以在 main() 中使用函数后定义函数吗? 例如我写的一个绝对值函数:谁能告诉我它是否合理?

#include <stdio.h>

int main()
{
    double value1 = -5;
    printf("%lf",
           abs(value1));
}

double abs(double number)
{
    if (number < 0) {
        (number * (-1) = number);
    }
    return number;
}

最佳答案

您需要解决一些问题:

#include <stdio.h>

double abs(double number); // <<< 1. prototype here

int main()
{
    double value1 = -5;

    printf("%lf",
           abs(value1));
}

double abs(double number)
{
    if (number < 0) {
        number = number * -1.0; // <<< 2. fix here
    }
    return number;
}
  1. abs 函数需要一个原型(prototype),以便编译器在您调用它之前知道它的函数签名

  2. 你否定number的表达式是从后到前

关于c - C : Declaration of Functions简介,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16070299/

相关文章:

C - 调整指针数组的大小

c - 尝试声明字符串数组时出现 C 中的段错误。

c - 这段代码中究竟发生了什么?

c - gdb 无法运行带有 "File format not recognized"的 ELF 64 位程序

c++ - 来自 WDK 示例的 DbgPrint

C代码的Python翻译不起作用

c - 为什么我的链表冒泡排序函数有时会输出错误的结果,有时又看起来像是无限循环?

python - sklearn ,更改 cross_val_score 中的默认参数

javascript - 无法在 IE 中将函数从一个窗口传递到另一个窗口

debugging - Rust 有调试宏吗?