Program without conditional Statements - Possible ?

  • Thread starter Raghavendra R A V, CSS India
  • Start date
P

pete

Sidney said:
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 ?
 
R

Richard Heathfield

Sidney said:
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.
 
R

Richard Heathfield

Keith said:
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.)
 
D

Dan Pop

In said:
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.
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
 
D

donLouis

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.
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.[/QUOTE]

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

Kelsey Bjarnason

[snips]

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.
 
E

E. Robert Tisdale

Kelsey said:
[snips]

E. Robert Tisdale wrote:

Richard said:
> gabriel wrote:
>
>
> Just one. How did you manage to get a one-line C program *wrong*?


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.
 
O

Oleg Melnikov

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, (e-mail address removed)
 
J

Joona I Palaste

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

Richard Bos

Erik said:
yes.
Astounding, how people can drift off into the blue yonder, here on
Usenet ...

Hey, the OP asked a stupid question, he gets stupid answers! Nothing new
there.

Richard
 
E

Erik

On 3 Feb 2004 12:22:57 -0800, the right honourable
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
 
E

Erik

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

yes.
Astounding, how people can drift off into the blue yonder, here on
Usenet ...

I can think of no parallel in the Real World where this happens.

Why, oh why ?

frgr
Erik
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top