Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on What is the meaning of "short circuit" operators?

Post

What is the meaning of "short circuit" operators?

+6
−0

When reading about various operators used by programming languages, the term "short circuit behavior" is often used. For example in this C code:

int a = 0;
a && b++

Someone explained that b++ is never executed because the logical AND operator "short circuits". What do they even mean with this?

Assuming I'm a layman at electronics (but not necessarily at programming), the association I get when hearing "short circuit" is something like connecting + directly to - on a battery, resulting in a spectacular failure such as cables burning up. And that doesn't seem like something I would want to happen to my program...

Why is it called "short circuit behavior"? What's the analogy and how is it helpful in understanding how certain operators work?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Cables don't blow up from a short (5 comments)
Cables don't blow up from a short
matthewsnyder‭ wrote 8 months ago

Usually cables don't blow up from a short, especially if you're doing layman things. Shorts will allow electricity to be consumed rapidly. With grid AC (as in house wiring) there are already circuit breakers that will automatically turn off when current goes too high. With batteries, the battery will quickly go flat and heat up a bit. Decent modern batteries tend to also automatically stop current when they detect a short, although there may be some cheaper, low quality ones that catch fire and/or explode.

If you are building your own contraptions you can end up with enough current to actually cause an explosion, but if you're doing that I wouldn't call you a layman.

Lundin‭ wrote 8 months ago · edited 8 months ago

matthewsnyder‭ Here I'm trying to picture what a layman would imagine a short circuit to be. For example how I used the term myself, before I started working in the electronics industry. Think of some robot in sci/fi fiction having a malfunction, the others characters will often say something like "he has short-circuited". As for a battery cell or a pack of them, it is pure chemistry with no intelligence. If you short the poles, any conductor not designed for the resulting current will burn to a crisp. You may add supervising electronics to prevent catastrophic events. And when those electronics fail, the result is fire and pyrotechnic effects. Anyway, it is not important here.

matthewsnyder‭ wrote 8 months ago

I know, I just think you're hamming it up a little too much :)

Decades ago it might have been like you describe, but not anymore really. Especially in electronics, where you work with low voltages, it's hard to make things explode.

Battery packs these days do have intelligence with built in circuits that prevent overload.

I'm not saying this for idle chatter. This is kind of an outdated mental picture and many laymen grew up long after it wasn't the case. It would be nice to not contribute to a misapprehension of "if it doesn't explode everything is fine".

Lundin‭ wrote 8 months ago · edited 8 months ago

matthewsnyder‭ Sorry but that is uninformed. Look, I work with electronics design - I have been involved in many battery pack and charger designs, including writing firmware for one in place of a BMS. I have seen malfunctioning NiMH and Li/Ion batteries burn up several times, due to malfunctioning, polarity inversion or general rough/careless use. And no, there is no guarantees that there is built-in protection - the flood of electronic junk from China hitting the world has very diverse quality. Plain cells have no protection. In packs, the protection could be questionable, they could cheap out and skip thermistors etc. Just a month ago, a house near my home town burnt down and the cause was a Chinese e-bike charger. And it wasn't really that long ago when a reputable vendor like Samsung had major design problems. Meaning it will happen again and again. Batteries are pretty f-in' scary and we shouldn't dismiss the risks involved with them.

matthewsnyder‭ wrote 8 months ago

I don't disagree that poorly made batteries can be dangerous, but I think that maybe an extended discussion of battery discharge related hazards can be moved to a more appropriate site, like electrical engineering, rather than a question about boolean operators in programming.

This appears to have gotten a bit involved. My point was to give the feedback of: You could probably just remove the stuff about cables exploding from the question, it would make it shorter and easier to read quickly, and would take away much from the main question.

Educating people about electrical safety is laudable, but it seems like a tangent here. Anyway, just my $0.02, no hard feelings here if you choose to ignore it.