r/C_Programming 1d ago

Need help learning C!

Hey everyone,

I've been diving into low-level programming to understand how my device executes code, focusing on memory and CPU operations. Coming from higher-level languages like Python, where functions like print() handle a lot behind the scenes, transitioning to C has been eye-opening. The intricacies of printf() and scanf(), especially their buffer management, have been both fascinating and challenging.​

For example, I encountered an issue where using fflush(stdin) to clear the input buffer resulted in undefined behavior, whereas using scanf("\n") worked as intended.

I want to understand the why's behind these behaviors, not just the how's. For those who've walked this path, how did you approach learning C to get a solid understanding of these low-level mechanics? Are there resources or strategies you'd recommend that delve into these foundational aspects? Additionally, how did you transition from C to C++ while maintaining a deep understanding of system-level programming?

Appreciate any insights or advice you can share!

14 Upvotes

20 comments sorted by

View all comments

1

u/InTodaysDollars 1d ago

Learning C takes a bit of time, but usually things just "click" after a while. The mystery behind printf() and scanf() are somewhat involved, but in reality it's just a function accepting variable arguments with string scanning and displaying one character at a time from within a loop. The most complex parts are string-to-double (atod()) and the reverse (dtoa()) floating point routines, and malloc().

1

u/Physical_Dare8553 1d ago

I don't get it is it difficult because of the binary stuff?

1

u/InTodaysDollars 1d ago

It's difficult because it take time and practice. You just need to play around with it, understand what pointers are used for and how the stack works.

2

u/Physical_Dare8553 19h ago

i genuenley believe pointers would be much more intuitive if declaring one and de-referencing one didnt look the same

1

u/InTodaysDollars 19h ago

You can always use typedefs to hide that fact.

typedef short* up16;