C programming in Linux
For you guys (and gals) out there who wants to explore C programming in Linux, you'll be delighted to know that I'll be publishing a number of hubs dedicated to this topic. I've been programming in C for more than 10 years and I'm hoping that I can transfer this knowledge via easy-to-follow and easy-to-understand hubs.
In this hub, being the first one, I'll quickly show you the coverage that I expect to cover across the different hubs. As an intro, I'll show you how to get you setup with Linux and the C compiler called "gcc" that we'll be using. Overtime, I'll keep updating this hub so that it'll have clickable links to the hubs discussing each specific topic.
Please understand that even though we are using Linux here to create our C programs, you should not have much trouble using them on when programming C using other systems like Windows, Mac or others.
Here are the topics I wish to cover:
Formal introduction to C programming
Programming fundamentals in C
Functions in C
- User-defined functions. Library functions. External user-defined functions. Command-line arguments.
Controlling program flow
- Loops. If-Else. Switch-Case. Goto.
- Single and Multi-dimensional.
Abstract data structure
- Structure. Union. Enum.
- Variable. Function pointers.
- Print debugging. Debugger.
Right, let's get started, let's get your Linux setup...
Today, Linux is packaged and distributed from a lot of sources on the Internet, these are called "Linux distributions" or "distros" in geek-speak. Technically, you'll not only be installing Linux in your computer but also a bunch of applications that go with the distro; like a window manager, file editor programs, package/program and file managers, web browsers like Firefox and many more.
To see what are the available distros, point your web browser to http://www.distrowatch.com. Scroll down a bit and locate on the right-hand side of the page a list of the popular distros ranked according to the number of hits per day (HPD). It's wise to pick one from among the top ones like Ubuntu or Fedora or Debian since that means many people are using them which would tend to mean that they're stable and highly supported.
I'm using a Debian-based distro called "Joli OS". Therefore if you're using a Debian-based distro like Ubuntu, the commands I use here should apply to you directly. If not, however, you should not have any trouble finding out the equivalent for your chosen distro. If you're having trouble with any of the commands I use, just drop me a comment below and I'll be happy to answer.
Most distros should already ship the C compiler and linker programs that we need. If, however, they're not available, it should be trivial to install them.
What do we need?
Basically, other than Linux, you'll need the following:
- Terminal - a command-line interface where you enter commands or run programs
- C compiler - we'll use the free and open-source compiler called GCC
- Linker - we'll use the free and open-source linker from a package called "BINUTILS"
- Text editor - any text editor that you like; I use "vim".
I'll be using extensively the terminal so this is a good time to fire up (start up) that terminal program in your Linux box. To start a Terminal or Console, try to find it under Applications --> System or Applications --> Utilities. It'll be called "Terminal" or "Console" or "Konsole".
Below is a screenshot of a terminal called "Konsole" in a distro called "Kubuntu".
GCC C compiler
To test if you have the C compiler installed, simply type the following command on a terminal or console:
If you don't have the compiler installed, simply install it using a package manager that comes with your distro. In a Debian-based system, you can install this easily using the terminal using the following command: (In the example below I'm installing gcc version 4.3, you can safely install other versions if you like.)
sudo apt-get install gcc-4.3
Next is to check if you already have the linker installed, please enter the command below:
As in the compiler described above, if you don't have the linker installed, you can install it using your distro's package manager or enter the commands in the terminal:
sudo apt-get install binutils
We're almost done... now we'll need a text editor which we'll use to write our programs. As I've mentioned above, I'm using "vim", however, you are very much welcome and encouraged to choose a text editor that you feel like using. I will make some suggestions, however.
I would choose an editor with the following features:
- Syntax highlighting - Since you'll be writing C programs, it'd be helpful if the editor can colorize or highlight certain words (aka "syntax") in our program. This will help us prevent some really common typing mistakes.
- Light-weight - You will most likely be switching between your editor and the terminal to run your programs as you progress therefore you would want an editor that's not processor-heavy and memory-hungry; unless of course you have tons of CPU and memory power at your disposal.
- Built-in compile and run functions - Most editors designed for programmers will have a feature that allows you to compile and run your programs from within the editor itself. Most of these editors are called "IDE" or Integrated Development Environment. You'll have the advantage of not having to switch between your editor and terminal when programming which eases your life a bit.
- Powerful search functions - As your program gets longer, it'll help you a lot if you can quickly and flexibly search through your code. Some editors also offer a function that lets you jump to a certain line number in your code. And most editors even offer a search and replace function which replaces some text automatically.
- Macros support - This is an advanced feature that you might not use at the start. However as you get more experienced with writing code, you'll notice that often you will be constantly repeating some keystrokes in various lines of your code. A "macro" is a function that allows you to save those keystrokes so that you can replay them on a different line in your code; a real time-saver.
Some of the popular text editor of choice when programming C:
- ViM - www.vim.org
- Geany - www.geany.org
- Nano - www.nano-editor.org
- Eclipse - www.eclipse.org
- Emacs - www.gnu.org/software/emacs
So there you have it! I hope this hub has helped you get ready for C programming in Linux. (...and the sleepless nights ahead... just kidding!)
More by this Author
The Philippines is a country that's pretty visible on the map of the world in the South East area in Asia. It's population to this date has just gone a little over the 90 million mark. A fraction of this...
The C programming language provides a rich set of operators that you can use in manipulating data. Operators are symbols like "+" and "=" that are used in conjunction with variables and constants in...
Computer programs manipulate data in order to be useful. Data that can come external to the program, like data coming from the user or from a file or from a device (e.g. network). This is considered as "input"...
No comments yet.