Program without conditional Statements - Possible ?

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

CBFalconer

Morris said:
Sidney said:
Peter said:
Sidney Cadot wrote:

[...] /Any/ one-line C program is wrong by the standards
of c.l.c if it is to have a discernable side-effect.

...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct, and have a side
effect.

It does? Pardon my ignorance, where?

Its "side effect" (after adding a semicolon to get it to
compile) is to take up resources. If you disagree, you would
have a point.

However, I feel that it takes a weird definition of "side
effect" indeed to define away what happens if you execute
10,000 simultaneous instances of this program on your
computer.

'S not bad; but I like better:

int main(void){return main();}

That will halt with a fault on any implementation known to me.
 
D

Dan Pop

In said:
I would think this program actually /is/ wrong "by the standards of c.l.c".
???


Hmmm. I guess I failed to stipulate that providing your own prototypes
doesn't count.

"Providing your own prototypes doesn't count."

...So there :p

So there:

int puts(); int main(){ return 0 * puts("Hello world"); }

No prototypes at all ;-)

Dan
 
M

Morris Dovey

CBFalconer said:
Morris Dovey wrote:

That will halt with a fault on any implementation known to me.

Lucky you! I still have a couple that'll just "go west". :)
 
J

Jeremy Yallop

CBFalconer said:
That will halt with a fault on any implementation known to me.

With optimization (-O2 or above) turned on GCC optimizes away the tail
call, making it equivalent to an infinite busy loop.

Jeremy
 
D

donLouis

donLouis said:
Sidney Cadot wrote:

Sidney Cadot wrote:

Richard Heathfield wrote:

Just one. How did you manage to get a one-line C program *wrong*???


That's easy. /Any/ one-line C program is wrong by the standards of
c.l.c if it is to have a discernable side-effect.

...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct,

Er, no. :)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1

I believe that the missing return is OK for C89,

It isn't.
that the warning
can be ignored,

It can't.
and that the -W is "aggressive", if you will.

I won't. The diagnostic is required because there is an error in the syntax
of the program: specifically, the for-statement syntax is:

for(expression-opt; expression-opt; expression-opt) statement

The statement part is not optional and must not be omitted. To omit it is a
syntax error, and a conforming implementation must generate at least one
diagnostic for a program containing a syntax error.

Oops, missed the terminating semi-colon on the for. I use -W all
the time myself, but I don't think it is required to generate
a syntax error on the for statement. Anyway, I was (not) thinking
of -Werror...

Question; does adding the semi-colon disqualify the program as a
"one liner"?

donLouis
 
D

donLouis

donLouis said:
which could be argued to be correct,

Er, no. :)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1


I believe that the missing return is OK for C89, that the warning
can be ignored, and that the -W is "aggressive", if you will.

The problem is that the for statement isn't properly terminated using a
semicolon, I'm afraid.

Yes, I see that I missed that not-so-subtle point. Oh well, more
coffee and cigarettes should fix that.
 
C

CBFalconer

Jeremy said:
With optimization (-O2 or above) turned on GCC optimizes away the
tail call, making it equivalent to an infinite busy loop.

and so it does! Thus disproving the old adage that optimization
can introduce bugs. This one removes one. Now if we can persuade
ERT here, and EGN on comp.programming, to run it on start-up, the
world would become a more peaceful place.
 
C

Christopher Benson-Manica

Joona I Palaste <[email protected]> spoke thus:

This is something that I've been often wondering. Why is it "the foo"
instead of "the fool"? Any relation to the ubiquitous metasyntactic
variable "foo"?

Short answer: because that's how Mr. T always said it.

Long answer: Mr. T is an old 70's television character. He was black,
proud of it, and drove a mean van. He was both a bastion of wholesome
living (I think he drove his van around on crusades for the local
YMCA, or something) and an icon of flavalicious style. One of his
characteristic expressions was "I pity the foo!" - the 'l' was omitted
since it made the expression sound so much cooler. More than you
wanted to know, I'm sure.

</offtopic>
 
P

Peter Pichler

donLouis said:
Question; does adding the semi-colon disqualify the program as a
"one liner"?

Doesn't it depend on *where* you put the semicolon? ;-)
 
P

Peter Pichler

Jack Klein said:
Why bring up this rubbish here? While the function definition might
be perfectly legal C, the definition of k most certainly is not.

