why doesn't this recurse

K

kshudra

please tell me what is happening here why is it not recursing like the
other version
int main()
{
int c;

( ( c = getchar()) != EOF ) ? main() : putchar(c);

return 0;
}

the other version:
int main()
{
int c;
if ( ( c = getchar()) != EOF)
main();
putchar(c);
return 0;
}
 
R

ramasubramanian.rahul

abe saale .. kahan hai....
long time no see at Uttara ..
mail me back asap
 
N

Norm Mann

kshudra said:
please tell me what is happening here why is it not recursing like the
other version
int main()
{
int c;

( ( c = getchar()) != EOF ) ? main() : putchar(c);

In this example, the statement executed after falling out of recursion is
the return statement.
return 0;
}

the other version:
int main()
{
int c;
if ( ( c = getchar()) != EOF)
main();

In this example, the statement executed after falling out of recursion is
the putchar statement.
putchar(c);
return 0;
}

HTH,

-NM
 
A

Andrew Poelstra

please tell me what is happening here why is it not recursing like the
other version

please tell me what is happening here why are you not typing like the
other posters
int main()
{
int c;

( ( c = getchar()) != EOF ) ? main() : putchar(c);

return 0;
}

the other version:
int main()
{
int c;
if ( ( c = getchar()) != EOF)
main();
putchar(c);
return 0;
}

Both are recursive. You should fix your formatting, and make sure to
#include <stdio.h>
 
K

kshudra

Andrew said:
please tell me what is happening here why are you not typing like the
other posters


Both are recursive. You should fix your formatting, and make sure to
#include <stdio.h>
yes ,
i should have included the file stdio.h for it was essential for
EOF
please shed some light on why it is not executing putchar()
and also feel free to suggest posting etiquette or some guidelines you
can point to.
 
N

Norm Mann

kshudra said:
yes ,
i should have included the file stdio.h for it was essential for
EOF
please shed some light on why it is not executing putchar()
and also feel free to suggest posting etiquette or some guidelines you
can point to.

The two example statements are not the same:
The first is an IF-ELSE making the putchar() a *part* of the statement which
determines whether to recursivly call main().
The second is an IF and only determines whether to recursivly call main() or
not. The putchar() is the *next* statement.

When returning from recursion, it's the *next* executable statement after
the one that sent it recursing that is executed.

HTH,

-NM
 
A

Andrew Poelstra

yes ,
i should have included the file stdio.h for it was essential for
EOF
please shed some light on why it is not executing putchar()
and also feel free to suggest posting etiquette or some guidelines you
can point to.


Sure.
1) Be sure to snip signatures when you're replying, unless you're
replying to the signature itself.
2) Try your best to use proper grammar, capitalization, etc. It isn't a
big deal, but in general you get more help that way.
3) Format your code correctly and consistently:

int main(void)
{
int c;
((c = getchar()) != EOF) ? main() : putchar(c);
return 0;
}

int main(void)
{
int c;
if (( c = getchar()) != EOF)
main();
putchar(c);
return 0;
}

You can see that in the second example, putchar() is always executed, as
you have an if clause and nothing else. In the first example, putchar()
is /not/ executed if main is, because you have (effectively) an if
clause and an else clause.
 
C

Christopher Benson-Manica

please shed some light on why it is not executing putchar()

That's not what you asked the first time. The conditional operator
causes exactly one of its predicate expressions to be evaluated; if
you want both of them evaluated, use something other than the
conditional operator.
and also feel free to suggest posting etiquette or some guidelines you
can point to.

http://www.ungerhu.com/jxh/clc.welcome.txt
http://c-faq.com
http://benpfaff.org/writings/clc/off-topic.html
 
W

Walter Roberson

please shed some light on why it is not executing putchar()

Some people have said that the first version is not recursive, but
Andrew is correct that -both- versions are recursive.

The second version executes one putchar() per invocation of main().
The first version executes one putchar() -total-, and the value
that it attempts to putchar() is EOF.

The exact value of EOF is not defined by the C standards: what
is specified, though, is that the value of EOF must be negative.

When you putchar() a value, then that is equivilent to putc() the
value to stdout, which in turn is equivilent to fputc() the
value to stdout. fputc() will take the integral value handed to it,
the negative value which is EOF, and will convert it to an
unsigned char, and will output that.

*Usually* EOF is implemented as the signed value -1, and *usually*
converting -1 to unsigned char will result in the (decimal) value 255
[but there are systems on which neither of these "usually" hold].

stdout will be a text stream if the system can prove that stdout
is associated with "a terminal". The C standard says that if
you output something to a text stream and do not output a newline
before closing the stream, that the implementation is not required
to actually output it; if stdout happens to be a binary stream
then the output must be produced even without the trailing newline,
except that trailing '\0' characters are allowed to be lost.

Putting these together: if your operating system allowed you
to redirect the C program output to a file, and you were to carefully
examine that file after executing the first program, you would see
that you did get a small amount of output: whatever
converting EOF to unsigned char came out as. It is fairly unlikely
that it will happen to be a printable character, so you might
have to use some kind of binary dump tool in order to tell that it
is there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top