请编写一个unsigned fun(unsigned w)

来源:百度知道 编辑:UC知道 时间:2024/06/30 06:10:42
请编写一个unsigned fun(unsigned w),w是一个大于10的无符号整数,若w是n(n>=2)位的整数,函数求出w的后n-1位的数作为函数值返回。
例如:w值为5923,则函数返回923;w值为923则函数返回23。

#include <stdio.h>

unsigned fun(unsigned w)
{
int c = 0, t = w, i;

while (t)
{
++c;
t /= 10;
}

if (c > 1)
{
t = 1;

for (i = 0; i < c-1; ++i)
t *= 10;

return (w % t);
}

return 0;
}

void main(void)
{
printf("%d %d\n", fun(1234), fun(5678));
}