Want to get inside the computer’s head?
Need to write high-performance code for a new game? Program an Arduino? Or use that advanced third-party library in your iPhone app? If so, then C’s here to help. C works at a much lower level than most other languages, so understanding C gives you a much better idea of what’s really going on. C can even help you better understand other languages as well. So dive in and grab your compiler, and you’ll soon get started in no time.
The C language is designed to create small, fast programs. It’s lower-level than most other languages; that means it creates code that’s a lot closer to what machines really understand.
Computers really only understand one language: machine code, a binary stream of 1s and 0s. You convert your C code into machine code with the aid of a compiler.
C is used where speed, space, and portability are important. Most operating systems are written in C. Most other computer languages are also written in C. And most game software is written in C.
There are three C standards that you may stumble across. ANSI C is from the late 1980s and is used for the oldest code. A lot of things were fixed up in the C99 standard from 1999. And some cool new language features were added in the current standard, C11, released in 2011. The differences between the different versions aren’t huge, and we’ll point them out along the way.
To create a full program, you need to enter your code into a C source file. C source files can be created by any text editor, and their filenames usually end with .c.
Let’s have a look at a typical C source file.
So let’s look at the main() function in a little more detail.
C is a compiled language. That means the computer will not interpret the code directly. Instead, you will need to convert—or compile—the human-readable source code into machine-readable machine code.
To compile the code, you need a program called a compiler. One of the most popular C compilers
is the GNU Compiler Collection or gcc
.
gcc
is available on a lot of
operating systems, and it can compile lots of languages other than C.
Best of all, it’s completely free.
Here’s how you can compile and run the program using gcc
.
You can compile and run your code on most machines using this trick:
This command will run the new program only if it compiles successfully. If there’s a problem with the compile, it will skip running the program and simply display the errors on the screen.
Do this!
You should create the cards.c file and compile it now. We’ll be working on it more and more as the chapter progresses.
Congratulations! You have compiled and run a C program. The
gcc
compiler took the
human-readable source code from cards.c and
converted it into computer-readable machine code
in the cards
program. If you are
using a Mac or Linux machine, the compiler will have created the
machine code in a file called cards. But on Windows, all programs need
to have a .exe extension, so the file will be
called cards.exe.
The C language doesn’t support strings out of the box.
C is more low-level than most other languages, so instead of
strings, it normally uses something similar: an array of
single characters. If you’ve programmed in other languages,
you’ve probably met an array before. An array is just a list of things
given a single name. So card_name
is just a variable name you use to refer to the list of characters
entered at the command prompt. You defined card_name
to be a two-character
array, so you can refer to the first and second character
as char_name[0]
and char_name[1]
. To see how this works, let’s
take a deeper dive into the computer’s memory and see how C handles
text...