473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

why doesn't this recurse

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;
}

Oct 14 '06 #1
8 1438
abe saale .. kahan hai....
long time no see at Uttara ..
mail me back asap
kshudra wrote:
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;
}
Oct 14 '06 #2

"kshudra" <ex****************@yahoo.co.inwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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

Oct 14 '06 #3
On Fri, 2006-10-13 at 22:51 -0700, kshudra wrote:
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>

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 14 '06 #4
Andrew Poelstra wrote:
On Fri, 2006-10-13 at 22:51 -0700, kshudra wrote:
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>

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
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.

Oct 16 '06 #5

"kshudra" <ex****************@yahoo.co.inwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Andrew Poelstra wrote:
>On Fri, 2006-10-13 at 22:51 -0700, kshudra wrote:
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>

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
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

Oct 16 '06 #6
On Sun, 2006-10-15 at 22:46 -0700, kshudra wrote:
Andrew Poelstra wrote:
On Fri, 2006-10-13 at 22:51 -0700, kshudra wrote:
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

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.

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.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 16 '06 #7
kshudra <ex****************@yahoo.co.inwrote:
please tell me what is happening here why is it not recursing like the
other version
( ( c = getchar()) != EOF ) ? main() : putchar(c);
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

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 16 '06 #8
In article <11**********************@m73g2000cwd.googlegroups .com>,
kshudra <ex****************@yahoo.co.inwrote:
>Andrew Poelstra wrote:
>On Fri, 2006-10-13 at 22:51 -0700, kshudra wrote:
( ( c = getchar()) != EOF ) ? main() : putchar(c);
vs
if ( ( c = getchar()) != EOF)
main();
putchar(c);
>Both are recursive.
>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.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Oct 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher | last post by:
I have a hash that is several levels deep. ie: 'vars' => { '$filers' => '10.10.10.10/32', '$networksa' => '10.10.10.10/32', '$networksb' => '10.50.0.0/16', '$wintel_boxes' =>...
1
by: Cat | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm getting a validation error when I try to restrict the content of nested groups with xs:redefine whereas the same restriction on xs:element's...
1
by: Mr. B | last post by:
I've been beating my brains out on trying to figure out how to do Recurse (Parent/Child) code for my application (VB.net). Basically a User enters data into an array. The data is pretty simple:...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
6
by: Laszlo Szijarto | last post by:
What's the best way to recurse through the members of an enum? I want to take an enum and dump into a ListBox the enum's names. Thank you, Laszlo
2
by: Dmitri Shvetsov | last post by:
Hi, Does anybody know if I can use something like recurse to get the whole list of the public members of the class and their types/values? For example, I created the object with default...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
15
by: Aggelos | last post by:
Hello, I am using php4 And I have a class product I create an array of productObjects that has 2 dimensions. Generraly I am sorting my objects inside arrays and I need to some how recurse those...
63
by: David Mathog | last post by:
There have been a series of questions about directory operations, all of which have been answered with "there is no portable way to do this". This raises the perfectly reasonable question, why,...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.