在尝试学习 C 时,我遇到了这个问题:我正在尝试编写一个函数 squared,它应该在屏幕上打印一个填充的正方形,其大小和填充字符由用户选择。所以我写了这个:
#include <stdio.h>
int squared (int side, char fillCharacter);
int main(){
int side;
char fillCharacter;
printf("Enter the side ");
scanf("%d", &side);
printf("Enter the character ");
scanf("%c", &fillCharacter);
printf("%d%c",squared(side, fillCharacter));
}
int squared (int side, char fillCharacter){
for (int row=1 ; row <= side; row++){
for (int i =1; i <= side; i++){
printf("%c", fillCharacter);
}
puts("");
}
}
但遗憾的是,它的输出甚至没有扫描字符,只是打印一个空格
Enter the side 2
Enter the character
我尝试改变事情,但我让事情变得更糟,所以如果有人能给我一个提示,我真的很感激。谢谢。
最佳答案
此链接至 this problem 。这个答案提供了一个修复方法,但您可以通过反转对 scanf 的调用来修复它:首先是字符,然后是数字。
关于无法理解函数输出 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44482316/