ANSI-C:打印十进制int的最大字符数 [英] ANSI-C: maximum number of characters printing a decimal int

查看:105
本文介绍了ANSI-C:打印十进制int的最大字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以轻松确定打印小数的最大字符数 int

I'd like to know if it is an easy way of determining the maximum number of characters to print a decimal int.

我知道< limits.h> 包含 INT_MAX 等定义,表示最大一个int可以假设,但它不是我想要的。

I know <limits.h> contains definitions like INT_MAX that say the maximum value an int can assume, but it is not what I want.

我希望能够做到这样的事情:

I'd like to be able to do something like:

int get_int( void )
{
    char draft[ MAX_CHAR_OF_A_DECIMAL_INT ];

    fgets( draft, sizeof( draft ), stdin );
    return strtol( draft, NULL, 10 );
}

但是如何找到 MAX_CHAR_OF_A_DECIMAL_INT的值以便携和低调的方式?

But how to find the value of MAX_CHAR_OF_A_DECIMAL_INT in a portable and low overheaded way?

谢谢!

推荐答案

我不知道在纯ANSI-C中做任何你想要的技巧是什么,但在C ++中你可以轻松地使用模板元编程来做:

I don't know if it is any trick to do what you want in plain ANSI-C, but in C++ you can easily use template metaprogramming to do:

#include    <iostream>
#include    <limits>
#include    <climits>

template< typename T, unsigned long N = INT_MAX >
class   MaxLen
{
public:
    enum
    {
        StringLen = MaxLen< T, N / 10 >::StringLen + 1
    };
};

template< typename T >
class   MaxLen< T, 0 >
{
public:
    enum
    {
        StringLen = 1
    };
};

你可以用纯C代码调用它来创建一个额外的C ++函数:

And you can call it from your pure-C code creating an additional C++ function like this:

extern "C"
int int_str_max( )
{
    return  MaxLen< int >::StringLen;
}

这有一个ZERO执行时间开销并计算所需的确切空间。

This has a ZERO execution time overhead and calculates the exact space needed.

您可以使用以下内容测试上述模板:

You can test the above templates with something like:

int main( )
{
std::cout << "Max: " << std::numeric_limits< short >::max( ) << std::endl;
std::cout << "Digits: " << std::numeric_limits< short >::digits10 << std::endl;
std::cout << "A \"short\" is " << sizeof( short ) << " bytes." << std::endl
    << "A string large enough to fit any \"short\" is "
    << MaxLen< short, SHRT_MAX >::StringLen << " bytes wide." << std::endl;

std::cout << "Max: " << std::numeric_limits< int >::max( ) << std::endl;
std::cout << "Digits: " << std::numeric_limits< int >::digits10 << std::endl;
std::cout << "An \"int\" is " << sizeof( int ) << " bytes." << std::endl
    << "A string large enough to fit any \"int\" is "
    << MaxLen< int >::StringLen << " bytes wide." << std::endl;

std::cout << "Max: " << std::numeric_limits< long >::max( ) << std::endl;
std::cout << "Digits: " << std::numeric_limits< long >::digits10 << std::endl;
std::cout << "A \"long\" is " << sizeof( long ) << " bytes." << std::endl
    << "A string large enough to fit any \"long\" is "
    << MaxLen< long, LONG_MAX >::StringLen << " bytes wide." << std::endl;

    return  0;
}

输出为:

Max: 32767
Digits: 4
A "short" is 2 bytes.
A string large enough to fit any "short" is 6 bytes wide.
Max: 2147483647
Digits: 9
An "int" is 4 bytes.
A string large enough to fit any "int" is 11 bytes wide.
Max: 9223372036854775807
Digits: 18
A "long" is 8 bytes.
A string large enough to fit any "long" is 20 bytes wide.




  • 注意 std略有不同的值: :numeric_limits< T> :: digits10 和MaxLen< T,N> :: StringLen,因为如果不能达到'9',前者不会考虑数字。
    当然你可以使用它,如果你不在乎在某些情况下浪费一个字节,只需添加两个。

    • Note the slightly different values from std::numeric_limits< T >::digits10 and MaxLen< T, N >::StringLen, as the former does not take into account digits if if can't reach '9'. Of course you can use it and simply add two if you don't care wasting a single byte in some cases.
    • 编辑:

      有些人可能发现很奇怪,包括< climits>
      如果您可以使用C ++ 11,您将不需要它,并且将获得额外的简单性:

      Some may have found weird including <climits>. If you can count with C++11, you won't need it, and will earn an additional simplicity:

      #include    <iostream>
      #include    <limits>
      
      template< typename T, unsigned long N = std::numeric_limits< T >::max( ) >
      class   MaxLen
      {
      public:
          enum
          {
              StringLen = MaxLen< T, N / 10 >::StringLen + 1
          };
      };
      
      template< typename T >
      class   MaxLen< T, 0 >
      {
      public:
          enum
          {
              StringLen = 1
          };
      };
      

      现在你可以使用

      MaxLen< short >::StringLen
      

      而不是

      MaxLen< short, SHRT_MAX >::StringLen
      

      好,不是吗?

      这篇关于ANSI-C:打印十进制int的最大字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