473,836 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program without conditional Statements - Possible ?

hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !

Thanks, Raghu
Nov 14 '05
92 9920
Sidney Cadot wrote:

pete wrote:
It is easier for me to memorize, that

int main(void)
{
return 0;
}

will work in all C programs, in all standards,


Shouldn't you use EXIT_SUCCESS in C89?


I suppose you could, if you also wanted to #include <stdlib.h>,
but I don't get your point.

Are you saying that there's something
wrong with the above program in C89 ?

--
pete
Nov 14 '05 #81
Sidney Cadot wrote:
pete wrote:
It is easier for me to memorize, that

int main(void)
{
return 0;
}

will work in all C programs, in all standards,


Shouldn't you use EXIT_SUCCESS in C89?


0 works just fine in C89 and C99. Like EXIT_SUCCESS, 0 communicates the
successful completion of the program to the calling environment.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #82
Keith Thompson wrote:
Da*****@cern.ch (Dan Pop) writes:
In <bv**********@t itan.btinternet .com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
>donLouis wrote:
>
>> I believe that the missing return is OK for C89,
>
>It isn't.
Chapter and verse, please.


C90 5.1.2.2.3 "Program Termination":

A return from the initial call to the main function is equivalent
to calling the exit function with the value returned by the main
function as its argument. If the main function executes a return
that specifies no value, the termination status returned to the
host environment is undefined.

C90 6.6.6.4, "The return statement":

If a return statement without an expression is executed. and the
value of the function call is used by the caller, the behavior is
undefined. Reaching the } that terminates a function is
equivalent to executing a return statement without an expression.


