-3
\$\begingroup\$

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.

\$\endgroup\$

closed as off-topic by user80551, ProgramFOX, Geobits, TimWolla, algorithmshark Apr 14 '14 at 17:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win." – user80551, ProgramFOX, Geobits, TimWolla, algorithmshark
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ To avoid braces, use goto statements. \$\endgroup\$ – Joe Z. Aug 22 '13 at 3:35
  • 2
    \$\begingroup\$ Just 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. \$\endgroup\$ – dmckee Aug 22 '13 at 3:55
  • 1
    \$\begingroup\$ Like 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? \$\endgroup\$ – ugoren Aug 22 '13 at 4:38
  • \$\begingroup\$ Uh... [! Nobody mention trigrams. ] \$\endgroup\$ – luser droog Aug 22 '13 at 4:40
  • \$\begingroup\$ 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. \$\endgroup\$ – Dave Aug 22 '13 at 10:12
6
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Dave Aug 22 '13 at 2:12
  • 1
    \$\begingroup\$ @Dave Yep, bending the rules is our specialty :D \$\endgroup\$ – Doorknob Aug 22 '13 at 2:13
  • \$\begingroup\$ 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. \$\endgroup\$ – Dave Aug 23 '13 at 0:36
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ nice, I didn't know you could take pointers to labels. \$\endgroup\$ – Dave Aug 23 '13 at 0:23
  • \$\begingroup\$ 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. \$\endgroup\$ – Dave Aug 23 '13 at 0:28
  • \$\begingroup\$ 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. \$\endgroup\$ – Joe Z. Aug 23 '13 at 0:31
  • \$\begingroup\$ 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). \$\endgroup\$ – Dave Aug 23 '13 at 0:34
  • \$\begingroup\$ Well it's the best we've got, basically. \$\endgroup\$ – Joe Z. Aug 23 '13 at 0:43
4
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.