用C语言编输入一个整数输出其位数

学习 时间:2026-04-04 18:40:21 阅读:7808
用C语言编输入一个整数输出其位数

最佳回答

慈祥的大雁

高贵的石头

2026-04-04 18:40:21

int 类型所能容纳的数字位数不能超过 10。我写的这个程序稍微长了点,但不受 int 类型容量的限制,能够处理很长的整数输入(由 buffer 数组的大小决定)。这程序只处理纯整数输入。有疑问尽管问。  #include #include void main( ) {    int  count = 0;    char buffer[ 1001 ],         *p = buffer,         c;    puts( "Enter an integer:" );    gets( buffer );    // Skip leading space。    while( isspace( *p ) )        p++;    while( isdigit( c = *(p + count) ) )        count++;    // Ignore number with trailing non-space character。    if( c != '\0' && !isspace( c ) )        count = 0;    if( count > 0 )        printf( "The number of digit(s) in your integer is %d。\n",count );    else        puts( "You didn't provide a valid integer。\n" );}

最新回答共有2条回答

  • 迷路的过客
    回复
    2026-04-04 18:40:21

    int 类型所能容纳的数字位数不能超过 10。我写的这个程序稍微长了点,但不受 int 类型容量的限制,能够处理很长的整数输入(由 buffer 数组的大小决定)。这程序只处理纯整数输入。有疑问尽管问。  #include #include void main( ) {    int  count = 0;    char buffer[ 1001 ],         *p = buffer,         c;    puts( "Enter an integer:" );    gets( buffer );    // Skip leading space。    while( isspace( *p ) )        p++;    while( isdigit( c = *(p + count) ) )        count++;    // Ignore number with trailing non-space character。    if( c != '\0' && !isspace( c ) )        count = 0;    if( count > 0 )        printf( "The number of digit(s) in your integer is %d。\n",count );    else        puts( "You didn't provide a valid integer。\n" );}

上一篇 一个乘数末尾有一个零,另一个乘数末尾也有一个零,积的末尾一定有两个零。

下一篇 是自行车前进的是后轮受到地面的摩擦力还是脚的蹬力?