r/C_Programming • u/xerrs_ • Jul 24 '26
Article C is way more different than I thought
I have had a lot of experience with Zig, and a bit of Assembly for a while now. Just 1 1/2 months ago I started coding in C, because it is a must-have for my upcoming job. I have a Zig project that decompresses a PNG from scratch, and I thought it would be a good learning experience to do the same with C. Tbh, I thought it will be a simple slide, but as it turns out; it really is not.
While Zig got heavily inspired by C, it differs from its features and API. C has a lot of headers for different things (such as stdio.h / stdlib.h / math.h), while Zig just has the std library, packaged with all of the other necessities (std.Io / std.math / std.crypto etc.). It was also interesting from going to using defer, errdefer, orelse, or other keywords in Zig that help in reducing bugs and make the code cleaner to read, to a lot less keywords. Looking at C89, C has 32 keywords, and Zig around 49 +/- 2 (not too sure). What that showed me, was that even with a lot of similarities, and the fact that I coded in Zig for 2 years, it was still difficult to get used to and learn C.
For me, one of the annoying parts was the fact, that I could never really remember what function or what struct is where. The fact that the FILE struct is located at stdio.h, while the DIR struct is in dirent.h, was something that bugged me for a while, but now after more than a month with C, is something I started to get along with.
Also, Makefiles are pretty darn cool. I have always been seeing them in big projects, and always wondered how they function. After coding a few projects and including a Makefile, I can safely say it is very nice seeing how your project is being made, after you type "make" and hit enter. The first time configuring the Makefile almost made me loose half my hair due to stress, but if you get it running properly once, then it is safe to say that it is gonna always run properly (especially for me, as my projects are somewhat small still).
What I came to appreciate more was the community in C. Thought I was gonna get insulted by some 59 year old grandpa, because I didn't make an out-of-bound check for my Pixels array, or because I didn't check whether or not the variable I de-noted with size_t exceeds __SIZE_MAX__ - 1. Kinda the opposite. I learned a lot by sharing my projects on Reddit, a lot of smart people were quick to point out mistakes, and tell me where I could improve or where I went wrong etc.
A lot of stuff, that I have learned, was because of people telling me. Adding -Wall -Werror warnings, -fsanitize=address,undefined, -fno-omit-frame-pointer, among other things are the sole reason I debugged and fixed my programs a lot quicker. Especially -fsanitize was important to me, as I always try to keep memory usage low for my programs, and activating that warning, showed me where my memory leaked, or where overflows happened (yeah... out-of-bounds checks are actually pretty useful...).
With that being said. I still have a lot to learn in C. I realized that my code is not really C-esque (as some might say), which is mostly because I come from (prior to Zig) high-level languages such as Python, JavaScript or even GoLang. Me handling arrays with indexes instead of using the given possibility of pointers, shows me, that I still have a long way to go.
But I am certain with one thing, that being that, C is way more different than I thought.
74
u/pedersenk Jul 24 '26 edited Jul 24 '26
Heh, yes, C has a competitor set that tries to make out it is old, legacy and only for the "older generation" but as you have found out, the community represents much of the entire computing platform.
Unfortunately so many beginners and juniors don't find this out until its too late and they have invested time into learning a less pervasive language.
As you may have noticed, dirent.h is not actually part of C but instead POSIX. Anything that is not really possible to put into a core language tends to get delegated to this. That way on POSIX conforming platforms, dirent.h is always available, otherwise you use the platform specific alternative (such as WinAPI).
After a while, this separation really does make sense, and ensures the portability and pervasiveness of C. Stuff like this is why other language ecosystems are not as portable to i.e embedded platforms.
47
u/Limp-Confidence5612 Jul 24 '26
seems to me like someone isn't using man -k enough.
25
u/ReallyEvilRob Jul 24 '26
Trying to memorize which headers to include and how to call each library function or how to access different structs is a futile effort. This is what an IDE is supposed to help with. Also, the man pages are an asset. I always keep a window open just for man pages when I need to look something up.
6
u/soundman32 Jul 24 '26
Don't modern IDEs automatically add the correct #include when it detects a method/struct that hasn't been defined?
20
u/logic_circuit Jul 24 '26
Comparing C to anything else is serious mistake. Just like C++. Beasts on their own.
10
u/Independent_Art_6676 Jul 24 '26
after a while you can tell what languages someone knows by how they write in the language they are learning.
Not sure why you would think older coders are mean spirited. Most people learn more patience as they age, and have nothing left to prove about their ability, so being hostile all the time becomes pointless. As retirement approaches, you want to pass on your wisdom and knowledge. That said, I can see the 'you bother me" foghorn attitude kicking in after a bit for some of us <g> esp if highly placed and busy.
5
u/rapier1 Jul 24 '26
Hey pal! I'm a 56 year old grandpa! You're doing fine.
8
u/flatfinger Jul 24 '26
Before the publication of the C Standard, C wasn't so much a single language as a recipe for tailoring language dialects to particular platforms and purposes. Someone who understood the recipe and understood a platform would have a pretty good idea how to write a program for a C implementation targeting that platform. One of the most useful features of the recipe is the extent to which it allowed platform-specific code to be written in toolset-agnostic fashion. Some platforms included features that could only be accessed via means not anticipated by the C recipe, but what would usually happen is that the first implementation targeting a platform would include language features to access those features (e.g. near and far pointers in 16-bit x86) and other people writing implementations for that platform would incorporate features that were compatible with whoever had gone first.
When the Standard was written, it wasn't intended to fully describe a language that could do everything that people had been doing with the aid of the C recipe, but instead specify a set of features that implementations intended for different platforms and purposes would extend as appropriate to serve the needs thereof.
The optimal design of a program that is intended to run only on platforms where it's impossible for a file name to exceed some particular length will often be quite different from the optimal design of a program where names could be longer. The Standard couldn't really say much about how file name strings passed to
fopenshould be processed, but a program that received a file name from the command line and passed it unmodified tofopenwouldn't need to care about anything having to do with file names--not even their actual or maximum lengths. If an execution environment would allow a million-character string to be passed on the command line, a program could pass that string as a file name to fopen. If an execution environment limited strings to 127 characters (something MS-DOS did, btw), then an inability to specify longer filenames on the command line would be a limitation of the execution environment, rather than the program or the language.Anything a program might try to do with file names other than pass them straight from command line arguments would be non-portable, but the range of fully portable use cases was sufficient to justify the inclusion of
fopenin the Standard. It would be much harder to provide a means of reading directories that would simultaneously accommodate systems where file names might be very long, or where it might be possible for multiple files with the same name to coexist in the same place using some other field like a revision index to distinguish them) without imposing a needless burden on systems where file names were limited to e.g. an eight-character base name and three-character extension, and consequently the C Standard didn't even try.4
u/Mindless-Study1898 Jul 24 '26
59 yo old grandpa? 59 isn't that old is it? 😭
6
u/fdwr Jul 24 '26
Note
deferis pending for C29, implemented by a few C compilers like clang/cake/slimcc/onramp.3
u/mykesx Jul 24 '26 edited Jul 24 '26
I would much rather mentor new C programmer's than wade through AI slop. Programming is an art, and the language dictates the way you express things. I suspect many of us were mentored at some point, nothing wrong with paying it forward.
1
u/thurizas123 Jul 25 '26
welcome to the C club :)
One other tool you may want to look at is valgrind - I find it very good at detecting memory related issues.
1
u/MentorBobProctor Jul 25 '26
Keep going at it bro. I have lost most my C ability not using it much and using JS and Python haha. Something so nice about C, I need to make some project just to use it again
1
u/BackgroundReturn9622 Jul 29 '26
The C programming language is old but reasonably well defined. One thing to consider is that many high level programming languages (Java, Python, MATLAB, C++) really just get converted to C. C is closer to how computers actually work behind the curtain. Memory allocation and pointers are the structure behind almost all programs. It is good to know C when a program behaves strangely. You could go one level further and learn assembly languages.
For fun "I think all foreign languages should be abolished". On the other hand I know many languages, FORTRAN, C, C++, java, python, MATLAB, maxima, etc. where each has their place.
0
u/stianhoiland Jul 24 '26
Shoutout to my boy, make!
0
u/LavenderDay3544 Jul 25 '26
Today's C isn't the C of the 1970s.