473,883 Members | 2,426 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 9930
On Thu, 05 Feb 2004 07:49:46 +0000, donLouis wrote:
On Wed, 4 Feb 2004 19:11:32 -0000
"Peter Pichler" <pi*****@pobox. sk> wrote:
"donLouis" <pa*******@eart hlink.net> wrote:
> > >> > Sidney Cadot wrote:
> > >> >
> > >> > int main(){for(;;)}
>
> Question; does adding the semi-colon disqualify the program as a
> "one liner"?
Doesn't it depend on *where* you put the semicolon? ;-)


I was semi-serious, actually. Which is the correct interpretation?

1. the white space is irrelevant, therefore, it's a one liner.


So by definition any program can be turned into a one liner. Just do the
preprosessing first. Is there a limit to line length?
2. the semi-colon implies a statement, therefore, it's a two liner.


Arn't you allowed statements in one liners?

int main(){for(;;){ }}

No additional semicolon :)

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #71
Nils Petter Vaskinn <no@spam.for.me .invalid> writes:
So by definition any program can be turned into a one liner. Just do
the preprosessing first. Is there a limit to line length?


Yes. A conforming C99 implementation is not required to support logical
source lines (i.e. source lines after backslash-newline-sequences have
been removed) longer than 4095 characters (5.2.4.1#1).

Martin
Nov 14 '05 #72
On Thu, 05 Feb 2004 10:02:44 +0100, Martin Dickopp wrote:
Nils Petter Vaskinn <no@spam.for.me .invalid> writes:
So by definition any program can be turned into a one liner. Just do
the preprosessing first. Is there a limit to line length?


Yes. A conforming C99 implementation is not required to support logical
source lines (i.e. source lines after backslash-newline-sequences have
been removed) longer than 4095 characters (5.2.4.1#1).


So the rule would be: "A one liner can be no more that 4095 characters".
Since a conforming compiler could reject it if it was longer.

Does anyone know if different compilers enforce this limit? I've never
written a 4096 character line so I wouldn't know.

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #73
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
Da*****@cern.c h (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.

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.

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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #74
donLouis <pa*******@eart hlink.net> writes:
[...]
1) I'm sitting at home coding in exercises from a text book. Stuff
like
#include <stdio.h>
int main(void) {
printf("whateve r\n");
/* more basic i/o stuff */
}
no filtering, no checking the return status (if I even can), just
learning basic for, while, do, case, whatever. There are plenty
of these types of programs that don't _necessarily_ need a return.

2) Infinite loops. In the bulk of the code that I write these
days, returning from main (or most other functions) is a fatal
error.

3) <OT?> Adding "dummy" returns to GUI code, just to silence
a warning that, based on the cited text above, I think is
harmless.


My advice: Develop good habits now. If a function is declared to
return an int, return an int; main is no different this respect than
any other function.

Ok, that's not quite true, for two reasons. First, C99, unlike C90,
explicitly requires falling off the end of main to be equivalent to
"return 0;" (a feature that I dislike, but there it is). Second, for
most functions, if you don't want to return a value, you can declare
it to return void; you don't really have that option for main.

But the point remains: even if you're not going to use the returned
value, it's easier just to be consistent about returning a value than
to worry about when you can get away with not doing so.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #75
Da*****@cern.ch (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. 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.

It was perfectly clear that that was what I meant.

OK?
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. <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.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #76
Peter Pichler wrote:
"CBFalconer " <cb********@yah oo.com> wrote:
nrk wrote:

.... snip ...
int main(void) {
int a[10];
size_t i;

initarray(a, sizeof a/sizeof a[0], 0);
for ( i = 0; i < sizeof a/sizeof a[0]; ++i )
printf("%d\n", a[i]);
return 0;
}


No no no. Replace the for statement with:

showarray(a, sizeof a/sizeof a[0], 0);

and my suggestion for showarray is:

void showarray(int *array, size_t len, size_t cur)
{
if (cur < len ) {
array[cur] = cur+1;
showarray(array , len, ++cur);
}
}


Which is equivalent to the for loop above exactly how? (Hint: no output)


I do seem to have goofed thoroughly. :-[ Maybe the "arr[cur] =
cur+1;" line should be revised :-)

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #77
On Thu, 05 Feb 2004 20:30:27 GMT
Keith Thompson <ks***@mib.or g> wrote:
donLouis <pa*******@eart hlink.net> writes:
[...]
1) I'm sitting at home coding in exercises from a text book. Stuff
like
#include <stdio.h>
int main(void) {
printf("whateve r\n");
/* more basic i/o stuff */
}
no filtering, no checking the return status (if I even can), just
learning basic for, while, do, case, whatever. There are plenty
of these types of programs that don't _necessarily_ need a return.

2) Infinite loops. In the bulk of the code that I write these
days, returning from main (or most other functions) is a fatal
error.

3) <OT?> Adding "dummy" returns to GUI code, just to silence
a warning that, based on the cited text above, I think is
harmless.


My advice: Develop good habits now. If a function is declared to
return an int, return an int; main is no different this respect than
any other function.

Ok, that's not quite true, for two reasons. First, C99, unlike C90,
explicitly requires falling off the end of main to be equivalent to
"return 0;" (a feature that I dislike, but there it is). Second, for
most functions, if you don't want to return a value, you can declare
it to return void; you don't really have that option for main.

But the point remains: even if you're not going to use the returned
value, it's easier just to be consistent about returning a value than
to worry about when you can get away with not doing so.


In the above example #1 and #3, I do habitually return a value from
main. My contention is that it is _not_ required in C89/90. For a
long time, I thought that it was. Your point on consistency,
however, is well taken.

--
donLouis
Nov 14 '05 #78
donLouis wrote:

On Thu, 05 Feb 2004 20:30:27 GMT
Keith Thompson <ks***@mib.or g> wrote:
donLouis <pa*******@eart hlink.net> writes:
[...]
1) I'm sitting at home coding in exercises from a text book. Stuff
like
#include <stdio.h>
int main(void) {
printf("whateve r\n");
/* more basic i/o stuff */
}
no filtering, no checking the return status (if I even can), just
learning basic for, while, do, case, whatever. There are plenty
of these types of programs that don't _necessarily_ need a return.

2) Infinite loops. In the bulk of the code that I write these
days, returning from main (or most other functions) is a fatal
error.

3) <OT?> Adding "dummy" returns to GUI code, just to silence
a warning that, based on the cited text above, I think is
harmless.


My advice: Develop good habits now. If a function is declared to
return an int, return an int; main is no different this respect than
any other function.

Ok, that's not quite true, for two reasons. First, C99, unlike C90,
explicitly requires falling off the end of main to be equivalent to
"return 0;" (a feature that I dislike, but there it is). Second, for
most functions, if you don't want to return a value, you can declare
it to return void; you don't really have that option for main.

But the point remains: even if you're not going to use the returned
value, it's easier just to be consistent about returning a value than
to worry about when you can get away with not doing so.


In the above example #1 and #3, I do habitually return a value from
main. My contention is that it is _not_ required in C89/90. For a
long time, I thought that it was. Your point on consistency,
however, is well taken.


It is easier for me to memorize, that

int main(void)
{
return 0;
}

will work in all C programs, in all standards,
than it is to know the variations of what will work
according to year of the standard and whatever else might matter.

--
pete
Nov 14 '05 #79
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?

Best regards,

Sidney

Nov 14 '05 #80

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.