C语言关于强制转换符的问题

/*从键盘输入一个不多于5位的整数,能显示出它是几位数,并按正反两种顺序显示出各位数字*/
#include<stdio.h>
void main()
{long a;
int place,one,ten,hundred,thousand,ten_thousand;
printf("intput a number(0~99999):");
scanf("%ld",&a);
if(a<0||a>100000)
{printf("input again\n");
scanf("%ld",&a);}
if(a>9999)
place=5;
else if(a>999)
place=4;
else if(a>99)
place=3;
else if(a>9)
place=2;
else place=1;
printf("\nweishu:");
printf("%d\n",place);
ten_thousand=a/10000;
/*thousand = (int)(a-ten_thousand*10000)/10,在求除万位以外的其他位时得加这个强制转换符,要不得不到
正确的结果。编译器是turbo c。其他 编译器就不会有这个问题。原因因该是跟turbo c 里int 型是16位,而long型是32位有关
具体原因谁能帮我解释下呢?*/
thousand=(a-ten_thousand*10000)/1000;
hundred=(a-ten_thousand*10000-thousand*1000)/100;
ten=(a-ten_thousand*10000-thousand*1000-hundred*100)/10;
one=(a-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
printf("wangwei=");
printf("%d\n",ten_thousand);
printf("qianwei=");
printf("%d\n",thousand);
printf("baiwei=");
printf("%d\n",hundred);
printf("shiwei=");
printf("%d\n",ten);
printf("gewei=");
printf("%d\n",one);
switch(place)
{case(1):printf("the changed number is:");printf("%d\n",one);break;
case(2):printf("the changed number is:");printf("%d%d\n",one,ten);break;
case(3):printf("the changed number is:");printf("%d%d%d\n",one,ten,hundred);break;
case(4):printf("the changed number is:");printf("%d%d%d%d\n",one,ten,hundred,thousand);break;
case(5):printf("the changed number is:");printf("%d%d%d%d%d\n",one,ten,hundred,thousand,ten_thousand);break;
}
getchar();
getchar();
}
我想问的是加不加这个(int)有什么不同? 有区别啊。在turbo c里没加强制转换就不正确了啊。

如果是32位(或者64位)平台上就无区别,这些平台上int和long int都是占4字节,但是古老的TC2是16位的,所以在它里面,int是2字节.在转换时会丢失精度。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-02
给你一个简单点的,这个没有限制是5位数以内,需要的话自己再设置一下

#include <stdio.h>
#include "string.h"

void main()
{
char str[100];
int s,i,n,j;
printf("input a number:");
scanf("%d",&s);
for(i=0;s>0;i++)
{
n=s%10;
s=(s-n)/10;
str[i]=n+48;
}
str[i]='\0';
printf("\n这是一个%d位数的数字\n",i);
printf("\n正序为:");
for(j=i-1;j>=0;j--)
printf("%c ",str[j]);
printf("\n逆序为:");
for(j=0;j<i;j++)
printf("%c ",str[j]);
}

好久不用tc了,不清楚,你试下不就知道了
相似回答