C 当返回 long 时,程序的行为就像我返回 int

标签 c return-type

所以,在我的函数中,当我尝试返回 long 时,它的行为就像我正在返回 int 。当我尝试返回 long 时,这主要发挥作用。有more than 10 digits ,它将返回 int仅限10 digits .

例如,我的代码试图找到 long 数组中的最大数字。是这样的,

#include<stdio.h>
#include<stdlib.h>

long maximum(long arr[])
{
    long i, max;

    for(i = 0; i < sizeof(arr); i++)
    {
        //This sets the initial number to the maximum
        if(i == 0)
        {
            max = arr[0]
        }

        else if(arr[i] > max)
        {
            max = arr[i];
        }
    }

    return max;
}

当我执行 printf 时在返回之前的数组中,它打印出正确的 long及其所有数字,但是当我返回它并尝试使用此函数返回 long 时在另一个函数中,它只会返回 int仅限10 digits 。我觉得我可能缺少一些明显的东西,但我不知道它是什么。

最佳答案

我的猜测是,您没有明确告诉编译器签名将从其他实例化单元返回long。只需在其他代码中的某处声明该函数实际上是long max(long arr[])。使用 gcc,您甚至可以使用 -Wimplicit-function-declaration

关于C 当返回 long 时,程序的行为就像我返回 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28082894/

相关文章:

在 C 中创建 atoi 函数

c++ - 标识符节点*未定义(链接器问题)

c++ - 为什么返回类型在函数名之前?

c++ - Koenig 查找的尾随返回类型和回退

c - pthread malloc 段错误

c - 将数组成员与整数进行比较时出现段错误

c - 如何在函数中动态分配数组?

c# - 返回类型错误的 Blazor 任务

return-type - 是否有可能获得要在宏中使用的方法调用的推断返回类型?

Python 3.x C API : Do I have to free the memory after extracting a string from a PyObject?