This one looks obvious… until it isn’t 👇 int i = -3; unsigned int j = 5; printf("%d\n", i < j); What would you expect this to print? Most people would say 1 (true). But the actual output is: 0 So what’s going on here? It comes down to how C handles comparisons between signed and unsigned integers. When i (which is -3) is compared with an unsigned value, it doesn’t stay negative. Instead, it gets implicitly converted to an unsigned integer. That transformation turns -3 into a very large positive number (on a 32-bit system, it becomes 4294967293). So the comparison the compiler actually performs is: 4294967293 < 5 → false Why this matters: This kind of implicit conversion is subtle and easy to miss, but it can introduce serious bugs — especially in: • Embedded systems • Kernel / low-level code • Boundary checks and loop conditions It’s one of those cases where the code looks right, compiles fine, and still behaves unexpectedly. Have you run into bugs caused by signed vs unsigned mismatches? #cprogramming #embeddedc #systemsprogramming #lowlevel #debugging #codingpitfalls #softwareengineering #linux #cplusplus #techinsights
Signed vs Unsigned Integer Comparison Gotcha in C
More Relevant Posts
-
What really happens when you compile a C program using GCC? Most of us just run: 👉 gcc main.c -o main But behind this single command… there are 4 powerful stages working step by step 🔹 Preprocessing Removes comments, expands macros, and handles header files. 🔹 Compilation Converts high-level C code into assembly instructions. 🔹 Assembly Transforms assembly into machine-level object code. 🔹 Linking Combines object files and libraries to create the final executable. From ".c" → ".i" → ".s" → ".o" → executable That’s the real journey of your code! Understanding these stages helps in: ✔ Debugging errors ✔ Optimizing performance ✔ Writing better embedded code #EmbeddedSystems #GCC #Linux Toolchain GNU Project Compiler
To view or add a comment, sign in
-
-
One glance, instant truth about a backup's footprint. Here's a one-liner that shows name, size, and mtime in one go 🔥 `stat -c '%n %s bytes %y' backup.tar.gz` In -c, format string. %n = name, %s = size in bytes, %y = last modification time. You're auditing a nightly backup; you need to confirm it's the right file, the size matches, and the timestamp is fresh. This prints a single line you can paste into notes or tickets ⚡ The terminal doesn't bluff: exact, fast, repeatable checks. Try it in your backup workflow and drop your output in the comments. #linux #terminal #oneliner #filesystem #stat #devops #sysadmin #programming #softwareengineering #opensource #productivity #scripting #backup #datamanagement #buildinpublic
To view or add a comment, sign in
-
-
No more chasing 'Exception' in aging logs. The terminal just handed you instant context. 🔥 `time grep -R 'Exception' /var/log 2>/dev/null` Time = duration, grep -R = recursive search, 'Exception' = literal, /var/log = log root, 2>/dev/null = hide errors. You're debugging a flaky service at 2AM. Run this on the prod box to surface every exception with timestamps, in one go. One oneliner, instant context, faster triage. 🔥 What command would you pair this with? Drop it below. #linux #terminal #bash #commandline #devops #sysadmin #programming #softwareengineering #productivity #loganalysis #opensource #troubleshooting #buildinpublic #learntocode
To view or add a comment, sign in
-
-
One important realization while working with multithreading in C++: std::thread vs pthread is not about which creates threads — both ultimately rely on the OS. It’s about how you interact with them. At a glance: C++ → std::thread → pthread → OS threads So what actually changes? With pthread: • Low-level C API • Manual handling (void*, return codes) • More control, more room for mistakes With std::thread: • Modern C++ abstraction • RAII-based safety • Strong typing • Exception handling • Cleaner, more readable code The key shift: You’re not switching away from system threads you’re switching to a safer and more expressive interface to use them. That’s why modern C++ feels powerful: It doesn’t remove control it wraps it intelligently. Rule of thumb: Use pthread when you need fine-grained system-level control Use std::thread for almost everything else Curious: Did you start your journey with low-level pthread, or jump straight into modern C++ threading? #cpp #cplusplus #multithreading #concurrency #linux #softwareengineering #programming
To view or add a comment, sign in
-
-
Missed disk space? Find the culprits in seconds. One line does the audit. ⚡ `du -sh * | sort -rh | head -n 5` — this is the audit command. du -sh lists human-readable sizes for each item. sort -rh sorts by size, largest first. head -n 5 picks the top five results. Real use: you're on a prod server at 2AM and space is burning. You run this in the project root to spot the biggest hogs and reclaim space fast. The terminal is a superpower; tiny one-liners save hours. Run it right now. Tell me what you find. 🐧 #linux #terminal #bash #commandline #devops #sysadmin #programming #softwareengineering #developer #coding #opensource #productivity #automation #buildinpublic #learntocode
To view or add a comment, sign in
-
-
As I can see most of Software failures on 2026 aren't due to null pointers (as rust dev could argue, but rust is great in that sense), they are mainly caused by NOT RESPECTING THE BASIC RULES !! - Uniformity and standardization of paths - Messin with dynamic link libraries - Backward compatibility - Keep It Simple Here another example https://lnkd.in/dTwczqsV #SoftwareQuality #Programming
To view or add a comment, sign in
-
🚨 Most engineers hear deadlock and think it means only one thing: “Two threads waiting forever.” But in real Linux systems, deadlock has many faces… and some are harder to detect than others. Here are 4 dangerous lock failures every systems engineer should know: 1️⃣ Circular Deadlock Classic case. Thread A holds Lock 1, waits for Lock 2 Thread B holds Lock 2, waits for Lock 1 Nobody moves. 2️⃣ Recursive Deadlock A thread locks the same non-recursive mutex twice. It blocks… waiting on itself. Yes, a thread can deadlock itself. 3️⃣ Owner-Death Deadlock A thread crashes while holding a mutex. Other threads keep waiting unless you use robust mutex recovery. The lock holder is gone… but the lock remains. 4️⃣ Livelock Looks active, but no progress. Two threads keep retrying, backing off, yielding… forever. CPU busy. Work zero. 💡 What I learned in Linux debugging: Not every freeze is a deadlock Not every active system is healthy Logs + thread dumps + lock order analysis save hours 👉 Concurrency bugs are scary because the code looks correct. Until timing proves otherwise. 💭 Which is harder to debug in your experience: deadlock or race condition? #Linux #Deadlock #Concurrency #Multithreading #EmbeddedLinux #SystemProgramming #Pthreads #Mutex #LowLevelProgramming #Debugging #SoftwareEngineering #OperatingSystems #RaceCondition #RealTimeSystems #TechLearning
To view or add a comment, sign in
-
How do I run a C/C++ program on Raspberry Pi? Raspberry Pi is a Linux computer, so running C or C++ on it is much closer to developing on a small Debian-based PC than programming a bare-metal microcontroller. Modern Raspberry Pi boards such as Raspberry Pi 3 Model B, 3 Model B+, 4 Model B, Zero 2 W, and 5 all use Arm-based processors, while Raspberry Pi OS remains the standard software starting point for most projects. Raspberry Pi OS is available in desktop and Lite editions, and the Lite edition is command-line only, which is often ideal for embedded C/C++ work. This means the usual workflow is simple: install Raspberry Pi OS, open a terminal or connect over SSH, install the compiler tools you need, write your .c or .cpp file, compile it with gcc or g++, and run the generated executable from the shell. For headless setups, Raspberry Pi’s official documentation supports preconfiguring SSH in Raspberry Pi Imager, which is useful when you want to develop from another computer. View more details, please click: https://lnkd.in/gQs4etVg #RaspberryPi #Cprogram #RaspberryPiZero2W #RaspberryPi3 #RaspberryPi4 #RaspberryPi5
To view or add a comment, sign in
-
-
In this part, I have looked into how memory taging operation can help us build a lock to sync my block layer's operations across multiple processes and contexts i.e both kernel/interrupt context. - Started of by making my existing block_read/block_write to use the batching queue of virtio, now i am able to make multiple requests without hardcoding any index with in the queues, my current example is sequential and is called only from kernel context, so my code works fine, but i reviewed other possible cases, and my codes is currently racy and wont be good enough for those cases. Eg: my global block request count, request index array all of them need some sort of syncing - Checked how linux kernel handles this cases, ie mutex/spinlocks used for this, turns out linux uses a spinlock to sync the virtio queue locking and other operations are async based on irq, in my case i would need spinlocks for all three cases, but i have also thought if mutex can be usefull at all, as it will require help from the scheduler to yeild off when a process is not able to get hold of the lock, and similar case in spinlock where we need to mask all interrupts so we will never be swapped off when within a spinlock critical section. - Looked into how we can implement a spinlock in ARM, ie to use check-and-swap atomically, learned about the LDREX, which tags a memory location and STREX read it back, and be sure that nothing changed in between, if something changed, STREX would fail, we can use this to implement our spinlock. I will be implementing this next week and test with an example userspace process trying to read from block using the locking. I have streamed the entire session on youtube. https://lnkd.in/emj-QqRw https://lnkd.in/eDspSP3Y #kernel #security #debugging #arm
Kernel Development | C | Day-22
https://www.youtube.com/
To view or add a comment, sign in
-
Your docker ps output is a noise storm. You need the map, not the noise. 🔥 Command: docker ps --format 'table {{.Names}} {{.Status}} {{.Ports}}' Three fields: Names, Status, Ports. {{.Names}} prints the container name; {{.Status}} shows Up or Exited; {{.Ports}} lists the port mappings. Real use case: on-call at 2AM, a misrouted proxy triggers alerts. This command shows who’s Up and what ports they expose, at a glance. Why it matters: fast triage that proves the terminal is a superpower. ⚡ Try it. Drop your output in the comments. #linux #terminal #docker #commandline #devops #sysadmin #programming #opensource #productivity #automation #tooling #kubernetes #cloudcomputing #buildinpublic
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development