Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Zopotantor posted:

This has the same problem as including a semicolon at the end of a statement-like macro: users can leave off the semicolon when calling it. AND THEY WILL :argh:

Thanks for playing along :3: In this particular case, despite TRACE() appearing like a function call, users would have to leave off the semicolon for the macro-expanded code to be syntactically valid! With the user's "statement"-terminating semicolon so reasonably added, you'd end up with something that looks like:

C code:
if (1 < 2)
  { ... } ;
else 
  { ... } ;
This is why the idiom, weird as it is, has to be this way: we need a way to splat out the contents of the macro in a new block scope but also wrapped in something that looks like an expression statement. And the do-while construct is the only way that we can do it!

The definition of an iteration statement in the C language standard posted:


* while ( expression ) statement
* do statement while ( expression ) ; <-- hay guys
* for ( expressionopt ; expressionopt ; expressionopt ) statement
* for ( declaration expressionopt ; expressionopt ) statement


Notice that a do-while loop is terminated with the semicolon but no others are - this is why, if anyone was wondering if we could write the macro with a nominally-equivalent single-iteration for-loop wrapping the new scope, as in:

C code:
    5 #define WARN(msg)  for (int i = 0; i < 1; i++) { if (VERBOSITY > 10)  fprintf(stderr, "WARN:  " msg "\n"); }
    6 #define DEBUG(msg) for (int i = 0; i < 1; i++) { if (VERBOSITY > 30)  fprintf(stderr, "DEBUG: " msg "\n"); }
    7 #define TRACE(msg) for (int i = 0; i < 1; i++) { if (VERBOSITY > 50)  fprintf(stderr, "TRACE: " msg "\n"); }
You would expect from the language grammar that this would have the same terminating semicolon issue!

hoo boy language lawyering hyperfocus is a trip lemeetellya

Adbot
ADBOT LOVES YOU

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.
Good thing empty statements are valid.

PDP-1
Oct 12, 2004

It's a beautiful day in the neighborhood.


Thanks much for the excellent and detailed explanation! :tipshat:

You wrap your code in do{}while(0) so that it works syntactically as you want with semicolons, but since that structure doesn't do anything useful it gets optimized out to nothing in the actual output. Other constructs like if statements don't work the same way with semicolons so it has to be do-while.

pseudorandom name posted:

Well, you screwed up your examples, it should be do { } while (0)

True, I was typing it out from memory and auto-included the semicolon by force of habit. I didn't understand the importance of it not being there so it didn't jump out at me. I get why you don't want it there now.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You could also use if (1) { statements; } else (void) 0, but do/while is simpler.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

PDP-1 posted:

Thanks much for the excellent and detailed explanation! :tipshat:

You wrap your code in do{}while(0) so that it works syntactically as you want with semicolons, but since that structure doesn't do anything useful it gets optimized out to nothing in the actual output. Other constructs like if statements don't work the same way with semicolons so it has to be do-while.

True, I was typing it out from memory and auto-included the semicolon by force of habit. I didn't understand the importance of it not being there so it didn't jump out at me. I get why you don't want it there now.

If you add it then the semicolon is just optional for the user it's not really a big deal

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

leper khan posted:

If you add it then the semicolon is just optional for the user it's not really a big deal

Missing semicolons can mess up auto indentation in editors.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Zopotantor posted:

Missing semicolons can mess up auto indentation in editors.

Extra semicolons cause few to no issues. Typically a generated warning about a null statement.

StumblyWumbly
Sep 12, 2007

Batmanticore!
Very inspired by the planning committee that designed the C language that they included this intuitive syntax for such common situations.

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

StumblyWumbly posted:

Very inspired by the planning committee that designed the C language that they included this intuitive syntax for such common situations.

"Planning committee"? "Designed"? What do you think this is, f*cking Ada?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Are you suggesting that Kernigham and Ritchie made some bad decisions when creating C?

pseudorandom name
May 6, 2007

leper khan posted:

If you add it then the semicolon is just optional for the user it's not really a big deal

leper khan posted:

Extra semicolons cause few to no issues. Typically a generated warning about a null statement.

Extra semicolons cause compilation errors, e.g.

code:
#define MACRO(x) do { (void) x; } while (0);

void test(int a, int b)
{
        if (a)
                MACRO(b);
        else
                MACRO(a);
}

StumblyWumbly
Sep 12, 2007

Batmanticore!

Plorkyeran posted:

Are you suggesting that Kernigham and Ritchie made some bad decisions when creating C?

The post above you did not end in a semicolon, so I am reporting this post;
I am a C compiler;

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

pseudorandom name posted:

Extra semicolons cause compilation errors, e.g.

code:
#define MACRO(x) do { (void) x; } while (0);

void test(int a, int b)
{
        if (a) {
                MACRO(b);
        }
        else {
                MACRO(a);
        }
}

What's the problem?

b0lt
Apr 29, 2005

