-7

I have this C recursive function

#include<stdio.h> 
int main(){ 
    int entier; 
    int rlt; 
    printf("\nSaisir un entier : ");  
    scanf("%d",&entier); 
    rlt=loga(5); 
    printf("Le logarithme base 2 de %d est %d:",entier,rlt); 
} 

int loga(int x){ 
    if(x==1){
        return 0;
    } 
    else {
        return (loga(x)=1+loga(x/2));
    } 
}
j0k
  • 22,600
  • 28
  • 79
  • 90
  • 10
    You make it sound as if a recursive function was some sort of disease. – jogojapan Jan 01 '13 at 12:00
  • 2
    It would also be nice to translate the texts into English. Although we all love and speak French, some don't. –  Jan 01 '13 at 12:03
  • 1
    Welcome to Stack Overflow. Ignore the chauvinists who can't read anything but English. The question should be English; AFAIAC, the code does not have to be. However, your question should identify what the problem is. For example, it should show the error message from the compiler complaining about the problems in the second `return` statement in `loga()`. Since you're using the C99 feature to return 0 from `main()`, you should also follow C99 rules and declare `loga()` before using it. – Jonathan Leffler Jan 01 '13 at 12:15
  • @JonathanLeffler: Yes I agree with you for technicality we can check the syntax, but many a times(not in this question) some logic comes from the variablenames, method names, and it becomes quite difficult to understand it. Then we have to discuss in comments. Other way round, few people from non-english region cant even ask question in english that is very difficult to understand. Can we have a translate to english kind of thing in SO? – Anoop Vaidya Jan 01 '13 at 12:28

4 Answers4

2

You need to declare your function before you use it.

#include<stdio.h> 

int loga(int x); //declaration

int main(){ 
    //... 
} 

int loga(int x){ 
    if(x==1){return 0;} else {return (loga(x)=1+loga(x/2));} 
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

You're assigning into non-lvalue in loga(x)=1+loga(x/2). This is meaningless and invalid.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
0

The recursive part seems strange.

int loga(int x){ 
    if(x==1){
        return 0;
    } else {
        return (1+loga(x/2));
//             ^^^^^^^^^^^^^ Changed here
    } 
}
timrau
  • 22,578
  • 4
  • 51
  • 64
  • 1
    I like that Clang-style spot-of-error identification with `^^^^^^`. –  Jan 01 '13 at 12:14
  • Please see that the function's prototype is not defined. It will also generate an error. See my answer posted below please.. – Praveen Vinny Jan 01 '13 at 12:19
-1

I think, the following code will solve your issue.

#include<stdio.h> 
int loga(int);

int main()
{ 
    int var; 
    int result; 
    printf("\nPlease enter a value : ");  
    scanf("%d",&var); 
    result=loga(5); 
    printf("The logarithm to the base 2 for %d is %d:",var,result); 
} 

int loga(int x)
    { 
    if(x==1)
        {
        return 0;
        } 
    else
        {
        return (1+loga(x/2));
        } 
      }

This code should execute correctly.

Praveen Vinny
  • 2,372
  • 6
  • 32
  • 40