I nearly replied in a similar fashion last night, but then I realized
that it *may* actually be valid (e.g. if used in a function scope) :-(

As someone pointed out recently, you cannot guess wrong all the time.

Peter
 
E

E. Robert Tisdale

Jack said:
Why bring up this rubbish here?
While the function definition might be perfectly legal C,
the definition of k most certainly is not.

What sort-of-C-like-but-not-really-C language are you thinking of?
On second thought, please don't tell us.

Nonsense!
It is both legal *and* recommended C 99.

You are out-of-touch Jack.
The world is passing you by.
 
A

Ashish

gabriel said:
Just out of curiosity... How did you come up with this question? School
assignment, or just something you thought up, perhaps?

Possible. When I was in school, my prof (who was barred from teaching any
programming courses) divided the class into 4 groups. One group was supposed
to write a C program, for a particular problem, using for loops, the other
with gotos and the other using both, and the last one, using neither.

I ended up initializing an array using...
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
....

*sigh*

Stupid prof, but fortunately the course lasted only 4 months. I'm still
recovering.
 
G

gabriel

Ashish said:
I ended up initializing an array using...
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
...
*sigh*

LOL! Cut and paste baby, cut and paste!

In my CS coursework, I ended up coding everything in C/C++ with Visual C++
6, debugging it there too, and also doing Boundschecker tests under
Windows. When when the source was fully clean and debugged, I would
compile under UNIX for submittal. Doing the whole thing under UNIX was
hell and I gave up soon.
 
N

nrk

Ashish said:
gabriel said:
Just out of curiosity... How did you come up with this question? School
assignment, or just something you thought up, perhaps?

Possible. When I was in school, my prof (who was barred from teaching any
programming courses) divided the class into 4 groups. One group was
supposed to write a C program, for a particular problem, using for loops,
the other with gotos and the other using both, and the last one, using
neither.

I ended up initializing an array using...
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
...

*sigh*

Stupid prof, but fortunately the course lasted only 4 months. I'm still
recovering.

Well, you could've used recursion... Now you'll tell me that functions, if
and arithmetic, logical operators were banned as well :) If that were the
case, you should tell your prof:

Real computer scientists don't write in anything less portable than a 2B
pencil. Its a shame that Turing machines are so poor at I/O.

#include <stdio.h>

int initarray(int *array, int len, int cur) {
if ( cur >= len ) return 0;
array[cur] = cur+1;
return initarray(array, len, ++cur);
}

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);
return 0;
}

-nrk.
 
G

gabriel

nrk said:
Well, you could've used recursion...

But with a 10000 element array will will blow your stack. Of course, with
a 10K element array your fingers would hurt as well from all the typing if
you do not use some sort of loop!
 
N

nrk

gabriel said:
But with a 10000 element array will will blow your stack. Of course, with
a 10K element array your fingers would hurt as well from all the typing if
you do not use some sort of loop!

Even my poorly written code would likely be converted by an optimizing
compiler into a non-recursive solution. If you make that:
void initarray(int *array, int len, int cur) {
if ( cur < len ) array[cur] = cur + 1, initarray(array, len, ++cur);
}
it is almost certain that most modern compilers will optimize away the
recursion. Read about tail call optimization if you're interested in
finding more.

-nrk.
 
A

Ashish

nrk said:
Ashish said:
gabriel said:
Raghavendra R A V, CSS India wrote:

condotional statements ? It would be a great help for me if someone
helps me...

Just out of curiosity... How did you come up with this question? School
assignment, or just something you thought up, perhaps?

Possible. When I was in school, my prof (who was barred from teaching any
programming courses) divided the class into 4 groups. One group was
supposed to write a C program, for a particular problem, using for loops,
the other with gotos and the other using both, and the last one, using
neither.

I ended up initializing an array using...
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
...

*sigh*

Stupid prof, but fortunately the course lasted only 4 months. I'm still
recovering.

Well, you could've used recursion... Now you'll tell me that functions, if
and arithmetic, logical operators were banned as well :) If that were the
case, you should tell your prof:

Real computer scientists don't write in anything less portable than a 2B
pencil. Its a shame that Turing machines are so poor at I/O.

#include <stdio.h>

int initarray(int *array, int len, int cur) {
if ( cur >= len ) return 0;
array[cur] = cur+1;
return initarray(array, len, ++cur);
}

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);
return 0;
}

-nrk.

--



What I gave was a simple example. The real example couldnt be done using
recursion. If I clearly remember, we had to read from a file containing
course and student info, and fill arrays.

So...
 
A

Ashish

nrk said:
Ashish said:
gabriel said:
Raghavendra R A V, CSS India wrote:

condotional statements ? It would be a great help for me if someone
helps me...

Just out of curiosity... How did you come up with this question? School
assignment, or just something you thought up, perhaps?

Possible. When I was in school, my prof (who was barred from teaching any
programming courses) divided the class into 4 groups. One group was
supposed to write a C program, for a particular problem, using for loops,
the other with gotos and the other using both, and the last one, using
neither.

I ended up initializing an array using...
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
...

*sigh*

Stupid prof, but fortunately the course lasted only 4 months. I'm still
recovering.

Well, you could've used recursion... Now you'll tell me that functions, if
and arithmetic, logical operators were banned as well :) If that were the
case, you should tell your prof:

Real computer scientists don't write in anything less portable than a 2B
pencil. Its a shame that Turing machines are so poor at I/O.

#include <stdio.h>

int initarray(int *array, int len, int cur) {
if ( cur >= len ) return 0;
array[cur] = cur+1;
return initarray(array, len, ++cur);
}

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);
return 0;
}

-nrk.


And if the problem were that simple, I didnt need an array. I could've
simply used the fact that a = i + 1 :)


-Ashish
 
A

Arthur J. O'Dwyer

((x) && ({foo();}) || ({bar();});

uglier

{[(x])}&(&{{foo}{};)] || ([)[bar][]()]};;)

uglier still

[That was a joke, son. :) Plays off of the fact that the text
you posted was not C, it was just a collection of parens and braces
superficially similar to nrk's C example. So duh, it's uglier.
It's easy to make things ugly when you don't care about sense.]

-Arthur
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top