我可以在C中更改默认的返回类型int吗?

时间:2017-11-17 08:10:37

标签: c return-type

有没有办法可以在64位机器上更改int的位宽?

在64位机器上,int为32位,long为64位。

下一个程序将核心,因为getenv函数默认返回int。但char *是64位。我想更改int位宽,或将默认返回类型更改为long:

#include <stdio.h>

int main(){
    printf(getenv("HOME"));
}

1 个答案:

答案 0 :(得分:2)

  

因为getenv函数默认返回int

没有。这是一个C标准的lib函数,定义为(C11 7.22.4.6):

using Newtonsoft.Json;
using NodaTime;
using NodaTime.Serialization.JsonNet;
using NodaTime.Text;
using System;

public class DateObj
{
    public ZonedDateTime Date { get; set; }
}

class Program
{
    static void Main()
    {
        var date = "2017-10-26 13:32:11 Etc/GMT";
        var json = $"{{\"Date\": \"{date}\"}}";

        var pattern = ZonedDateTimePattern.CreateWithInvariantCulture(
            "yyyy-MM-dd HH:mm:ss z",
            DateTimeZoneProviders.Tzdb
        );

        var settings = new JsonSerializerSettings
        {
            DateParseHandling = DateParseHandling.None,
            Converters = { new NodaPatternConverter<ZonedDateTime>(pattern) }
        };
        var dateObj = JsonConvert.DeserializeObject<DateObj>(json, settings);
        Console.WriteLine(dateObj.Date);
    }
}

但是,如果使用旧的,过时的C编译器编译代码并且无法包含#include <stdlib.h> char *getenv(const char *name); ,则返回类型将默认为stdlib.h。这是18年前修复的C语言的设计错误。

因此,如果您的程序中存在错误,因为您未能包含相关标头int ,那么您将getenv返回int的唯一原因就是您使用的是旧的,过时的编译器。

通过添加stdlib.h来解决此问题,然后确保您使用的是现代编译器 1)

1)早于5.x的gcc版本默认为旧的,过时的非标准#include <stdlib.h>。如果您使用的是旧版本的gcc,则必须使用以下选项之一进行编译:{{1​​}}(如果支持则为首选)或-std=gnu90