lerrorgk 2017-08-08 09:40 采纳率: 25%
浏览 2086
已采纳

为什么只有输入count才会执行void count(void)函数

#include
char get_choice(void);
char get_first(void);
void count(void);
int get_int(void);
int main(void)
{
int choice;
void count(void); //为什么只有输入count才会执行void count(void)函数
while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' :
printf("Buy low, sell high.\n");
break;
case 'b' :
putchar('\a');
break;
case 'c' :
count();
break;
default :
printf("Program error!\n");
break;
}
}
return 0;
}

char get_choice(void)
{
int ch;
printf("Enter the letter of your choice:\n");
printf("a. advice b. bell\n");
printf("c. count q. quit\n");
ch = get_first();
while ((ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
}
return ch;
}

char get_first(void)
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}

void count(void)
{
int n, i;
printf("Count how far? Enter an integer:\n");
n = get_int();
for (i = 1; i <= n; i++)
printf("%d\n", i);
while (getchar() != '\n')
continue;
}

int get_int(void)
{
int input;
char ch;
while (scanf("%d", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}

  • 写回答

3条回答 默认 最新

  • 战在春秋 2017-08-08 10:03
    关注
    //声明函数,告诉编译器count这个名字是函数,其参数类型为void,返回值类型为void。
    //这句必须放在main函数外面。
    void count(void); 
    
    //在main主函数中调用count函数,因为count函数返回值类型为void,所以不用赋值给变量,直接调用即可。
    count();  
    

    函数声明、定义、调用三者是有区别的,必须遵循一定的语法格式。
    详细可参考: http://blog.sina.com.cn/s/blog_762cf5f80101eeln.html

    如果对您有帮助,请采纳答案好吗,谢谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料