ios 是否有定义 long 或 integer 的最大值的常量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9760662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:23:43  来源:igfitidea点击:

Is there a constant which defines the max value of long or integer?

iphoneobjective-cioscipad

提问by u1053236

In java, there is a constant for the max value of Long. Such as:

在java中,Long的最大值有一个常量。如:

long minBillId = Long.MAX_VALUE

Is there a constant for the max value of long or int in Obj-C?

Obj-C 中 long 或 int 的最大值是否有常量?

回答by Amit Shah

If you import limits.hyou can call LONG_MAX

如果您导入,limits.h您可以调用LONG_MAX

For reference this siteshows how to get the max for all types as such:

作为参考,该站点显示了如何获得所有类型的最大值,例如:

#import <limits.h>
 // ...
NSLog(@"CHAR_MIN:   %c",   CHAR_MIN);
NSLog(@"CHAR_MAX:   %c",   CHAR_MAX);
NSLog(@"SHRT_MIN:   %hi",  SHRT_MIN);    // signed short int  
NSLog(@"SHRT_MAX:   %hi",  SHRT_MAX);
NSLog(@"INT_MIN:    %i",   INT_MIN);
NSLog(@"INT_MAX:    %i",   INT_MAX);
NSLog(@"LONG_MIN:   %li",  LONG_MIN);    // signed long int
NSLog(@"LONG_MAX:   %li",  LONG_MAX);
NSLog(@"ULONG_MIN not defined, it's always zero: %lu", 0);  
NSLog(@"ULONG_MAX:  %lu",  ULONG_MAX);   // unsigned long int
NSLog(@"LLONG_MIN:  %lli", LLONG_MIN);   // signed long long int
NSLog(@"LLONG_MAX:  %lli", LLONG_MAX);
NSLog(@"ULLONG_MIN not defined, it's always zero: %llu", 0);  
NSLog(@"ULLONG_MAX: %llu", ULLONG_MAX);  // unsigned long long int

回答by bryanmac

INT_MAX should resolve to 2147483647

INT_MAX 应解析为 2147483647

This:

这个:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSInteger foo = INT_MAX;
    NSLog(@"foo: %li", foo);  // li because 64 bit OSX cmd line app

    return 0;
}

Outputs:

输出:

2012-03-18 13:58:54.509 Craplet[51324:707] foo: 2147483647

回答by Jason

  NSLog(@"INT_MIN:    %i",   INT_MIN);
  NSLog(@"INT_MAX:    %i",   INT_MAX);
  NSLog(@"LONG_MIN:   %li",  LONG_MIN); 
  NSLog(@"LONG_MAX:   %li",  LONG_MAX);