the function
void fun()
{
char c;
if(c=getchar())!='\n')
fun();
printf("%c",c);
}
Gives the output cba .Can't say why?

Right off the bat I see 2 things...

if(c=getchar())!='\n')

should be

c == getchar();
if(c != '\n')

and

there is no getChar function, plus the fun() function calls itself.

getch() returns an int, not a char. And there is an open parenthesis missing in the if condition.

int c; 
if( (c==getchar())!='\n')
<snip>

the function
void fun()
{
char c;
if(c=getchar())!='\n')
fun();
printf("%c",c);
}
Gives the output cba .Can't say why?

This must have happened when you must have given the input "abc" isnt it ?

The above function is basically an example of a recursive function or basically a function which calls itself. Hence the above prog waits for the user input and keeps on accepting it until the user presses a Return or Enter key on the keyboard. And when the return key is pressed the entered input is printed in reverse order.

One note though, recursive functions if used improperly can cause stack overflow since each time a recursive function is called a new stack is allocated for the function.

Maybe this will give some info about the recursive functions
http://www.dsi.unive.it/~arch/corsoC/subsection3_9_5.html

Hope it helped, bye.

getch() returns an int, not a char. And there is an open parenthesis missing in the if condition.

int c; 
if( (c==getchar())!='\n')
<snip>

I don't see why two people assume that == is the desired operator between c and getchar(). For one, (c==getchar()) will never return a value equal to '\n'. And of course Deacon J's use of == is nonsensical.

a new stack is allocated

A new stack frame. There's just one stack (in C, anyway), and you normally don't make copies of it.

commented: Read Between The Lines :) +1

A new stack frame. There's just one stack (in C, anyway), and you normally don't make copies of it.

I think i should start using "Activation record" to avoid typing mistakes !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.