Home > OS >  Define custom syntax "until" in C
Define custom syntax "until" in C

Time:01-06

I expect I can do something like this

int i = 0;
until (i == 2){
  printf("yes\n");
  i  ;
}

Without telling detail about what until does, I'm sure reader know what is the algorithm from code above. Yes I know I can just use while(!condition){}.

The output will be:

yes
yes

So is possible I can achieve my goal?

I feel macro able to do this with define or something else. But I'm lack of knowledge about preprocessing directive syntax in C

#define until <what should I fill here>

CodePudding user response:

First of all, and I can't stress this enough: making your own secret, private language using function-like macros is a cardinal sin in C. It is perhaps the worst thing you can ever do.

Why? Because other people reading your code are expected to know C. They are however not expected to know your secret private macro language. Furthermore, they have absolutely no interest in learning your secret private macro language.

So please never do things like this in real programs.


That being said, you pretty much already answered the question yourself:

#define until(condition) while(!(condition))

Note that condition, being a function-like macro parameter, should be placed inside a parenthesis. This prevents accidental operator precedence bugs. For example if the caller passes until(i 1) then you want it to loop while(!(i 1)) and not while(!i 1).

CodePudding user response:

From Expert C Programming:

Macro use is best confined to naming literal constants, shorthand for a few well-chosen constructs. Define the macro name all in capitals so that, in use, it's instantly clear it's not a function call. Shun any use of the C preprocessor that modifies the underlying language so that it's no longer C.

Re: "I'm sure reader know what is the algorithm from code above."


No, it would confuse one even more as the keyword until is not part of the C language. It doesn't take much to type a few extra characters.

That being said, you could do:

#define until(condition) while(!(condition))

Compile your program with:

gcc -E -nostdinc main.c

to see what changes the preprocessor made.

But it would still be an abomination, and not something one would condone.

CodePudding user response:

Yes, you can use define for that. See the following example for the macro definition

#include <stdio.h>

#define until(x) while(!(x))

int main() {
   int i = 0;
   until (i == 2){
     printf("iteration %d\n", i);
     i  ;
   }
   return 0;
}

If you run it, the output would be

iteration 0
iteration 1

CodePudding user response:

Using until is useful in select cases.

Sometimes an algorithm or software contract uses until in its definition, so it is good to see that match in code.

Yet re-writing language semantics adds confusion and maintenance costs.

Consider a comment when until is needed.

int i = 0;

// until (i == 2) {
while (i != 2) {
  printf("yes\n");
  i  ;
}

CodePudding user response:

I don't know why you would do this, until is a mostly abandoned keyword for a reason. But this should work:

#define until(cond) while (!(cond))
  • Related