|
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 Thanks for playing along 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:
The definition of an iteration statement in the C language standard posted:
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:
hoo boy language lawyering hyperfocus is a trip lemeetellya
|
# ? Nov 1, 2023 14:20 |
|
|
# ? May 26, 2024 23:02 |
|
Good thing empty statements are valid.
|
# ? Nov 1, 2023 15:30 |
|
Thanks much for the excellent and detailed explanation! 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.
|
# ? Nov 1, 2023 16:23 |
|
You could also use if (1) { statements; } else (void) 0, but do/while is simpler.
|
# ? Nov 1, 2023 17:46 |
|
PDP-1 posted:Thanks much for the excellent and detailed explanation! If you add it then the semicolon is just optional for the user it's not really a big deal
|
# ? Nov 1, 2023 18:03 |
|
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.
|
# ? Nov 1, 2023 18:23 |
|
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.
|
# ? Nov 1, 2023 18:29 |
|
Very inspired by the planning committee that designed the C language that they included this intuitive syntax for such common situations.
|
# ? Nov 1, 2023 20:27 |
|
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?
|
# ? Nov 1, 2023 21:03 |
|
Are you suggesting that Kernigham and Ritchie made some bad decisions when creating C?
|
# ? Nov 1, 2023 21:45 |
|
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:
|
# ? Nov 1, 2023 21:57 |
|
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;
|
# ? Nov 1, 2023 22:09 |
|
pseudorandom name posted:Extra semicolons cause compilation errors, e.g. What's the problem?
|
# ? Nov 1, 2023 23:24 |
|
leper khan posted:What's the problem? the bug happens if the if doesn't use braces, the following is invalid: code:
|
# ? Nov 1, 2023 23:35 |
|
What I’m hearing is we should move to m4 for all our syntax transforming needs
|
# ? Nov 2, 2023 00:00 |
|
All good C/C++ compilers support statement expressions already anyway, you can just do #define TRACE(x) ({ blah blah blah })
|
# ? Nov 2, 2023 00:15 |
|
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?
|
# ? Nov 2, 2023 03:26 |
|
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
|
# ? Nov 2, 2023 07:44 |
|
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.
|
# ? Nov 2, 2023 10:58 |
|
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
|
# ? Nov 2, 2023 11:18 |
|
more falafel please posted:unbraced if blocks should be a warning
|
# ? Nov 3, 2023 02:02 |
|
You're right. It should be an error.
|
# ? Nov 3, 2023 02:49 |
|
Presto posted:You're right. It should be an error.
|
# ? Nov 3, 2023 03:03 |
|
goto fail;
|
# ? Nov 3, 2023 04:20 |
|
I would simply make whitespace indentation significant
|
# ? Nov 3, 2023 15:51 |
|
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.
|
# ? Nov 3, 2023 16:06 |
|
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
|
# ? Nov 3, 2023 16:10 |
|
chglcu posted:Especially when sickos put the opening brace on its own line. Our style guide at my job mandates Allman style braces.
|
# ? Nov 3, 2023 20:04 |
|
Dijkstracula posted:I would simply make whitespace indentation significant
|
# ? Nov 3, 2023 20:50 |
|
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.
|
# ? Nov 3, 2023 21:41 |
|
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
|
# ? Nov 3, 2023 21:45 |
|
StumblyWumbly posted:There is a non zero chance I am getting ghosted because I added brackets to an if statement in a coding exercise. If true you wouldn’t want to work there anyway
|
# ? Nov 3, 2023 22:46 |
|
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.
|
# ? Nov 3, 2023 22:56 |
|
Sweeper posted:If true you wouldn’t want to work there anyway
|
# ? Nov 3, 2023 23:00 |
|
leper khan posted:Opening on same, closing on its own line. Don't curry Also, sometimes opening and closing both on the same line if it's short.
|
# ? Nov 3, 2023 23:06 |
|
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.
|
# ? Nov 4, 2023 00:05 |
|
roomforthetuna posted:Sometimes you close and it's an else or the while of a do-while.
|
# ? Nov 4, 2023 06:36 |
|
Presto posted:No it isn't because those go on the line after the brace. This one gets it
|
# ? Nov 4, 2023 09:59 |
|
Presto posted:No it isn't because those go on the line after the brace. code:
|
# ? Nov 4, 2023 14:40 |
|
|
# ? May 26, 2024 23:02 |
|
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? That looks correct.
|
# ? Nov 4, 2023 14:56 |