n--<0不满足,不执行循环。跳过了。
【以下回答由 GPT 生成】
问题的原因是在pound()
函数中,参数是int
类型,而井号#
的ASCII码对应的是35,超出了int
类型的范围,导致输出时被截断成空格。要解决这个问题,可以将参数的类型改为char
类型。下面是修改后的代码:
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<float.h>
void pound(int n);
void printChar(char ch);
int main(void) {
int times = 5;
char ch = '#';
float f = 6.0;
pound(times);
printChar(ch);
printf("\n");
pound(f);
return 0;
}
void pound(int n) {
while(n-- > 0) {
printf("#");
}
}
void printChar(char ch) {
printf("%c", ch);
}
修改部分说明: 1. 新增了一个名为printChar()
的函数,用于打印单个字符。这个函数的作用是为了输出#
字符。 2. 在main()
函数中,将第二个调用pound(ch)
修改为printChar(ch)
,这样就可以通过新的函数打印出#
字符了。
这样修改后,程序就能正确输出井号#
了。
【相关推荐】