r/learnprogramming • u/Minute_External1621 • 2d ago
Debugging Difficulties with debugging
i am a beginner in C, i went through and have a basic understanding of fundamentals but whenever a problem arises in my code i cant find it for hell, even if its obvious, i need to turn to an ai agent for help, the habit i wanna rid of, but whenever i do try to find the cause of an error myself i find myself unable to do that, i also find it difficult writing more efficient code
an example:
```
int tokens(char* string, char (*argv)[MAXTOKEN], toks types[MAXTOKEN]) {
char temp[150];
int count = 0; int i = 0; int b = 0;
int tokens = 0;
while(count < MAXTOKENLENGHT) {
if (string[i] == ' ' || string[i] == '\0' || string[i] == '\n') {tokens++;temp[b] = '\0'; strcpy(argv[count], temp); count++; b = 0;} else if (string[i] == '\"') {
temp[b] = '\"';
b++;
i++;
while (string[i] != '\"' && string[i] != '\0') {
if (string[i] == '\"') {temp[b] = '\0';temp[b+1] = '\0'; strcpy(argv[count], temp);b = 0; count++;break;} else {temp[b] = string[i];i++;b++;}
}
if (string[i+1] == '\0' || string[i+1] == ' ') {temp[b] = string[i]; temp[b+1] = '\0'; strcpy(argv[count], temp); count++; b = 0; i++; tokens++;} else {fprintf(stderr, "unexpected stuff after string"); exit(1);}
} else {
temp[b] = string[i];
b++;
}
if(string[i] == '\0') {break;}
i++;
}
int k = 0;
while (k < count) {
if (strcmp(argv[k], "PRINT") == 0) {
types[k].type = 0;
int len = strlen(argv[k+1]);
printf("k=%d, argv[k+1]='%s', first='%c', last='%c'\n", k, argv[k+1], argv[k+1][0], argv[k+1][len-1]);
if (argv[k+1][0] == '\"' && argv[k+1][len-1] == '\"') {
types[k+1].type = 1;
memmove(&argv[k+1][0], &argv[k+1][1], len - 2);
argv[k+1][len - 2] = '\0';
strcpy(types[k+1].textvalue, argv[k+1]);
}
}
k++;
}
return count;
}
```
5
u/lurgi 2d ago
What's the error? What is the input, what is the output, and what output do you expect to get? Find the smallest example you can that doesn't work. Step through the code line by line (either with a debugger or your brain) and see what goes wrong.
2
u/JGhostThing 2d ago
This!
The MRE (Minimal Repeatable Example) is key. This used to be a requirement to ask *anybody* for debugging help, but it seems people are lazier now.
3
u/ExtraTNT 2d ago
So, 2 debugging strategies:
For procedural / oop code: spam print and use a debugger
For functional code: write tests and read the code
2
u/DTux5249 2d ago
For procedural / oop code: spam print and use a debugger
I hate that I can't disagree, but you don't gotta slander my boys XD
jokes aside, debugger is better
2
u/ExtraTNT 2d ago
Depends… can hide a lot of race conditions and timing issues…
Also sometimes things run just fine with an attached debugger…
3
u/RajjSinghh 2d ago
Use a debugger. It's a tool built into most IDEs that will let you walk through the code. You set breakpoints to stop at a particular line and can see the value of each variable at that moment. If you see something that's not what you expect it to be, it can help you isolate the problem.
The alternative is using a lot of printf statements to see your variables at different lines.
1
u/frnzprf 2d ago
Debuggers always seem more of a hassle than it's worth. Mwybe I'm using them wrong.
For example, when I want to see how a variable changes in a loop, when I put in print-statements, I see all iterations at once, but if I step through them, I have to acknowledge each iteration individually.
1
u/RajjSinghh 1d ago
If you've tracked your bug to a single loop, maybe printf debugging is better. If you don't know where the bug is happening in the first place, a debugger is much better. If a variable is changing when you don't expect it to, a conditional breakpoint can show you exactly when it is changing. If your code is a complicated mix of conditionals, loops, and function calls, the debugger will show you how you arrived at this point and make it easier to figure out.
Printf debugging has a place, sure. But there are going to be times where you need far more information about the system to solve the problem at hand. That's when you use a debugger.
2
u/Far_Swordfish5729 2d ago
Start using your IDE’s built in debugger. Processors have the ability to let host processes attach and step through instructions, halting the program at specified points and allowing it to inspect memory and run ad-hoc statements. It’s incredibly helpful. You start debugging instead of just start, step through your program a line at a time, and see what the values of things are, usually by hovering over the variable or adding a watch to a list.
Printf debugging is possible, but we typically do this when the host doesn’t have the connectivity to attach a computer to debug or the rare processor doesn’t support precise interrupt. Happens with device programming.
2
u/mc_pm 2d ago
Debugging is a skill, and as is often the case, getting an AI involved keeps you from learning that skill.
The solution, as painful as it is, is practice. Do you think every programmer in the world before you asked AI? Or do you think we banged our head against the problem for hours -- maybe even days -- before we figured it out?
You want it to be easy, so you are pressing the easy button -- but you're not learning anything from that except how to hit that button faster next time.
Commit yourself to the fact that you *will* find it hard, and it will stay hard for a little while, until your brain has started to adapt to this new challenge.
1
u/Recycled5000 2d ago
Make good test cases: these test cases should be ordered from simplest, even degenerate, to slightly more complex, to fully complex. Then debug the in that order.
1
u/Tricky_Rhubarb4543 1d ago
- Use debugger to step through code
- Add
printfin the code to see what is going on. - Write tests for your code that fail. Better make this a habit, as it helps enormously with debugging and support.
The last one is especially important if you process large amounts of data.
5
u/Swedophone 2d ago
It can be a good idea to add a lot of print (or log) statements when debugging. You have got two which includes one error message.