[英]Calling a function in C not producing a function
我是C語言的新手,到目前為止,它非常不同。 不過,我試圖使用scanf和switch語句從主函數中調用函數,但是我不相信所調用的函數正在起作用。
int main(void)
{
int Example_number = 0;
bool Continue = true;
char ch;
while(Continue)
{
printf("Which example would you like to run?\n");
scanf("%d",&Example_number);
switch(Example_number)
{
default: printf("No such program exists.\n");
break;
case 1: void Various_test();
break;
}
printf("Would you like to test another?(Y/N)\n");
scanf("\n%c",&ch);
if(ch == 'Y' || ch == 'y')
{
NULL;
}
else
{
Continue = false;
}
}
}
void Various_test(void)
{
int k = 2;
printf("\n%d",k);
}
我希望程序在輸入1的情況下打印2,但是while循環只是重復一次。
感謝您對這個問題的審議。
void Various_test()
是該函數的前向聲明。 要調用它,您實際上只需要Various_test()
。 您可能實際上需要前向聲明(取決於您的編譯選項)。 在那種情況下,將void Various_test();
以上main
。
您可以執行以下兩項操作之一:
在main的開頭添加函數聲明,如下所示:
int main(void)
{
void Various_test(void);
...
或者,將Variant_test的函數定義移至main之前,如下所示:
void Various_test(void)
{
int k = 2;
printf("\n%d",k);
}
int main(void)
{
int Example_number = 0;
...
您選擇的任何一種方式都將起作用。 就像您現在擁有的那樣,編譯器不了解Variant_test函數。 兩種方法都告訴編譯器有一個名為Various_test的函數,這就是它的外觀。
還有一件事,您在switch語句中錯誤地調用Various_test:
case 1: void Various_test();
應該:
case 1: Various_test();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.