r/cprogramming • u/trayhan066 • Mar 03 '26
Need help in understanding C
Hello, I am a first-year, second-semester college student. I have been studying C programming since the beginning of my college, but I have been very confused about how it works. I’ve read books and watched videos, but it still feels difficult to understand. I only understand the basic concepts up to printf and scanf. Beyond that—topics like if-else, switch-case, and sorting algorithms like bubble sort—are extremely hard for me to grasp. Also, if someone asks me to write a C program for something like the Fibonacci series, I just freeze. I understand what the Fibonacci series is, but I don’t know how to think through the logic or translate it into code. I couldn’t attend my first-semester final exam due to personal reasons, but I’m pretty sure I would have ended up with a backlog anyway. Do you have any recommendations on how I should study and improve my understanding of C programming?
17
u/Snezzy_9245 Mar 03 '26
Worst plan: watching videos.
Best plan: writing code, even if you're just copying.
When copying it must go in through your eyes and out through your fingers. NOT copy and paste. Get to know what each little part is doing and how things fit together. Write code every day.
8
u/Prevent_Scurvy Mar 03 '26
Hands on is the only way to retain new information. You only learn by making mistakes and struggling through something at least once.
7
u/SmokeMuch7356 Mar 03 '26
Programming is something you learn by doing - if you find you don't really understand a concept, write small toy programs (plural) to explore just that concept.
Like, you say you have trouble understanding things like
if-elseorswitchstatements. So you need to write some simple code that just explores those individual topics.For
if-elsestatements specifically, remember that the basic idea isRemember that in C,
0in a Boolean context evaluates to "false" and any non-zero value evaluates to "true" (it's actually more accurate to think in terms of "false" and "not false" as opposed to a singular "true" value).The easiest way to illustrate it is with code like
Start with a program that does just that - figure out a way to set
x(either using ascanfcall, or just assigning it directly and editing and recompiling code to test different values), then expand to test against against specific values:or relational expressions:
if ( x < 100 ) ...
Relational and equality expressions like
x < 100andx == 40will evaluate to1ifxsatisfies the condition or0if it doesn't.But write code that does just this much; don't try to do anything much more complicated than that until you're sure you understand how
ifstatements work. Then you can start branching out into more complex code.It also helps to write things out on paper. Like with a bubble sort, just write out a sequence of values:
then execute the following algorithm by hand
istarts at0,jstarts at1:Is
3(arr[j]) less then7(arr[i])? Yes, so we swap those two elements:Now we advance
jby 1:5is not less than3so there's no swap. Advancejagain:4is not less than3, so advancejagain:1is less than3, so swap:Neither
8nor2are less than1so we'll skip those steps.1is now in its final position.Now we advance
iand setjtoi+1:and go through the whole thing again;
5is less than7, so swap:Advance
j:Lather, rinse, repeat, until
ireaches the element before the last element in the list.TL/DR: Write simple programs to explore individual concepts. Execute algorithms by hand on paper to understand how they work. Eventually stuff clicks.
7
u/[deleted] Mar 03 '26
Show us your Fibonacci code.
3
u/theNbomr Mar 03 '26
Abstract problems are not the best types of problems to solve as a learning experience. Think of a simple problem that makes sense to you and pursue that. Don't worry if it's something you can solve with simple existing tools; that doesn't matter. Don't be surprised at how simple a problem can be without presenting challenges to you as a novice.
Manipulation of text, such as replacing a specified string in a specified text file with another specified string will provide you with abundant challenges and also provide you with good reason to make use of the programming language constructs that you are finding difficult.
Don't be afraid to start over, or let the program go unfinished when you realize you have mastered the concepts needed to solve the problem and are ready to take on bigger challenges. But also, don't fool yourself into thinking you've achieved mastery when you really have not.
Start thinking about the process as a sequence of logical steps and how the programming language is simply a formal unambiguous way to express that logic process. Try to draw comparisons to things like spoken/written language grammar and structure, or to familiar instruction formats like cooking recipes or furniture assembly instructions.
2
u/Specific-Housing905 Mar 03 '26
Forget Fibonacci and sorting, they are not for total beginners. You know printf and scanf. That's enough to get some input and output. Next step would be if-else statements. What exactly don't you understand?
2
u/zenluiz Mar 03 '26
It seems you lack knowledge in basic logic. So I’d first study programming logic (if, else, switch, for, while, AND, OR, not, etc). After that, you need some basic understanding of how computers and operating systems work, like memory addresses, file handles. After that, I believe learning C would become easier.
4
u/BIRD_II Mar 03 '26
Have you read the book? The C Programming Language?
1
u/Xinizen Mar 03 '26
Try file handling or implementing data structures
1
u/grimvian Mar 04 '26
Try:
Learn to program with c by Ashley Mills
https://www.youtube.com/playlist?list=PLCNJWVn9MJuPtPyljb-hewNfwEGES2oIW
I don't use the same IDE, the program you use write code in, but Code::Blocks, because it's easy and fast to install, easy to use, everything is ready from start and it's open source!
1
u/OkShip1259 Mar 03 '26 edited Mar 03 '26
I'm going to reveal something to you: you'll never truly learn C until you study computer architecture and operating systems, given that it was conceived that way. And of course, to understand that, you need data structures and algorithms—that is, the hodgepodge called "discrete mathematics." Because informally understanding algorithms, in my opinion, isn't enough, although I should point out that I'm not saying you have to be an expert.
1
u/zhivago Mar 04 '26
The most important thing you can do is very simple.
Learn to ask intelligent questions.
1
u/Snezzy_9245 Mar 04 '26
Remember Brian Kernighan and Dennis Ritchie learned and they didn't have the book. But they wrote it. If the rest of us can learn C then you can too. Don't give up.
1
u/flatfinger Mar 06 '26
It's important to understand three kinds of programming languages, which I'd suggest learning in the following order:
In C# (a language of the first type) for example, one might declare a two-dimensional array with five rows having five elements each, with rows and columns both being indexed from 0 to 4. If code were to attempt to access element 8 of row 1, that would cause an "index out of range" exception to be thrown. This would often lead a programmer relatively directly to the problem.
In low-level dialects of C, one could similarly declare an array with five rows of five elements, which would reserve space for 25 elements total, which may be viewed as being numbered 0 to 24. An attempt to access
arr[i][j]when i is in the range 0 to 5 would access element (i*5)+j of the entire array if that value would fall in the range 0 to 24, without regard for whether j is in the range 0 to 5. Thus, accessingarr[1][8]would be equivalent to an accessingarr[2][3]orarr[0][13], since all would access element 13 of the entire array. If the access would fall outside the array, the code would likely access "something else" with consequences that would generally not be predictable, which may make debugging somewhat difficult.In high-level dialects of C, a compiler that could determine that a particular input would result in a program attempting to access anything past element 4 of any particular row would be allowed to produce code that would completely ignore what the programmer wrote if such input were received. Compilers like gcc are designed to do this quite aggressively. Troubleshooting code processed with such dialects is even worse than troubleshooting those processed with the second kind, since the code that's actually being processed may bear no relation whatsoever to what the programmer actually wrote.
Many people here like to disparage the second kind of language, even though most tasks for which the third kind of dialect would once have been appropriate can be better accomplished using languages of the first type that have been developed in half century since C was invented, and even though the specifications for the third kind of language were never designed to unambiguously distinguish cases that must be processed meaningfully from those that programmers must avoid at all costs.
1
u/demetrioussharpe Mar 07 '26
Practice. Research. Practice some more.
0
u/[deleted] Mar 04 '26
[deleted] — view deleted comment
2
u/[deleted] Mar 04 '26
[removed] — view removed comment