How Can I Learn to Solve Coding Problems Without Relying on AI?
Asked by
supper.club minutes
I'm getting back into programming and want to rebuild my C fundamentals without depending on AI to solve problems for me. How do you approach a programming problem when you can't immediately find an identical example online?
For instance, I was making a small calculator and ran into confusing input behavior:
int x, y;
printf("Enter two numbers: ");
scanf("%d", &x);
scanf("%d", &y);
printf("x: %d, y: %d", x, y);
When I entered values such as 10 and 2, I sometimes got 10 and 0, or the second value was missing. I eventually tried reading both values with one scanf call, but I still didn't understand what caused the original behavior. My searches also weren't very useful, possibly because I have trouble describing programming problems clearly.
What methods do you use to investigate issues like this without immediately asking AI for the answer?
Answers
A good debugging process is to test one assumption at a time. Print values after every input, inspect the return value from each scanf call, and reduce the program to the smallest version that still fails. A debugger can also show exactly where the result changes.
For input-heavy programs, many C programmers prefer reading a whole line with fgets and then converting or parsing it. That gives you the complete text that the user entered and makes invalid input easier to detect. The important habit is to investigate what the program actually did rather than guessing what it did.
Reply to this answer
You donโt need to avoid every tool completely; the goal is to remain responsible for understanding the answer. Before looking anything up, write down what each line should do, form a specific hypothesis, and try a small experiment. Then consult a book, course notes, official documentation, a debugger, or experienced people. If you use an automated explanation, ask for the reasoning and source documentation, then verify it yourself instead of copying the proposed code.
Reply to this answer
Start by searching with short, specific keywords instead of writing a whole question. Things like "C scanf second value fails," "scanf return value," or "scanf format specifier int" will usually lead you to documentation and relevant examples. The language manual and library documentation are especially useful because they explain the required types and failure behavior. Also check the return value from scanf so you know whether it successfully converted each input.
Reply to this answer
The original code is generally valid if the input is actually an integer. One likely issue is entering something like 10.0 while using %d. That conversion expects an int; it reads the 10 and leaves the .0 in the input stream, so the next scanf can fail when it encounters the period. Check scanfโs return value and make sure the format matches the variable type.
Also, scanf("%lf %lf", &x, &y) is not a correct fix when x and y are int variables. In scanf, %lf expects pointers to double, while %d expects pointers to int. Passing the wrong pointer type can cause undefined behavior. Use scanf("%d %d", &x, &y) for ints, or change the variables to double and use %lf.
Reply to this answer
Learning without instant answers mostly means becoming comfortable with manuals and deliberate searching. Read the functionโs documentation, look up unfamiliar terms, compare the required argument types with your variables, and check error or return values. Struggling for a while is useful, but you donโt have to stay stuck indefinitelyโasking a focused question after making a real attempt is still part of learning.
Reply to this answer
Add your answer
Answers are reviewed before publication.
supper.club minutes: That explains the behaviorโI had been entering decimal-looking values and hadnโt checked the return value. I also didnโt realize that %lf required double variables. Iโll start reading the documentation for each function instead of copying examples without checking the types.