Loading video...

Video Failed to Load

Go Home

This is a common C bug. You write a MAX macro, then call MAX(m++, n++). The preprocessor pastes the text, so it becomes (m++ > n++) ? m++ : n++. The smaller one increments once. The larger one increments twice. Stanford CS107, lecture 12

29,707 views • 2 months ago •via X (Twitter)

30 Comments

TempeTeaParty's profile picture
TempeTeaParty2 months ago

This isn't so much a coding bug as it is a comprehension problem on the part of the programmer. How can this be common?

FM's profile picture
FM2 months ago

It's interesting that the `inline` keyword is still not standard in C. It could've saved a lot of headache.

Max Sanna's profile picture
Max Sanna2 months ago

I would never dream, not even by mistake, of using this macro in this way

Ali 🦁's profile picture
Ali 🦁2 months ago

This isn't a 🐞 this is expected preprocessor behavior! Some compilers might resolve the issue though

jhonny's profile picture
jhonny2 months ago

Use Claude or GPT sol or Grok

proc0's profile picture
proc02 months ago

You don't use ++ more than once in the same statement, it's undefined behavior.

Dark Goldenrod's profile picture
Dark Goldenrod2 months ago

I almost never use inline ++ or --. Yeah, it is an entire 3 seconds faster to type: arr[ idx++ ] = item than: arr[ idx ] = item; idx++; but it's slower to read every time.

Zavian's profile picture
Zavian2 months ago

Become a teacher become a dev solana:5yC9BM8KUsJTPbWPLfA2N8qH1s9V8DQ3Vcw1G6Jdpump, become a ai video creator, what moore?

Benathon's profile picture
Benathon2 months ago

I would never do that

Mickey Stewart's profile picture
Mickey Stewart2 months ago

I remember watching this Stanford cs class on iTunes U when all these universities first started to make these available. These were awesome and So was Harvards CS50.

Advanced SuperSilly Intelligence's profile picture
Advanced SuperSilly Intelligence2 months ago

The human suffering needs to end. Thank goodness for AI.

ashrealite's profile picture
ashrealite2 months ago

I cant believe we whiteboard dimwitted shit like this in supposed prestigious universities.

∂≈∞ --- ∫∫∫∆∏∑∫∫∫ π⃜÷⃜×⃜•⃜|⃜¶⃜∆⃜$⃜%⃜|⃜+⃜'s profile picture
∂≈∞ --- ∫∫∫∆∏∑∫∫∫ π⃜÷⃜×⃜•⃜|⃜¶⃜∆⃜$⃜%⃜|⃜+⃜2 months ago

Master tetsuo, your posts brighten my day. I am grateful that I discovered your account.

mutex.up's profile picture
mutex.up2 months ago

That's not a C bug, that's simply correct text substitution by the preprocessor. Use the -E flag to check the preprocessed code if unsure. "inline" functions are safer, and "const" constants can be seen by a debugger. Understand your tools.

Matcha's profile picture
Matcha2 months ago

I guess this is why I'm killing macros and replacing with __inline__ functions. AS I UNderstand it, the __inline__ functions expand to code within the calling location just like a macro, but without all the macro gotchas (like this one).

Augustus's profile picture
Augustus2 months ago

Today, you need a really good reason to use defines. Inline functions > defines

smyanthota's profile picture
smyanthota2 months ago

gcc statement expressions with typeof kill the double-eval, or just use an inline function and move on

Abbas Banu Vohra's profile picture
Abbas Banu Vohra2 months ago

thanks thats why i tRUST because it (its designers) know that increment/decrement ops are pain in the ... : abv_bay_yanlis_8647_DMGINXL

Unique.Aioli8518's profile picture
Unique.Aioli85182 months ago

It's a feature, not a bug (at least not a C bug, anyway)!

Youssef Afella's profile picture
Youssef Afella2 months ago

Everyone think using a chair is safe but watch what happens when you beat the shit of someone with it

zaphodicus's profile picture
zaphodicus2 months ago

It is not a bug but a programmer mistake. A function is much better and the compiler will inline it automatically is needed. The macro system, even if I love it, is full of traps

ElectraX's profile picture
ElectraX2 months ago

That's not a bug.

泰多宝's profile picture
泰多宝2 months ago

the gcc statement-expression fix assigns each argument to a __typeof__ temp first, so the side effect happens exactly once.

gb's profile picture
gb2 months ago

Except it's not that common... unless you're a beginner and don't know it's a macro (despite it being written in capitals) ofc.

Matthew's profile picture
Matthew2 months ago

Ibit++ known zeros

kimiyoribaka's profile picture
kimiyoribaka2 months ago

I don't like mid-expression incrementing to begin with, but doing multiple at once seems like it's really asking for trouble.

Wade Noam Sayen's profile picture
Wade Noam Sayen2 months ago

These curated into a collection is massive win

Orion Night's profile picture
Orion Night2 months ago

every C programmer learns this exactly once

Path of Totality's profile picture
Path of Totality2 months ago

Maybe our preprocessors need to be a little smarter 😆

Steve Mac's profile picture
Steve Mac2 months ago

I love these little clips!

Related Videos