Skip to playerSkip to main contentSkip to footer
  • 6/11/2025
Hey coders! Ever hit a wall with a program that just won?t work? In this video, I?m spilling the beans on caveman debugging?a super simple, no-fuss way to hunt down bugs in your C++ code (or any language, from assembly to Python)! No need to wrestle with complex debuggers; this method is all about using print statements to trace what your code is doing, step by step.

I?ll show you a real C++ program where things go wrong (think integer overflows and mystery crashes) and walk you through how to sprinkle in cout statements to spot the problem fast. You?ll learn how to label decision points like loops and if-statements, break down complex expressions with temporary variables, and format your debug output so it?s easy to read. Plus, I share a neat trick called short circuiting to toggle your debug prints on or off without deleting them?saving you tons of time!

Whether you?re a beginner just starting out or a pro debugging a massive codebase, caveman debugging is a lifesaver. It?s quick, it?s intuitive, and it fits my ?let?s figure this out? vibe perfectly. Stick around to see how I catch a sneaky bug in a for-loop and fix it in minutes.

Why watch?

* Learn a beginner-friendly debugging technique that works in any language.

* See real code examples with clear, relatable explanations.

* Get tips to make your debug output readable and avoid hours of frustration.

* Discover how to short circuit print statements for reusable debugging.

If you?re ready to level up your coding game and squash bugs like a champ, smash that play button! Don?t forget to subscribe and hit the bell for more coding tutorials, from beginner hacks to advanced tricks. Drop a comment if you?ve ever used caveman debugging or have a bug you need help with?I read every one! Check out my next video on using a proper debugger for even more debugging goodness.

Scan the QR code on-screen or visit my website (linked below) for more tutorials, services, and coding resources. Let?s keep the coding community growing?your support means the world! #CavemanDebugging #CodingTips #DebuggingHacks


Thanks for watching!

Find us on other social media here:
- https://www.NeuralLantern.com/social

Please help support us!

- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Transcript
00:00Hey everybody, in this video I'm going to try to familiarize you with a concept called
00:05Caveman Debugging. It's just as bad as it sounds, but it's really really really useful.
00:16So suppose for the sake of argument you're writing a program. I'm going to show a C++
00:21program here, but this concept is not just for C++ debugging. It's also for assembly and
00:27any other kind of language that you can come up with. But suppose I have a program and I'm trying
00:33to figure out why it's not working. So if you look at this program right here that I've kind of written
00:38up already, we have a main function here and we have like a little hello message. That's not my name.
00:44I love that name though. And we have a variable a and then we just sort of do some nonsense to it
00:50to try and compute a value for a. When I say nonsense, I mean literally I just made up
00:57instructions. The point of this video is not to show you some advanced program. It's just to show
01:01you that, you know, this is how you can possibly debug a program that has gone awry. So you can
01:07imagine at home that this code you're seeing right here might be your larger project, your larger
01:13code base, whatever it is that you're doing that is not working for some reason and you're not sure why.
01:19So let me run the program real fast. It basically is just like a little for loop and then it just sort of,
01:25you know, not randomly, but it just kind of increases the value of a for no reason
01:30a bunch of times. And then it calls a function that will crash later.
01:35So let me show you what happens when we try to run this program. Okay.
01:40Okay. I want to go clear and make run. If you don't know how to do a make file or compile or link or
01:46anything like that, then go see my other videos. So clear and make run. What did I do wrong?
01:54Temp result was not declared in this scope. Okay. Well, this is now a video on reading compiler errors.
01:59Line 59. I probably left something out. Oh, because I just changed it. I forgot to put it back in there.
02:04Return temp result. We should just return input times two and I'm just making this up. It's not really like
02:10a valid algorithm. Okay. So, uh, we run our program. We, uh, print the final result and we realize for
02:18some reason the final result is wrong. So I don't, I mean, I just made this up, so I don't really know
02:23what the right answer is, but just imagine that you are looking at some sort of an output, some
02:27sort of a final state, maybe a crash and you realize, oh, my program doesn't work, but it's
02:32too complicated to figure out caveman debugging to the rescue caveman debugging, uh, kind of is as bad
02:40as it sounds. It's just really, really simple, but it's also really useful, especially if you just want
02:45to do a quick little, you know, check on your code to see what's going on. You don't want to dive deep
02:50into a full on debugger or do something more advanced. Uh, it literally is just printing a
02:56lot of things while your program is running. I do it all the time. I usually don't even go to a debugger
03:02unless I really get in trouble with like some huge mess. Um, okay. So the first thing I'm going to say
03:08is in this for loop, we, uh, you know, we're kind of like iterating it a hundred times.
03:15And so probably this is a good idea to announce that we're iterating in the for loop. So I'm just
03:20going to go see out in C plus plus, you know, we have the C out, uh, object that we can use to
03:25stream, uh, texts to standard output, uh, which usually goes to the terminal. So use whatever
03:31construct you're using for your particular language. So we're gonna do C out. And, uh,
03:38I should say also, if you're programming in assembly, if you are programming in assembly,
03:43it's a little bit more complicated than just writing a C out. You probably have to
03:47have some, uh, pre-computed messages, and then you can use a library to print a number to the screen
03:52or use print F to print stuff to the screen, but you can do it just takes a little bit longer.
03:58Um, so anyway, conceptually, I'm just going to say, this is the first iteration of my loop.
04:02So I'm going to sort of announce it. I'm going to say main, and then I'm going to call it main four,
04:08or how about primary four or first four, there's only one four, but I want to try to label the for
04:13the for loop. And, um, maybe instead of, uh, leaving those parentheses blank, I could say iteration
04:21number I right there, or maybe I could do iteration I. That'd be pretty good. Then I just have to
04:29replace the I with the actual variable I so that, uh, the I keeps increasing. So I'm just going to like
04:34to do another stream operator here and I'm going to say I, then I'll print a new line. So we've got,
04:40uh, you know, one iteration of our loop. Maybe I should also print what is the current value of
04:47a because the point of this, you know, weird nonsense code is we're coming up with a value
04:52for a, right? So I should just print what a is at the top of each iteration of the for loops. I'm going
04:57to say a is equal to, and then do another a at the end of it. And then maybe at the bottom of the
05:04for loop, when the for loop is actually ending one of its iterations, I can just say, uh, we're at the
05:10bottom of the iteration. So maybe I'll put to end iteration and then print the final value of a.
05:16And then at the top, I'll say, begin iteration. At this point, you might start to feel like, Hey,
05:22aren't you using too many words here? Like, why can't I just print a bunch of numbers?
05:27I mean, you could, but when you're coding, you need all the brain power you can get
05:31to not just code, but to debug coding is already hard enough without making it harder,
05:36harder for yourself than you need it to be. So, you know, I see people all the time. They're
05:41trying to debug their code and they just like start printing numbers. And after not very long at all,
05:46they're looking at the debug output and they're just like, what did that number mean? Where is the
05:50number I'm looking for? It's just a bunch of numbers. It's harder to debug. When you do it
05:54that way, do it the nice and pretty way. It just costs you a couple of extra keystrokes to do,
05:59you know, nice letters and new lines and things, nice words, labels. Uh, isn't that still a lot
06:05better than debugging for an extra six hours because you can't really understand what you're seeing.
06:11Anyway, let me show you what this looks like so far with just those two lines added.
06:14Now we can kind of see, all right, all right, on iteration number 61, where I was 61,
06:21then, uh, you know, this was the, uh, first value of A. And then after that, this was the second value
06:27of A. You know, when we were finished with the iteration, it seems to have gone up by a certain
06:31amount. You know, it was like a negative blah, blah, blah, 836 and then negative blah, blah, blah,
06:36708. So it kind of increased in value. All right. That's a little bit helpful.
06:39Uh, now is a really good, uh, place to, uh, print our decision points. So anytime in your code where
06:47you have an if or an else or a function call or anything where you are kind of like, you know,
06:53making a decision or processing your data in some way, it's probably a good idea to announce
06:58your decisions. So I could say up here, I can go, maybe I'll say, uh, main
07:04first four and then I'll call it, uh, maybe I'll do a double colon there and I'll say
07:13top if just to make it easy to read that I'm kind of like at the first if statement.
07:19Um, maybe I'll even put parentheses. I'll say a is more than 20 just to kind of like name what I'm
07:25actually doing. And then I could replace the A with the actual value of A. So I could say
07:30a like that. And maybe, uh, uh, when I'm reading this later, I might want to kind of actually see
07:37the variable name. So I could say, uh, you could just like put a as a string and then maybe its
07:43value in parentheses or something like that. Do a new line. Um, and then maybe after that top thing,
07:51I'll say, uh, true to indicate that we're inside of the, the if statement. And then, uh, maybe at the
07:59bottom of that, if statement, we'll just say, um, a is now true. And we'll say like a is now,
08:10you know, whatever the new value of a was. So just basically we're announcing that we decided to go into
08:14this if block. Uh, and then we announce, uh, what we ended up changing A to. And also you probably,
08:20anytime you see a compound, uh, expression like this, where there's like, you know, B times something
08:25or A plus something, or just, you know, a big part of multiple parts of the expression, any expression
08:30that's more than just like one variable or one number, it's probably a good idea if you break that
08:34up into temporary variables too, and then, um, print out each variable. Uh, and I know that's a lot of
08:40stuff to read, but again, it beats debugging for an extra six hours. I'm going to leave that be for
08:45now though. Leave that be. And, um, I'm just going to continue adding comments into the rest of the
08:51codes. I'm going to do the second if block here. I'm going to say, uh, we're in the first floor.
08:57We're in the bottom. If, uh, more than 20 true. And then we'll print out what is the value of a after
09:05we're finished. We'll say a is now, is now, you know, whatever value for a,
09:14and then I'll do the same thing for this function call. Cause you know, when I call this function,
09:18it's going to change the value somehow. So we're going to do C out, um, about to a plus equals
09:27F, you know, call to F and then, uh, inside the arguments, I'm obviously putting I plus three,
09:33but, uh, because I is a variable, we could just print that. So I'm going to do quote, quote,
09:38and then, uh, just stick the actual I in there and then another new line. And then when I'm finished,
09:47I'll say, uh, finished
09:51a plus equals F. And then I'll just say, you know, a is now,
09:56you know, a, you know, print the new value of a, you could also say a equal, equal a, uh,
10:03and then print the value, whatever you want. But the point is I'm just printing everything here.
10:07So now let's go down a little bit more because there is a function call.
10:12If you look at this function call down here, uh, here's like some nonsense that's not being used.
10:16Here's some other, like, you know, disabled code. Really what I'm doing at the very end is I'm just
10:21taking the input that the function received, and I'm just multiplying it by two and returning it. So again,
10:26imagine that your code is a lot more complicated than this. Um, maybe input times two is actually
10:31input times two divided by three and then some other function call and then some other this and
10:34some other that and whatever. So a lot of people will just kind of stuff like a really complicated
10:40expression in one line or one assignment or one return statement. And that's super confusing
10:45because you can't really be sure that every part of the, uh, expression is as you thought.
10:51So it's a really good idea to break it all up into parts and print each part.
10:55So you could imagine, uh, that we have like a C out here and we'll say C out,
11:00you know, begin F I'll say like begin for F and then F receives an input of input,
11:09do an end L and then I'll make a temporary variable here. I'll say int temp results equals input
11:18times two. And then I'll print that F and maybe I'll do the input again.
11:30Temp results is now, um, and then say temp result. So I'm just going to print what the temp result
11:38actually was. And I could print something about, I just multiplied the input by two.
11:42And then, uh, you know, the more complicated your expression is, the more, uh, temporary variables
11:47you want to use and just kind of print every single one of them and print, you know, this new variable
11:52is the result of dividing by two. This new variable is a result of calling some other function and so forth.
11:56So then we have like a basically, you know, basically a pretty good idea. Maybe at the end though,
12:05instead of, uh, multiplying by two again, you want to make it more consistent with your debug output.
12:11So like here, uh, temp result is obviously going to be the result that I return. Why would you do the
12:15expression all over again, uh, for the return part when you could easily get it wrong? Uh, why not instead
12:22just, uh, return the temp result that you made, right? So then that way your output completely matches what
12:28you're actually returning. And for now, I think that's all we need to do. Let me run it one more time
12:33and then you'll see a bunch of stuff. Now, uh, we have a lot of information that we can use to trace
12:40how our program was thinking and hopefully find the problem. But isn't this like a little bit, it's
12:44starting to get a little bit hard to read, right? Um, you can see that there are kind of blocks happening
12:49when, whenever we iterate. So, you know, for me, I always try to keep in mind, you need all the brain
12:54power you can get. Why not just format it a little bit better. So it's really easy to just very quickly
13:00look at one block and see, uh, you know, that all the, the C outs kind of like belong together. So
13:06I'm just going to add one more C out in the main for loop here, just at the very bottom, I'm going to
13:12go C outs and L just so that I get a new line. And then if I run the program again,
13:19notice how it's easier to see the blocks like, Oh, that's clearly one iteration right here. You know,
13:24your mind just grabs onto it faster and that increases your brain power and makes it easier
13:29to debug and read the debug output. So I can go, all right. All right. Okay. So we're right up here.
13:34Oh, around, uh, iteration, um, number 83, uh, a was this number and then it got increased by, uh, this
13:43other number. And the reason that that happened is because the function returned 172. So it basically
13:47increased it by 172 and then it ended up being that number. Oh, okay. I think I see what went wrong.
13:53Oh no. Right. In this particular program, if we scroll up just a little bit, we can probably see
13:58that the integer is overflowing, right? The integer was getting bigger and bigger and bigger here.
14:03And then eventually it jumped into the negative at some point. So right here we can see, Oh, okay.
14:09When did this number become negative? Suppose we didn't want it to be negative. Um, what was this?
14:14This is like a million. This is a billion. No, wait, wait, wait. This is a, no, that's a million right
14:21there. So it's like 299 million. And then it overflowed. Oh no, no, it didn't. It didn't overflow.
14:28It started at 299. Then at the bottom, it was sitting at 899 million. That's kind of close to a
14:34billion. And it did jump up from 300 million to 900 million. So that's a jump up of like 600 million.
14:43Oh, you know, it's like kind of jumping up faster with each iteration.
14:46Oh, and I'm using a 32 bit signed integer, which has a maximum value of around 2 billion.
14:52So now I could possibly realize at this point, Oh, I think maybe it, the number is just like it,
14:57it became too big and I need to change my data type or I need to change my algorithm for some
15:02reason. Right? So you can kind of get to the bottom of things pretty quickly.
15:06So here's another problem that people encounter when they're using caveman debugging.
15:11Uh, basically you, you add all these C out statements or print statements or whatever
15:16you're doing in whatever language you have. And then when you're finished, you're kind of like,
15:21well, I guess, uh, I need to delete all of these C outs because I don't want all of this, you know,
15:28junk being printed in my program after I fixed the problem.
15:31Or maybe you're trying to debug a different part of your program. So, uh, then you start
15:36commenting out all the print statements or you start, you know, deleting them or whatever.
15:40And then whoops, later on, you realize maybe that part of my program
15:43was not actually fixed. And I have to add all the C outs all over again from scratch
15:47that costs time. So that's not fun. So here's the trick that I like to use.
15:52It's called a short circuit. It's a sort of like a beginning C plus plus
15:57thing, or just when you're first learning how to use logical operators, uh, for Boolean expressions.
16:04So I'm going to do a Boolean and I'm just going to name it after what problem I'm trying to solve. So
16:10we could just call this, you know, main C outs or something like that. Uh, let's, I don't know,
16:15primary problem. I'll call it primary problem. C outs, start the Boolean as true,
16:21because then what'll happen is, uh, we can then short circuit all of the C out statements. We can
16:28say primary problem, C outs and C out like that. And the way the short circuit works works is if you
16:37have a logical and expression, you can see right here, we have two sides of that statement. Now
16:42one's on the left, which is just the Boolean and one's on the right, which is the C out object.
16:46Uh, if we have a logical and then, you know, one and zero is equal to zero true and false is equal
16:54to false. So both things have to be true for the expression to evaluate to true, which means if the
17:00first thing is false, there's no point in even looking at the second thing at all. Because if I,
17:04if I set that to false, then the second part, the C out is, it doesn't matter. The whole expression is
17:11going to evaluate to false anyway. So, uh, the logical operator itself will just, you know,
17:16block out the rest of the statement that's called a short circuit. So I'm going to copy the short
17:20circuiting to all of my C out statements like this. Okay. And doop, doop, doop, doop. Okay.
17:35Do that. And then the C out there. And I guess I'll leave the final result there. And for the moment,
17:39you know, this is just a regular function. I can't, uh, uh, for the time being apply the short
17:45circuiting to this other F, uh, unless I do a separate Boolean, but keep in mind,
17:50if this was a class and you had two different methods that you wanted to short circuit C outs
17:56inside of at the same time, you could just use a member variable and name it after the problem
18:00you're currently trying to solve and then just short circuit each, uh, C out in the appropriate
18:05method with that Boolean. So I guess in this regular program that doesn't have a class,
18:10I could move the Boolean into the global if I wanted to, but I'm not going to right now.
18:15And if you're writing assembly or some simpler language and you're thinking, Hey,
18:18I can't short circuit an assembly or some other language where you could write a function that
18:22just takes a couple arguments. You could write a function that takes a string to print and maybe
18:27a number to print or just, you know, something simple like that. And then
18:30in the function itself that prints, you could call it debug print. If you want, uh, have it look at a
18:38global variable or define that you've set up at the top of the assembly module, which you can just
18:42turn on and off, like just do a data byte and set it to a zero or set it to a one. And then the
18:48function debug print will just look at that variable to decide whether or not it's going to actually print
18:54or not. So, you know, it's a little more complicated in assembly, but you can do it basically the same
18:58concept. Let's run this again just to make sure it still works. Okay. So it still works. So now
19:04suppose I want to turn off all the C out statements. If I just change the true to false,
19:11notice how it's all gone, except for the stuff inside of F, which I told you I wasn't going to
19:15touch, but notice how easy it was to mute it. And then if I want to bring it back later, I just go true
19:22to unmute it. Of course, if you want to do a little bit more typing, you can also do
19:27if statements like if this condition is true and that condition is true, then print the debug
19:32statements. So then you can have a more complicated way of muting and not muting, but I'm going to
19:36leave it as is right now. Let's see. What else do we got here?
19:43Um, I think actually at this point, this is the basic idea of caveman debugging. Just
19:49basically print everything, print all of your decision points, use pretty labels that are really,
19:55really easy for you to understand, use formatting, um, so that your brain doesn't have to struggle
20:01to understand what you're seeing. If you just print a bunch of numbers, you're going to be shooting
20:04yourself in the foot. Um, and you short circuiting or if statements to selectively mute them so you
20:10can do less typing and, and debugging the debugging and so forth. But, um, yeah, I think, uh, I think this
20:18is all I really wanted to show you in this particular video. I hope you feel like an expert in caveman
20:22debugging. I certainly am. And it, you know, it kind of fits my personality a little bit. I'm like,
20:27what's going on? Um, and like I said before, you can use a proper debugger. It's probably a better
20:33idea, but I usually don't unless I'm in big trouble and I have a huge mess on my hands. I usually just
20:38go directly to caveman debugging. Uh, we'll, we'll look at the same piece of code in my next video that
20:43I'm going to publish, which is, uh, going to talk about using a proper debugger. So, uh, watch the
20:50next one also, but for now, I hope you've enjoyed this video. Uh, thank you for watching. I hope you
20:57learned a little bit and had a little bit of fun. See you in the next video.
21:00Oh, whoops. What the heck? Hey everybody. Thanks for watching this video again from the bottom of my
21:10heart. I really appreciate it. I do hope you did learn something and have some fun. Uh, if you could
21:14do me a please, a small little favor, could you please subscribe and follow this channel or these
21:20videos or whatever it is you do on the current social media, some website that you're looking at
21:25right now. Um, it would really mean the world to me and it'll help make more videos and grow this
21:30community. So we'll be able to do more videos, longer videos, better videos, or just, I'll be
21:34able to keep making videos in general. So please do me, do me a kindness and, uh, and subscribe.
21:40You know, sometimes I'm sleeping in the middle of the night and I just wake up because I know somebody
21:44subscribed or followed. It just wakes me up and I get filled with joy. That's exactly what happens
21:49every single time. So you could do it as a nice favor to me, or you could, you could troll me if you want
21:53to just wake me up in the middle of the night, just subscribe and then I'll, I'll just wake up.
21:57Uh, I promise that's what will happen. Also, uh, if you look at the middle of the screen right now,
22:02you should see a QR code, which you can scan in order to go to the website, which I think is also
22:07named somewhere at the bottom of this video. And it'll take you to my main website where you can just
22:12kind of like see all the videos I published and the services and tutorials and things that I offer
22:16and all that good stuff. And, uh, if you have a suggestion for, uh, uh, clarifications or errata,
22:25or just future videos that you want to see, please leave a comment. Or if you just want to say,
22:29Hey, what's up, what's going on? You know, just send me a comment, whatever. I also wake up for those
22:34in the middle of the night. I get, I wake up in a cold sweat and I'm like, it would really,
22:39it really mean the world to me. I would really appreciate it. So again, thank you so much for watching this
22:44video and, um, enjoy the cool music as, as I fade into the darkness, which is coming for us all.
23:14So again, thank you so much for watching this video.

Recommended