No matter who you are, C is a valuable programming language to know. Common programs we use such as Ruby, Bash, and almost every OS uses C (and maybe some Assembler) at its core!
I decided to compose this blog post for the people who don't know C, or any compiled language for that matter, because C is highly underrated over its cousins C++ and Objective-C. C is messy once you get a large code base, but C can be clean too, that's the programmers job.
Why should I learn C?
- C is extremely fast
- C is used to build programming languages
- C is very useful on the Raspberry Pi, Arduino, etc.
- C programs can run on almost any OS without modification (unless you use Windows libraries, which I would always avoid unless specifically targeting Windows)
- Knowing C helps you understand C++ and Objective-C much better
- Knowing C helps you understand any language better, because you know what it's doing at its core
- C is used to write native extensions for Ruby, and other language (faster code written in C but creates functions in the language)
- C is used to program robots, LED screens, and other hardware that we use in our everyday life
- Since C was made almost 50 years ago, it has all the bugs worked out, and thousands of libraries and tools
Convinced? Let's make a little C program!
Writing Your First C Program
C programs usually end with the .c extension, and have a header file with the same name but it ends with .h. We'll get to header files later on, because first you need to understand the basics of C.
Go ahead and create a file called playground.c. playground isn't a bad name considering it is where we fool around with various C code.
Open playground.c in your favorite text editor, and type the following code into the file:
// This is a comment, you obviously don't
// need to type these in, as they just explain what our
// code is doing.
#include <stdio.h>
int main() {
printf("I'm a C program!\n");
return 0;
}
Now that you've written the program, run gcc playground.c -o playground in your command line. You'll get an executable file called playground. Run the executable using ./playground. At last, you should see the following appear in your command line:
I'm a C program!
NOTE: if gcc doesn't work, see the section below Compiling a C Program.
Congratulations! You've successfully written your first C program!
What is Our Code Doing?
C has something called a flag. Flags always start with # with some sort of word following it. Generally flags are used to include other C files or define macros (we won't do macros in this tutorial because they're a fairly complex subject).
We're beginning the file by using the #include flag to import some C code called stdio.h. stdio.h is a built-in C library on every OS, and contains basic input/output functions like puts and printf.
Next in line, we're creating a function called main(). Every C program has a function called main() that does everything in the C program. You can define custom functions, just like every other language, but C needs to find a function called main() to work. Before main() we have the word int, since main() returns the integer zero. Since zero is false, it returns false, which basically returns nothing without the use of void (which I'll explain in the next tutorial).
In the main function, we call a function called printf(). printf() is a basic function that prints a format to the screen, which is a string that can be interpolated (have dynamic values added to it).
Finally, we return the value zero since we declared main() would return an integer.
Compiling a C Program
We compiled our C program using an open source C compiler called gcc. If it doesn't work, try the following:
- For Mac users, try installing the Xcode command line tools (go to Xcode > Preferences > Downloads > Click "install" next to the "command line tools" column)
- For Windows users I'd recommend getting the Visual C++ tools or something like MinGW (I'm Unix for life)
- For Linux try running
apt-get install gcc, or search around for other C compilers
If it did work, you basically just compiled playground.c and spit out the binary output to a file called playground, which you can run via the command line.
Every C program that has a main() function can be compiled to a pure binary file (which is really fast), and have its output spit to a specified file.
Tip: gcc's -o option doesn't have to be included, by default the output will be in a file called a.out. Some people end their C output with .o, but it's completely optional and isn't commonly used for a project's main executable.
Let's Make Another C Program
Now that you completely understand how to compile, run, and write some very basic C code, let's do another more complex program. Delete the playground executable, and type the following in (you can delete the comments in the actual file):
// Let's include some more C code to use
// in our program this time... all the functions in
// string.h!
#include <string.h>
#include <stdio.h>
// Here we'll define a custom function that gets the length
// of the string "hello"
//
// we return an integer with the number of characters in the
// string "hello"
int hello_length() {
return strlen("hello"); // strlen() might look a bit weird, but every C function has shortened words
// note that you need to include string.h to use the strlen() function
}
int main() {
// we use %i to add an integer to the string, then
// use a comma to specify that the return value
// of hello_length() is the integer we want to include
// in the string
printf("Hello has %i characters in it.", hello_length());
return 0;
}
Compile playground.c again, and run the playground executable. You should get the following output as expected:
Hello has 5 characters in it.
Congratulations again! You defined your own function that returns the length of "hello".
Til' Next Tutorial
I can't write everything, so for now this is the end of part 1. Part 2 should be out soon showing you how to define your own return types, use types other than int, define structs, and more awesome stuff.
Thanks for reading, and don't forget to research a lot!