How do I learn the C programming language on my Apple Mac?
The C programming language has been around since the 1970s, but it has never gone out of style, and learning C is one of the best computer skills you can acquire. Mac OS X comes with C built into it, and Apple has used C while making every aspect of OS X and iOS.
Because C is such a popular language, it even forms the basis of many other programming languages, including two big names: C++ and Objective-C. And even though Apple is migrating from Objective-C to
Swift, guess what languages Apple used to build Swift? Throughout the Swift code, you’ll find C and C++ files.
How to learn C in Mac OS X: Why you should learn C in Mac OS X
You’re almost sure never to use C professionally, but learning it is a rite of passage for programmers.
The C programming language has influenced many other languages. Learning C enables you to understand what’s going inside a computer in general.
C is also a curious creature, in that it’s a high-level language (these are the easy ones to understand), but with low-level elements (so-called because they are “close to the metal”). The C syntax is similar to many modern programming languages. However, you have to learn how to allocate memory, free up memory to prevent leaks and use memory addresses and pointers (memory blocks that point to other memory blocks). All of this memory management will most likely drive you to tears at some point, but it’s superb for understanding what a programming language is doing.
This complexity is why C is used in courses such as Harvard’s CS50: Introduction to Computer Science. More modern languages, like Python, take care of memory for you; which is better for day-to-day use, but not as good educationally.
How to set up and use a Raspberry Pi 3 with a Mac
How to learn C in Mac OS X: How to set up C in OS X
Like Python, it’s easy to set up C in OS X. Mostly because it’s already packaged in the system and you don’t need to install anything.
Open terminal and enter clang –version. Clang is the compiler built by Apple to compile C and a bunch of other languages. We get Apple LLVM version 7.3.0 (clang-703.0.29) but whatever version you’re using is good enough to compile beginner’s code.
There are multiple standards for C, and the two you’ll come across most are C99 and C11. As a newcomer, you don’t need to worry too much about these, and you’ll almost certainly learn C99 then discover the new features in C11.
You’ll need a good text editor or IDE (Integrated Developer Environment) to write your C programs. TextEdit isn’t a good choice because it doesn’t use the plain text by default.
TextWrangler is a free program from the App Store, or you can download
Sublime Text, which has an indefinite trial mode (this is our current favourite text editor).
How to learn C in Mac OS X: How to create a Hello World program in C
C differs from other programming languages, like Python, in that you need to compile programs before you can run them. You’ll typically do this in C using the command “make”. C programs end with the “.c” extension, and you’ll run make to build a second file, that is the compiled program. The compiled file is what you run.
You can do all this from the command line, so let’s see how to make a traditional “Hello, World!” program.
Open your text editor and save a file called “hello.c” in your Home folder.
Enter the following code:
Note that we use four single spaces (” “) to indent the “printf” and “return” lines. You can also use a single Tab space, and there are plenty of vibrant arguments as to which is better. My stance is always to use spaces unless I’m working on somebody else’s code that uses tabs. What’s most important is not to mix and match styles. Either press the Space bar four times or press the Tab key.
- With the “hello.c” file saved in your Home folder, it’s time to compile it.
- Return to Terminal and enter cd ~ to make sure you’re working in the Home directory.
- Enter make hello and press Return. Note that you don’t include the “.c” extension.
- Terminal will display “cc hello.c -o hello”. If it displays any errors, then go back and check your code.
- When it compiles, there will be a second file called just “hello” in the home folder.
- Enter ./hello to run the hello program. You will see “Hello, World!” at the command line.
This process is how you build and make programs in C, at least when you’re starting. You write a C program in a text editor, and then compile it using make, and then run the compiled program to see the result.
Enter rm hello.c hello to get rid of both files from your Home directory.
How to learn C in Mac OS X: The best C resources and training programs
Now that you’ve created your first C program, you’ll be keen to get on and learn the C language. Fortunately, there are lots of different resources available for learning C. Here are some of the best places to get started.
C Programming for beginners. A popular course on Udemy, you’ll start from the very beginning in this 73 lecture course with 8 hours of video.
C Programming for beginners – Zero to Hero! A shorter Udemy alternative with 31 lectures and 3.5 hours of video.
Learn C the hard way. This classic course keeps you in the command line and teaches you the basics.
Learn C: This interactive tutorial walks you through the basics of C programming in an interactive website.
TutorialsPoint C. This is a great site to bookmark for looking up C functions and modules.
The C programming language. Straight from the horse’s mouth. This book was written by the people who originally created the C programming language.