我写了一个小程序,自己在单步debug时,程序进不了for()循环,大伙看看。
程序代码:#include <stdio.h>
#include <ctype.h>
#define _STDC_WANT_LIB_EXT1_ 1
#define max_len 4
long input_num(void);
int main (void)
{
long num = 0L;
char j = '0';
printf("This sheet for \"printable character\"\n");
for(int i =0; i<=127; ++i)
{
j = (char)i;
if(isgraph(j))
{
printf(" number %d is symbol \'%c\'\t", i,j);
if(i%2==0)
printf("\n\n");
}
else
continue;
}
printf("This sheet for \"printable character\"\n");
printf("Plese input number to match the character! \nthis program will output the character.\n");
//i = 0;
num = input_num();
if(num>=0 && num<=32)
printf("\n\nthe number %ld was not a printable character, but also can print \'%c\' \n\n", num, num);
else if (num == 127)
printf("\n\nthe number %ld was not a printable character, but also can print \'%c\' \n\n", num, num);
else
printf("\n\nthe number %ld was a printable character, the character is \'%c\' \n\n", num, num);
return 0;
}
long input_num (void)
{
char xy[max_len] = {'0'};
int test = 0;
size_t str_len = 0;
long int xx = 0;
//printf("\nThis progarm is build a Mult-sheet.");
do
{
test = 0;
printf("\nPlase input a number(0 to 127) : ");
fflush(stdin);
int ret_scan = scanf_s("%s", xy, sizeof(xy));
if(ret_scan == EOF)
{
printf("Error reading,input overflow!!\n");
return 1;
}
str_len = strnlen_s(xy,sizeof(xy));
//printf("str_len is %zd\n", str_len);
if(str_len == 0)
{
printf("strnlen_s() function error!!");
return 2;
}
//fflush(stdin);
for(int i = 0; i<str_len; ++i) //问题就在这!程序再运行到这里时无法进入for()循环,why?
{
if((xy[i]>='0') && (xy[i]<='9')) //按道理程序要进入for()循环修改标志位 test.
test = 0;
else
test = 1;
}
xx = atol(xy);
if((xx>=0) && (xx<=127))
test = 0;
else
test = 1;
}
while(test == 1);
//xx = atol(xy);
return xx;
}
这个程序目的先是输出0~127所对应的可打印字符(printable character),然后提示用户输入0~127的数字,然后输出对应的可打印字符。我的子函数input_num()的for()循环是检查每一位输入的字符是否为数字,然后修改标志位test。若test==1则表示用户输入存在错误。我的程序怎么无法进入for()循环?在debug时发现在进入for循环之前i就满足跳出循环条件了。。。








