2015-07-12 78 views
0
/*Program to print all the numbers between a lower bound and a upper bound values*/  

#include<stdio.h> 
#include<stdlib.h> 
void recur(int a, int b); 
int main(void) 
{ 
    int x,y; 
    printf("Enter the lower and upper bound values: \n"); 
    scanf("%d %d",&x,&y); 
    void recur(x,y); 
    return 0; 
} 
void recur(int a,int b) 
{ 
    if(a<b) 
    { 
     printf("%d /n",a); 
     a++; 
     void recur(a,b); 
    } 
} 

我得到的輸出是:ç遞歸函數調用的概念

Enter the lower and upper bound values: 
    10 
    50 
    process returned 0. 

這有什麼錯的語法或返回類型..? 我剛開始學習c.Need幫助

+5

'void recur(x,y);' - >'recur(x,y);','void recur(a,b);'同上。還有'/ n' - >'\ n' – BLUEPIXY

+3

你也想把「/ n」改成「\ n」來產生換行符,我相信。 – Yuval

+0

該程序在哪裏返回0消息輸出?我沒有看到任何printf消息。順便說一下,在主代碼中'void recur(x,y);'只聲明你有一個外部函數,它接受一個未定義的參數列表並返回'void',而不是調用它。不知何故,您必須使用一些C11編譯器或至少C98,因爲它允許您在可執行代碼之後聲明函數原型。 –

回答

2

兩個

void recur(x,y); 
void recur(a,b); 

聲明功能(原型)。爲了通話他們,改變他們

recur(x,y); 
recur(a,b); 
+0

是否有必要在main()和recur()中有不同的變量..?我的意思是形式參數x,y和實際參數a,b。 –

+0

@Gow。編譯器是否有可能注意到不必要的變量並優化它們? – Sebivor

+0

@高是的。爲什麼不嘗試一下? –

-1

這是你的工作的代碼。

#include<stdio.h> 
#include<stdlib.h> 
void recur(int a, int b); 
int main(void) 
{ 
    int x,y; 
    printf("Enter the lower and upper bound values: \n"); 
    scanf("%d %d",&x,&y); 
    recur(x,y);   // change made here 
    return 0; 
} 
void recur(int a,int b) 
{ 
    if(a<b) 
    { 
     printf("%d \n",a); 
     a++; 
     recur(a,b);   // change made here 
    } 
} 
+0

你爲什麼不在你的代碼中加入任何解釋/評論......你只是試圖證明你比他聰明?它爲什麼有效?什麼是錯誤?我認爲你是聰明的編程,但沒有解釋。 –

0
void recur(int a,int b) 
{ 
    if(a<b) 
    { 
     printf("%d /n",a); 
     a++; 
     //void recur(a,b); You need to call the function not to declare 
     // there is difference between dec and calling. 
     recur(a,b); // is the correct way to do this . 
    } 
} 

同樣需要在main()方法雖然

0

你已經在你的程序如下錯誤:

  1. 當你調用一個函數的語法是
funcName(param1, param2); 

或者,如果函數返回一個值:

ret = funcName2(param3, param4, param5); 

在代碼中,把無效的通話時間是一個語法錯誤:

void recur(a,b); 

這是函數的聲明方式,不叫。注意函數聲明和函數定義是有區別的。

  • 如果要打印用C特殊字符,像換行,你需要打印 '\ n' 行這樣的:
  • printf("some message followed by newline \n"); 
    

    注意,「\ n'是單個字符,即使你看到'\'和'n'。 '\'的目的是爲了逃避下一個字符'n'使它成爲一個新字符。所以'\ n'是換行符。

    C中的其他特殊字符爲:'\ t'爲製表符,'\'用於實際打印'\'。 C中的字符串用雙引號括起來,比如「message」,而字符用單引號括起來,如'a','b','\ n','n','\'。