hickttye 发表于 2019-3-16 17:18:46

带参数的函数

#include <stdio.h>
void pound(int n);
int main(void)
{
        int times = 5;
        char ch = '!';
        float f = 6.0f;

        pound(times);
        pound(ch);
        pound(f);

        return 0;
}

void pound(int n);
{
        while (n-- > 0)
                printf("#");
        printf("\n");
}

我想知道这个程序在visual studio中为什么会出错

人造人 发表于 2019-3-16 17:41:48

ba21 发表于 2019-3-16 18:37:03

敢问你在什么编译器就不出错?
错误1:参数不是int吗?传float可以?
float f = 6.0f;
pound(f);
问题2:
void pound(int n);
{
      while (n-- > 0)
                printf("#");
      printf("\n");
}

Sacura 发表于 2019-3-16 21:52:55

首先这个函数定义后边多了个分号
void pound(int n);
{
      while (n-- > 0)
                printf("#");
      printf("\n");

}
然后你定义的是int n,所以调用函数是只能传递整形,而你定义的ch是字符型,f是浮点型,所以pound(f)和pound(ch)这两句都是错误的。
页: [1]
查看完整版本: 带参数的函数