How C Programming Works

The C programming language is incredibly popular, and it’s easy to see why. Programming in C is efficient and gives the programmer a great deal of control. Many other programming languages like C++, Java and Python were developed using C.

Chances are increasing each day that if you’re a programmer, you won’t use C exclusively for your work. However, there are several learning C is highly beneficial, even if you don’t use it regularly. Here’s why:

You’ll be able to read and write code for software that can be used on many different types of computer platforms, including everything from small microcontrollers to desktop, laptop and mobile operating systems.

You’ll better understand what high-level languages are doing behind the scenes, such as memory management and garbage collection. This understanding can help you write programs that work more efficiently.

If you’re an information technology (IT) specialist, you could also benefit from learning C. IT professionals often write, maintain and run scripts as part of their job. A script is a list of instructions for a computer’s operating system to follow. To run certain scripts, the computer sets up a controlled execution environment called a shell. Since most operating systems run shells based on C, C shell is a popular scripting adaptation of C used by IT pros.

This article covers the history behind C, looks at why C is so important, shows examples of some basic C code and explores some important features of C, including data types, operations, functions, pointers and memory management. Though this article isn’t an instruction manual for programming in C, it does cover what makes C programming unique in a way that goes beyond those first few chapters of the average C programming guide.

Let’s start by looking at where the C programming language came from, how it has developed and the role it has in software development today.

What is C?

The simplest way to define C is to call it a computer programming language, meaning you can write software with it that a computer can execute. The result could be a large computer application, like your Web browser, or a tiny set of instructions embedded in a microprocessor or other computer component.

The language C was developed in the early 1970s at Bell Laboratories, primarily credited to the work of Ken Thompson and Dennis Ritchie. Programmers needed a more user-friendly set of instructions for the UNIX operating system, which at the time required programs written in assembly language. Assembly programs, which speak directly to a computer’s hardware, are long and difficult to debug, and they required tedious, time-consuming work to add new features .

Thompson’s first attempt at a high-level language was called B, a tribute to the system programming language BCPL on which it was based. When Bell Labs acquired a Digital Equipment Corporation (DEC) UNIX system model PDP-11, Thompson reworked B to better fit the demands of the newer, better system hardware. Thus, B’s successor, C, was born. By 1973, C was stable enough that UNIX itself could be rewritten using this innovative new higher-level language .

Before C could be used effectively beyond Bell Labs, other programmers needed a document that explained how to use it. In 1978, the book “The C Programming Language” by Brian Kernighan and Dennis Ritchie, known by C enthusiasts as K&R or the “White Book,” became the definitive source for C programming. As of this writing, the second edition of K&R, originally published in 1988, is still widely available. The original, pre-standard version of C is called K&R C based on that book.

To ensure that people didn’t create their own dialects over time, C developers worked through the 1980s to create standards for the language. The U.S. standard for C, American National Standards Institute (ANSI) standard X3.159-1989, became official in 1989. The International Organization for Standardization (ISO) standard, ISO/IEC 9899:1990, followed in 1990. The versions of C after K&R reference these standards and their later revisions (C89, C90 and C99). You might also see C89 referred to as “ANSI C,” “ANSI/ISO C” or “ISO C.”

C and its use in UNIX was just one part of the boom in operating system development through the 1980s. For all its improvements over its predecessors, though, C was still not effortless to use for developing larger software applications. As computers became more powerful, demand increased for an easier programming experience. This demand prompted programmers to build their own compilers, and thus their own new programming languages, using C. These new languages could simplify coding complex tasks with lots of moving parts. For example, languages like C++ and Java, both developed from C, simplified object-oriented programming, a programming approach that optimizes a programmer’s ability to reuse code.

Now that you know a little background, let’s look at the mechanics of C itself.

Editing and Compiling C Code

C is what’s referred to as a compiled language, meaning you have to use a compiler to turn the code into an executable file before you can run it. The code is written into one or more text files, which you can open, read and edit in any text editor, such as Notepad in Windows, TextEdit on a Mac, and gedit in Linux. An executable file is something the computer can run (execute). The compiler checks the code for errors and, if it seems to be error-free, creates an executable file.

Before we look at what goes into the C code, let’s be sure we can find and use a C compiler. If you’re using Mac OS X and most Linux distributions (such as Ubuntu), you can add a C compiler to your computer if you install the development tools software for that particular OS. These free C compilers are command line tools, which means you’ll typically run them from a command prompt in a terminal window. The command to run one of these C compilers is “cc” or “gcc” plus some command line options and arguments, which are other words typed after the command before you press Enter.

If you’re using Microsoft Windows, or you would prefer to use a graphical user interface rather than a command line, you can install an integrated development environment (IDE) for C programming. An IDE is a single interface where you can write your code, compile it, test it and quickly find and fix errors. For Windows, you could purchase Microsoft Visual C++ software, an IDE for both C and C++ programming. Another popular IDE is Eclipse, a free Java-based IDE that runs on Windows, Mac and Linux and has extensions available for compiling C and many other programming languages.

For C, as for other computer programming languages, the version of the compiler you use is very important. You always want to use a version of the C compiler that’s as new or newer than the version of the C language you’re using in your program. If you’re using an IDE, be sure to adjust your settings to make sure the IDE is using your target C version for the program you’re working on. If you’re at a command line, you can add a command line argument to change the version as in the following command:

gcc –std c99 –o myprogram.exe myprogram.c

In the command above, “gcc” is the call to run the compiler and everything else is a command line option or argument. The “-std” option was added followed by “c99” to tell the compiler to use the C99 standard version of C during its compiling. The “-o” option was added followed by “myprogram.exe” to request that the executable, the compiler’s output file, to be named myprogram.exe. Without “-o” the executable is automatically named a.out instead. The final argument “myprogram.c” indicates the text file with the C code to be compiled. In short, this command is saying, “Hey, gcc, compile myprogram.c using the C99 C programming standard and put the results in a file named myprogram.exe.” Browse the Web for a complete list of options you can use with your particular compiler, whether it’s gcc or something else.

With your compiler installed, you’re ready to program in C. Let’s start by taking a look at the basic structure of one of the simplest C programs you could write.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: