Learning C after high level languages

By Steven Goodstein, DevOps Engineer

To understand my perspective on learning C you must first understand my background in computer science. I’m not a computer science major. I’m not well versed on how computers work at a low level such as bytes, and memory management. My first language was Ruby, a language which provides eloquently constructed syntax, a language meant to be friendly towards the programmer and one that provides a standard library that does a whole lot for you. Ruby is more than 20 years newer and was actually written in C! After learning C I can say that you’ll really appreciate the abstractions Ruby and other high level languages provide.

The C Programming Language

With that being said I went about learning C the same way people have been learning C for about the last 45 years, by reading the de facto C book: The C Programming Language written by the creator of C Dennis Ritchie. If you’re surprised that people are still coding in a language written about 45 years, long before the modern internet, well so was I. If you’re wondering why I or any other person would program in such an “ancient” language relatively speaking there are several benefits of C. C is low-level, C is the building blocks for many other languages and will therefore perform much faster than them. C is very portable and easy to run on many different machines and is actually the most used language due to this. In addition C is relatively close to machine code, and gives you flexibility and control of things other languages may not.

Reading The C Programming Language was definitely a good start in learning C. The book does a really good job at exposing you to just about all C has to offer. It covers close to if not all of the 32 keywords C has available. It reviews about all of the core concepts and it provides tons of examples for how to write some basic boilerplate functions. For me and anyone else experienced with higher level languages it starts off fairly straightforward. “Hello world” with a print function, for loops, while loops, if / else statements. At first glance it appears like this will be a breeze as those are pretty much the same as every other language you know with minor syntax adjustments.

Then there are some differences. Declaring a variable’s data type was something I wasn’t accustomed to, as well as declaring the size of your arrays beforehand. Then you get into some things that really confused me at first. Strings, arrays, pointers and their relationships. Strings and arrays are something that every programmer has worked with but this may just trip you up.

I’ll outline how these 3 work which will hopefully help you greatly if you’re trying to learn C:

Strings:

Strings are actually arrays of characters that’s last element is ‘\0’. The ‘\0’ signifies that the string is complete. So a string is just an array that takes characters. Characters are a single letter like “A” and take up 4 bytes of memory.

Arrays:

Arrays are more or less like arrays in most other languages except they require that you set a predefined size so the computer can allocate proper memory. C doesn't provide any push or pop like most modern languages do. The big difference is that arrays often decay to pointers and we'll cover this more below.

Pointers:

Now pointers were totally different for me. Essentially a pointer is a variable that holds an address. This address is the place in memory where your data is stored. You can get a variable's address by using an & before it. You can then get its value back by dereferencing that address with a * symbol. So if you’re familiar with pass-by-reference you can very easily set a variable to point to the same address as another variable and then when there is a change it will be reflected in both. If you can understand that then you’re at a good point.

Relationship between Arrays and Pointers:

The part that really got me was that arrays including character arrays decay into pointers, this happens most of the times you’re trying to use the array and always when an array is passed to a function. If you were to just try to print your array you would get the pointer address. This was baffling to me. Another interesting thing is that array’s have a spot in memory and each element in an array has an address right next door. So remember I said arrays decay into pointers? Well you can dereference that array because it’s just an address of the beginning of the array and it’ll provide you the first element of the array. You can then do something like add 1 to an array and dereference that and you’ll get the next element! It’s all quite cool and makes sense actually if you realize that elements in an array are right next to each other in memory and adding 1 to the address is kind of like saying give me your first neighbor to the right.

Conclusion

Now if you can get through all that you're well on your way in C. There’s also other useful concepts like structures, and unions which are nice ways to hold and encapsulate data. Structures may remind you of objects and some of their benefits if you're familiar with OOP. Other things C is pretty good at include memory allocation and bit management.

The book is a great way to get started but there were two minor drawbacks I noticed. It doesn’t go too deeply into basic concepts, so if you’re very new to programming it may be hard to keep up. The other thing is that the book is a bit outdated in some ways. It was constantly referencing Pascal and making comparisons to programming languages at the time which are pretty irrelevant to us now. But if you want to learn C it’s a great start and if you supplement some of the trickier concepts with a little YouTube help you’ll be just fine.