Thanks, Keith.
It's not illegal, and it doesn't invoke undefined behavior (since
there's no "caller"), but I'd say that returning an undefined
termination status to the host environment doesn't qualify as "OK".


That's basically my take on it, too. (I note that Dan Pop disagrees.)

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #83
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:[...]
>It's not illegal, and it doesn't invoke undefined behavior (since
>there's no "caller"), but I'd say that returning an undefined
>termination status to the host environment doesn't qualify as "OK".


The question is what the standard says, not what you or anyone else does.
The C89 standard *explicitly* allows this programming construct, therefore
it is OK from the standard's point of view.


Please cite the section of the standard that defines its use of the
term "OK".

In the absence of such a definition, I will feel free to use the term
in its colloquial sense, based on my own opinion.


It is perfectly possible and sensible to use the colloquial sense when
deciding whether it is OK or not from the standard's point of view. So,
I fail to see your point about OK not being defined by the standard...
Failing to return a
value from main() is not "OK". What I mean by that is that it's a bad
idea. It doesn't violate the standard, it doesn't invoke undefined
behavior (unless you call main() recursively and try to use the
result), and it doesn't make hair grow on the palms of your hands.
If it doesn't invoke undefined behaviour and it doesn't require a
diagnostic, it is OK from the standard's point of view. OK?
It was perfectly clear that that was what I meant.
The original statement was made in the *explicit* context of a C standard.
You had no business to *implicitly* ignore that context in your reply.
Not every program is supposed to return a meaningful value to the host
environment. If I end all my toy programs with a return statement it is
to get a clean compile from gcc -Wall, not because the program termination
status has any meaning to anyone (which is always the case with programs
that only return one exit status: it contains as much information as no
exit status at all).


My environment is configured to complain if I run a program that
returns a failure status.


By this silly logic, it is not OK to return EXIT_FAILURE, because your
environment will complain. Yet, a hello world program that doesn't
check the return value of the output call has no a priori reason to
return 0 any more than EXIT_FAILURE.
<OT>I use tcsh with the $printexitvalue
variable set.</OT> If you don't care about the status returned by a
toy program, that's fine, but I do care about it.


This is not a valid argument when deciding whether a certain programming
construct is OK or not. The OP wasn't asking for personal opinions, but
for facts, and the fact is that the C89 standard explicitly allows it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #84
On 6 Feb 2004 19:45:31 GMT
Da*****@cern.ch (Dan Pop) wrote:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:

[...]
>It's not illegal, and it doesn't invoke undefined behavior (since
>there's no "caller"), but I'd say that returning an undefined
>termination status to the host environment doesn't qualify as "OK".

The question is what the standard says, not what you or anyone else does.
The C89 standard *explicitly* allows this programming construct, therefore
it is OK from the standard's point of view.


Please cite the section of the standard that defines its use of the
term "OK".

In the absence of such a definition, I will feel free to use the term
in its colloquial sense, based on my own opinion.


It is perfectly possible and sensible to use the colloquial sense when
deciding whether it is OK or not from the standard's point of view. So,
I fail to see your point about OK not being defined by the standard...
Failing to return a
value from main() is not "OK". What I mean by that is that it's a bad
idea. It doesn't violate the standard, it doesn't invoke undefined
behavior (unless you call main() recursively and try to use the
result), and it doesn't make hair grow on the palms of your hands.


If it doesn't invoke undefined behaviour and it doesn't require a
diagnostic, it is OK from the standard's point of view. OK?
It was perfectly clear that that was what I meant.


The original statement was made in the *explicit* context of a C standard.
You had no business to *implicitly* ignore that context in your reply.
Not every program is supposed to return a meaningful value to the host
environment. If I end all my toy programs with a return statement it is
to get a clean compile from gcc -Wall, not because the program termination
status has any meaning to anyone (which is always the case with programs
that only return one exit status: it contains as much information as no
exit status at all).


My environment is configured to complain if I run a program that
returns a failure status.


By this silly logic, it is not OK to return EXIT_FAILURE, because your
environment will complain. Yet, a hello world program that doesn't
check the return value of the output call has no a priori reason to
return 0 any more than EXIT_FAILURE.
<OT>I use tcsh with the $printexitvalue
variable set.</OT> If you don't care about the status returned by a
toy program, that's fine, but I do care about it.


This is not a valid argument when deciding whether a certain programming
construct is OK or not. The OP wasn't asking for personal opinions, but
for facts, and the fact is that the C89 standard explicitly allows it.


I think that the problem is that dogma takes precedence over
the standard.

--
donLouis
Nov 14 '05 #85
[snips]

On Tue, 03 Feb 2004 14:52:36 -0800, E. Robert Tisdale wrote:
Please just ignore Richard Heathfield.


If and when he's being a bonehead, sure, good advice. Thing is, he's so
rarely a bonehead (offhand, I can't recall his ever being so in the
several years I've been following c.l.c.) that I can't see the utility of
the advice.
Nov 14 '05 #86
Kelsey Bjarnason wrote:
[snips]

E. Robert Tisdale wrote:
Richard Heathfield wrote: gabriel wrote:
void main() { puts("hello world"); }

Any questions?


Just one. How did you manage to get a one-line C program *wrong*?
Please just ignore Richard Heathfield.


If and when he's being a bonehead, sure, good advice.
Thing is, he's so rarely a bonehead
(offhand, I can't recall his ever being so
in the several years I've been following c.l.c.)
that I can't see the utility of the advice.


I guess this was just one of those "rare" instances.
Nov 14 '05 #87
ra*********@vit .ac.in (Raghavendra R A V, CSS India) wrote in message news:<5a******* *************** ****@posting.go ogle.com>...
hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !

Thanks, Raghu


You should use funcs with params as choice, possible arrays
and (multiple?) return(smth) with recursion - see previous answers in
conf:
recursion could be used instead 'anything', not only loops.
There are langs with purely funcs-recursion style. But it's almost
just theory -
implementation requires new thinking - you should change conf for it
(I donno whether there are groups for such a languages or not).
Remember that practically
recursion is stack insafe (system stack overflow). If you wanna see
example loop
-optimizes-recursion-could-simply-does, e-mail (Sorry for my bad
English)

Melnikov Oleg, ki**********@ma il.ru
Nov 14 '05 #88
Oleg Melnikov <ki**********@m ail.ru> scribbled the following:
ra*********@vit .ac.in (Raghavendra R A V, CSS India) wrote in message news:<5a******* *************** ****@posting.go ogle.com>...
hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !
You should use funcs with params as choice, possible arrays
and (multiple?) return(smth) with recursion - see previous answers in
conf:
recursion could be used instead 'anything', not only loops.
There are langs with purely funcs-recursion style. But it's almost
just theory -
implementation requires new thinking - you should change conf for it
(I donno whether there are groups for such a languages or not).
Remember that practically
recursion is stack insafe (system stack overflow). If you wanna see
example loop
-optimizes-recursion-could-simply-does, e-mail (Sorry for my bad
English)


Why are you doing everything so difficult? All the OP asked for was
programs that did not use conditional statements, loop statements or
conditional operators. He did not say what the programs should do.
So this program qualifies:

int main(void) {
return 0;
}

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #89
On 3 Feb 2004 12:22:57 -0800, the right honourable
ra*********@vit .ac.in (Raghavendra R A V, CSS India) wrote:
hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !

Thanks, Raghu


25 years ago, I started work for a company. As I had written a program
in Fortran before, I was immediately labelled as King OneEye in that
country of blinds.
They had a "computer" to run a program for the calculation of certain
standard constructions.
I looked into the program and saw that the programmer did not
understand and use the idea of a loop counter.
He wrote a routine 10 times to process 10 similar parts of the
construction.
I introduced looping then, and brought the size of the program back to
3, instead of 9 magnetic cards (the machine was a Canon Canola
programmable tabletop machine).

Man, was I **KING** in the eyes of management !

As for conditionals, I suspect you cannot write a sensible program
without it, because taking decisions is the very raison d'etre of the
computer, is it not ?

Would even "hello World" have no looping or conditionals, if looked at
at the underlying machine code level ? C hides those , true...
But that is off-topic.

Anyway, I have the uneasy feeling, the OP is teasing his readers.
The answer being so obvious, and the kind of program resulting so ....
weird, useless, not done ?

frgr
Erik
Nov 14 '05 #90

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

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.