leper khan posted:

What's the problem?

the bug happens if the if doesn't use braces, the following is invalid:
code:
if (true) (void)0;; else {}

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

What I’m hearing is we should move to m4 for all our syntax transforming needs

pseudorandom name
May 6, 2007

All good C/C++ compilers support statement expressions already anyway, you can just do #define TRACE(x) ({ blah blah blah })

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

pseudorandom name posted:

All good C/C++ compilers support statement expressions already anyway, you can just do #define TRACE(x) ({ blah blah blah })

Cool, how does that evaluate if I put it in an unbraced if-else block with a semicolon after it?

more falafel please
Feb 26, 2005

forums poster

Jabor posted:

Cool, how does that evaluate if I put it in an unbraced if-else block with a semicolon after it?

unbraced if blocks should be a warning

Beef
Jul 26, 2004

pseudorandom name posted:

All good C/C++ compilers support statement expressions already anyway, you can just do #define TRACE(x) ({ blah blah blah })

This is the biggest reason why I hate that MSVC exists. Make statement expressions part of the language already ffs.

Qwertycoatl
Dec 31, 2008

Beef posted:

This is the biggest reason why I hate that MSVC exists. Make statement expressions part of the language already ffs.

It took 15 years between designated initialisers being in the standard and MSVC supporting them, I wouldn't hold my breath

qsvui
Aug 23, 2003
some crazy thing

more falafel please posted:

unbraced if blocks should be a warning

:wrong:

Presto
Nov 22, 2002

Keep calm and Harry on.

You're right. It should be an error.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Presto posted:

You're right. It should be an error.

:yeah:

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

goto fail;

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!


I would simply make whitespace indentation significant

chglcu
May 17, 2007

I'm so bored with the USA.
While I agree with requiring braces on a practical level, aesthetically it just irks me every time. Especially when sickos put the opening brace on its own line.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

chglcu posted:

While I agree with requiring braces on a practical level, aesthetically it just irks me every time. Especially when sickos put the opening brace on its own line.

Opening on same, closing on its own line. Don't curry

Presto
Nov 22, 2002

Keep calm and Harry on.

chglcu posted:

Especially when sickos put the opening brace on its own line.

Our style guide at my job mandates Allman style braces. :negative:

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Dijkstracula posted:

I would simply make whitespace indentation significant

:guillotine:

StumblyWumbly
Sep 12, 2007

Batmanticore!
There is a non zero chance I am getting ghosted because I added brackets to an if statement in a coding exercise.
The point here is people who don't use brackets are cowards at heart.

more falafel please
Feb 26, 2005

forums poster

chglcu posted:

While I agree with requiring braces on a practical level, aesthetically it just irks me every time. Especially when sickos put the opening brace on its own line.

I also program on an 80x24 VT100 and not a vertical monitor that can show around 100 lines on a screen at a time

Sweeper
Nov 29, 2007
The Joe Buck of Posting
Dinosaur Gum

StumblyWumbly posted:

There is a non zero chance I am getting ghosted because I added brackets to an if statement in a coding exercise.
The point here is people who don't use brackets are cowards at heart.

If true you wouldn’t want to work there anyway

chglcu
May 17, 2007

I'm so bored with the USA.

more falafel please posted:

I also program on an 80x24 VT100 and not a vertical monitor that can show around 100 lines on a screen at a time

It's not really the wasted space - though that's annoying, too - it just looks really ugly to me after like 20 years of using K&R.

StumblyWumbly
Sep 12, 2007

Batmanticore!

Sweeper posted:

If true you wouldn’t want to work there anyway
Very true

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

leper khan posted:

Opening on same, closing on its own line. Don't curry
Opening on same, closing on *the start* of a line. Sometimes you close and it's an else or the while of a do-while.
Also, sometimes opening and closing both on the same line if it's short.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Having a blank line before a conditional block is way better for at-a-glance understanding than having a nearly-blank line after the if statement but before the actual code, which is what putting the opening brace on its own line forces.

Presto
Nov 22, 2002

Keep calm and Harry on.

roomforthetuna posted:

Sometimes you close and it's an else or the while of a do-while.
No it isn't because those go on the line after the brace. :colbert:

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

Presto posted:

No it isn't because those go on the line after the brace. :colbert:

This one gets it

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Presto posted:

No it isn't because those go on the line after the brace. :colbert:
Yes it still is sometimes an else after a brace, you're just saying you should format that like a maniac. You appear to be suggesting this monstrosity?
code:
if (banana) {
  doStuff();
}
else {
  doOtherStuff();
}

Adbot
ADBOT LOVES YOU

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

roomforthetuna posted:

Yes it still is sometimes an else after a brace, you're just saying you should format that like a maniac. You appear to be suggesting this monstrosity?
code:
if (banana) {
  doStuff();
}
else {
  doOtherStuff();
}

That looks correct.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply