#include<stdio.h>
void reverse(void) {
int c;
if ((c=getchar())!='\n') reverse();
putchar(c);
}
main(){
printf ("Enter Text ") ;
printf ("\n") ;
reverse();
printf ("\n") ;
}
Here c is declared as int.
Unlike some other languages you may have used, chars in C are integers. char is just another integer type, usually 8 bits and smaller than int, but still an integer type.
In C we can convert between char and other integer types using a cast, or just by assigning.
Unless EOF occurs, getchar() is defined to return "an unsigned char converted to an int" so if it helps you can imagine that it reads some char, c, then returns (int)(unsigned char)c.
[note:we can convert this back to an unsigned char just by a cast or assignment, we can convert it to a char with a cast or by assigning it to a char.]
Recursion:
Eg string = load
we get the input text character by character using getchar() function until it get new line character. after taking each character the function call itself until is reach the end-of-line and the get the newline character. When it reaches the end-of-line then it start to print using putchar() function, (so first '\n' will be printed) and return to the previous recursive call of the function in the recursion and print the second last character (here 'd') and return to the upper function which call this function(which will putchar - a), this will recursively happens till the initial call of reverse.