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

Dani AI

Generated

A compact clarification and a safe fix.

The snippet in the original post is a short recursive reader that prints characters on the unwind, so an input like "abc" appears reversed when the recursive calls return. Several replies already flagged the main causes of confusion: misplaced/missing parentheses and the assignment-vs-comparison trap (), plus the fact that character I/O functions return an int, not a plain char (). also noted the recursion/stack behavior and corrected some misunderstandings about comparisons and frames.

Important technical points to keep in mind (these are the real bugs people trip over):

  • Operator precedence: c = getchar() != '\n' is parsed as c = (getchar() != '\n') — that assigns 0 or 1 to c. To do the assignment and then compare the result, put the assignment in parentheses: if ((c = getchar()) != '\n').
  • Type and EOF: getchar() returns an int and uses EOF to signal end-of-file. Store its result in an int and check for EOF to avoid undefined behavior when input ends.
  • Recursion limits: each recursive call adds an activation record; very long input can cause a stack overflow. For long lines prefer an iterative buffer or explicit stack.

A concise, corrected recursive version:

#include <stdio.h>

void reverse_input(void) {
    int ch = getchar();
    if (ch != EOF && ch != '\n') {
        reverse_input();
        putchar(ch);
    }
}

Troubleshooting tips: compile with warnings enabled (for example -Wall -Wextra on GCC/Clang) to catch suspicious assignments and uninitialized uses; use putchar for single-char output; switch to an iterative buffer/realloc approach for very long input to avoid deep recursion.

Recommended Answers

All 5 Replies

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

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.