Program without conditional Statements - Possible ?

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

E. Robert Tisdale

int main(int argc, char* argv[]) { puts("hello world\n"); }
I was trying to make a point,
not please the c-std religious feelings of others here
(feel free to "correct" it if you want).
Anyway, I'm putting much more thought on this
than on writing the one-liner program
so that gives you an idea...

Please just ignore Richard Heathfield.
 
P

Peter Pichler

Sidney Cadot said:
Sidney said:
[...] /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?
 
P

Peter Pichler

E. Robert Tisdale said:
int main(int argc, char* argv[]) { puts("hello world\n"); }

Err, no Mister. It is _still_ wrong.
Please just ignore Richard Heathfield.

I think it would be a great relief to everyone if _you_ did, Mr E.
 
P

Peter Pichler

Andras Tantos said:
/* Set up some condition: */
int cond_a = (some_function_call() > 0) % 2;
int cond_b = (some_other_function() > 0) % 2;
/* Note: you can remove '> 0' and '% 2' if you make sure that the return
value of the functions is either 0 or 1 only */

Seriously, why do you need % 2 anyway? And I would go for == 0, not > 0.
/* Boolean logic: */
int cond_and = cond_a * cond_b;
int cond_or = (cond_a + cond_b) % 2;
/* Conditional assignment: */
int Value = value_true * (cond_and) + value_false * (1 - cond_and);

This takes care of conditional expressions.

And things like

if (x)
foo();
else
bar();

could be written as ((x) && foo()) || (bar()), given some restrictions on
foo() and bar(). Ugly.
 
S

Sidney Cadot

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


Best regards,

Sideny
 
N

nrk

Peter said:
Seriously, why do you need % 2 anyway? And I would go for == 0, not > 0.


And things like

if (x)
foo();
else
bar();

could be written as ((x) && foo()) || (bar()), given some restrictions on
foo() and bar(). Ugly.

((x) && (foo(), 1)) || (bar(), 1);

Look mama, creative abuse of the comma operator :)

-nrk.
 
K

Keith Thompson

gabriel said:
Yes, the question (to me) was not worth the effort of answering it
"properly."

So typing "void main" requires a greater effort than typing "int main"?
 
P

Papadopoulos Giannis

E. Robert Tisdale said:
Functions return values of expressions:

int min(int i, int j) {
return (i < j)? i: j;
}

not a conditional?? how come?


--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
 
C

Christopher Benson-Manica

I think it would be a great relief to everyone if _you_ did, Mr E.

Shouldn't that be "Mr. T"? Maybe if he said "I pity the foo!" when he
attacks luminaries such as Mr. Heathfield people would listen :)
 
J

Jack Klein

Do you have any experience with an *applicative*
(sometimes called *functional*) computer programming language
such as the LISt Processing (LISP) computer programming language?

The idea is to write programs containing *only* constant
(because C supports numerous different types) and function definitions.
Functions return values of expressions:

int min(int i, int j) {
return (i < j)? i: j;
}

which can be used to define constants:

const int k = min(i, j);

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

Morris Dovey

Sidney said:
Peter said:
Sidney Cadot 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();}
 
D

donLouis

Sidney said:
...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, that the warning
can be ignored, and that the -W is "aggressive", if you will.
 
R

Richard Heathfield

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

Sidney Cadot

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.

Best regards,

Sidney
 
J

Joona I Palaste

Shouldn't that be "Mr. T"? Maybe if he said "I pity the foo!" when he
attacks luminaries such as Mr. Heathfield people would listen :)

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"?
 
D

Derk Gwen

Anyway, Tcl is written is C, so this is input to a C program, rather than
the C program itself.

package require derkgwen
namespace import derk::lambda

proc SKIP {} {
}

proc JOIN {a b} {
eval $a
eval $b
}

proc IF {p a b} {
eval [lindex [list $b $a] [eval $p]]
}
IF [lambda {} {expr 1}] [lambda {} {puts true}] [lambda {} {puts false}]
IF [lambda {} {expr 0}] [lambda {} {puts true}] [lambda {} {puts false}]

proc WHILE {p a} {
IF $p [list JOIN $a [list WHILE $p $a]] SKIP
}
set x 1; WHILE [lambda {} {expr $::x<=10}] [lambda {} {puts $::x; incr ::x}]

proc DO {a p} {
JOIN $a [list WHILE $p $a]
}
set x 1; DO [lambda {} {puts $::x; incr ::x}] [lambda {} {expr $::x<=10}]
set x 11; DO [lambda {} {puts $::x; incr ::x}] [lambda {} {expr $::x<=10}]

proc FOR {p q r s} {
JOIN $p [list WHILE $q [list JOIN $s $r]]
}
FOR \
[lambda {} {set ::x 1}] \
[lambda {} {expr $::x<=10}] \
[lambda {} {incr ::x}] \
[lambda {} {puts $::x}]

proc SWITCH {p args} {
SWITCHTAIL [eval $p] $args
}
proc SWITCHTAIL {p s} {
IF \
[lambda {s} {expr [llength $s]==0} $s] \
SKIP \
[list IF \
[lambda {s} {expr [llength $s]==1} $s] \
[lindex $s 0] \
[lambda {p q r s} {
IF [lambda {p q} {expr {$p==$q}} $p $q] \
$r \
[list SWITCHTAIL $p $s]
} $p [lindex $s 0] [lindex $s 1] [lrange $s 2 end]]
]
}
SWITCH [lambda {} {expr 3}] \
1 [lambda {} {puts one}] \
2 [lambda {} {puts two}] \
3 [lambda {} {puts three}] \
4 [lambda {} {puts four}] \
5 [lambda {} {puts five}]

proc BLOCK {block} {
BLOCKTAIL 1 SKIP @ $block
}
proc BLOCKTAIL {K code curr block} {
IF [lambda {block} {expr [llength $block]>0} $block] \
[lambda {K code curr statement block} {
SWITCH [lambda {statement} {lindex $statement 0} $statement] \
goto \
[list JOIN \
[list proc $curr {} [list JOIN $code [lindex $statement 1]]] \
[list BLOCKTAIL $K SKIP % [lrange $block 1 end]] \
] \
gotoif \
[list JOIN \
[list proc $curr {} [list JOIN $code \
[list IF [lindex $statement 1] [lindex $statement 2] %$K] \
]] \
[list BLOCKTAIL [expr $K+1] SKIP %$K [lrange $block 1 end]] \
] \
label \
[list JOIN \
[list proc $curr {} [list JOIN $code [lindex $statement 1]]] \
[list BLOCKTAIL $K SKIP [lindex $statement 1] [lrange $block 1 end]] \
] \
[list BLOCKTAIL $K [list JOIN $code $statement] $curr $block]
} $K $code $curr [lindex $block 0] [lrange $block 1 end]] \
@
}
BLOCK [list \
[lambda {} {puts top}] \
[lambda {} {set ::x 1}] \
{goto inloop} \
{label loop} \
[list gotoif [lambda {} {expr {$::x>10}}] bottom] \
{label inloop} \
[lambda {} {puts $::x}] \
[lambda {} {incr ::x}] \
{goto loop} \
{label bottom} \
[lambda {} {puts bottom}] \
]
 
P

Papadopoulos Giannis

nrk said:
Peter Pichler wrote:




((x) && (foo(), 1)) || (bar(), 1);

Look mama, creative abuse of the comma operator :)

-nrk.
((x) && ({foo();}) || ({bar();});

uglier

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top