2015-07-12 81 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','\'。