C without the braces

-3

My challenge is language-specific, but needs a lot of creativity: write a valid C program with only one pair of braces {} and none of the keywords if, for, while (all checked after preprocessing, and after substituting trigraphs). I'm looking for preprocessor abuse (but other methods are also welcome) to define a new pseudo-language which supports at least if/else, then use that language to do something cool.

That's pretty easy, and I'll just score this by upvotes since any scoring would be rather arbitrary, but here are some things which would be impressive:

  • function calls (also arguments, return values);
  • else if;
  • switch/case (also default, fall-through);
  • for or while (super-impressive. Also break, continue);
  • having only one semicolon ; (This one makes the challenge a bit different);
  • an interesting program;
  • full portability (portable in practice is good, portable in theory is even better).

This isn't about obfuscation. The preprocessor statements will probably be a bit hairy, but the actual code should be very easy to follow.

goto is allowed, but I believe it won't help much.

Dave

Posted 2013-08-22T01:43:57.860

Reputation: 7 519

Question was closed 2014-04-14T17:22:34.023

3To avoid braces, use goto statements. – Joe Z. – 2013-08-22T03:35:01.643

2Just a pedantic point that is really bugging me. There are no "implied braces" in a one line if/else construct. That thing works on single statements by design. The braces are a trick for making it work on multiple statements not the other way 'round. In any case, I'm with joe, c supports goto which means it supports unstructured programming. Welcome back to the big ball o'mud. – dmckee --- ex-moderator kitten – 2013-08-22T03:55:02.950

1Like dmckee, I don't get the implied braces idea. Are you forbidding if? Also, if braces are not allowed after preprocessing, what good will preprocessor abuse do? – ugoren – 2013-08-22T04:38:37.953

Uh... [! Nobody mention trigrams. ] – luser droog – 2013-08-22T04:40:04.690

Trigrams is a good point. Oops. (Unless those are handled by the pre processor, I'm not sure). @dmckee I did consider goto, but it won't help you unless you can get if working. And as for "implied braces", I see your point, so I suppose really I'm banning the native if, else, for, etc. (again after preprocessing). The intent of the challenge is to defining a pseudo-language which uses something other than braces to mark blocks. It's possible in at least one way, and I found it an interesting challenge; thought others might too. – Dave – 2013-08-22T10:12:49.517

@luserdroog pre processor abuse is really just to make the thing readable. I see your point about braces, so: yes. I am forbidding if, for, etc. (after preprocessing & trigraph substitution) – Dave – 2013-08-22T10:15:02.730

I feel I should restrict assembly too, but frankly, if somebody can make a portable, readable answer which uses assembly, I'm all for it. – Dave – 2013-08-22T10:30:39.303

Answers

6

Doesn't seem too hard....

#include <stdio.h>

int main(void) {
  printf("I only used one pair of braces!\n");
  printf("This is easy...\n");
  printf("Lalalalala\n");
  getchar();
  return 0;
}

having only one semicolon ; (This one makes the challenge a bit different);

Oh, also easy:

int main(void) {
  return 0;
}

else if;

How about goto and ternary operator abuse?

#include <stdio.h>

int main(void) {
  int testNum = 5;
  void *whichSection = testNum < 0 ? &&ifSection :
                       (testNum == 0 ? &&elseIfSection : &&elseSection);
  goto *whichSection;
  ifSection:
    printf("Number is negative\n");
    goto end;
  elseIfSection:
    printf("Number is zero\n");
    goto end;
  elseSection:
    printf("Number is positive and nonzero\n");
    goto end;
  end:
    return 0;
}

switch/case (kinda) (also default (kinda), fall-through (yes))

for (no) or while (no) (super-impressive. Also break (yes), continue (no));

My above solution is a bit like that - the goto is break, the elseSection is default, fall-through works, etc.

full portability (portable in practice is good, portable in theory is even better).

All of my solutions have this.


we code golfers are infamous for bending the rules :D

Doorknob

Posted 2013-08-22T01:43:57.860

Reputation: 68 138

hey hey now, I thought of that: "I expect … to define a new pseudo-language which supports at least if/else, then use that language to do something cool.". Admittedly I suppose that wasn't actually phrased as a rule. Hmm. – Dave – 2013-08-22T02:12:15.203

1@Dave Yep, bending the rules is our specialty :D – Doorknob – 2013-08-22T02:13:36.830

the goto stuff is more interesting! Although as I pointed out to Joe Z, it means your code isn't portable any more. Very cool though. – Dave – 2013-08-23T00:36:47.560

4

We can simulate a conditional branch in this manner:

int condition = /* insert some condition here, like a == b */ ;

void* if_branch[2];
if_branch[1] = &&ifcode;
if_branch[0] = &&elsecode;

goto if_branch[!!condition];

ifcode:
    /* code if (condition) is met */
    goto afterif;
elsecode:
    /* code if (condition) is not met */
afterif:

While convoluted, this shows that you can implement conditional branching without using if statements, ternary statements, or any other form of abuse other than goto pointers.

With conditional branching and goto (and all that other good stuff that doesn't require braces like addition, subtraction, etc.), you can build a machine language, and thereby implement all that other stuff that requires braces or "implied" braces, and could (theoretically, anyway) create any C program in this form without using more than one set of braces.

Joe Z.

Posted 2013-08-22T01:43:57.860

Reputation: 30 589

nice, I didn't know you could take pointers to labels. – Dave – 2013-08-23T00:23:07.540

one thing I will mention: I believe labels need unique names, so this method will struggle as far as making the code really easy to follow (i.e. it's impossible to hide all the details behind defines). But I still like it a lot. – Dave – 2013-08-23T00:28:24.577

The label pointers were also included in Doorknob's answer. I just generalized it a bit and showed that you could make any program like that. – Joe Z. – 2013-08-23T00:31:26.513

Ah that answer's been updated. I hadn't noticed. Also I've been looking it up and this is a GCC extension, so it doesn't quite meet all the points I listed (not portable). – Dave – 2013-08-23T00:34:08.973

Well it's the best we've got, basically. – Joe Z. – 2013-08-23T00:43:53.440

sure; I don't know any solution which meets every single point (my own solution doesn't support break or continue in loops, and the syntax for loops is odd). I'm just saying there's room for something more. I still think this one is very cool though. – Dave – 2013-08-23T01:10:22.157

Yeah, there's room for more, although as to whether it'll be portable or not also remains to be seen. Eventually if you want the sort of flexibility you're looking for, you need to build a machine language, which requires some form of conditional branching. – Joe Z. – 2013-08-23T02:38:36.940

4

No braces

Doesn't answer the question, but does answer its title - C without braces

main=195;

Works on x86 (32bit and 64bit). Does nothing exciting, but starts and terminates without error.

ugoren

Posted 2013-08-22T01:43:57.860

Reputation: 16 527