In this section:
C is one of the oldest programming languages still in popular use today. It is used extensively for developing operating systems and commercial applications. One of the best reasons to learn C or C++ is to get an appreciation for these endeavors even if you never have to use C or C++ in the applications that you build yourself. A great place to start learning C is Kernighan and Ritchie's classic book The C Programming Language31.
The main aim of this section is to give a basic appreciation of the language and be able to invoke C / C++ programs written by others in a web environment. You may want to do this because the C / C++ program may not have a web interface, only a command line interface. One important example of this is the Basic Local Alignment Search Tool (BLAST) program described later in this article.
Here is the basic hello world program in C.
The first line in the program is the include preprocessor directive. It tells the C preprocessor to include the standard input / output (stdio) library. Preprocessing is a step that sets up the compilation process. Compilation of C and C++ programs results in an output file or files that contain machine instructions. These are called executable binary files or executable binaries.
The next line in the program is the main function definition. This is the entry point of every C / C++ program but it is declared just like any other function. The printf function prints out the famous line from Kernighan and Ritchie.
To run the program you need to compile it first. The GNU C compiler GCC comes pre-installed on Linux and is freely available for many other platforms, including Windows. However, on Windows the commonly used standard is Microsoft Visual C++. To compile with GCC and run this program use the commands
The output of the compilation process is native code - i.e. matching instructions executed directly by the CPU. There is no intermediate form, such as Java bytecode.
That program is also a valid C++ program. However, C++ extends the C language to include object oriented (classes) and generic (templates) programming.
Command line arguments are passed in the main function as demonstrated in the example below.
The main function now declares two parameters. The first parameter, argc,
is the number of arguments passed to the program. The second parameter, argv,
is an array with the arguments. The program first prints out the
number of arguments passed to the program and then iterates over them
with a for loop. The for loop should
look familiar to Perl and Java programmers. The syntax in those
languages was based on C. In C programs all variables must be
declared with a type at the start of the program or function in a
variable declaration block. This is demonstrated by the
declaration of the integer (int) variable i. In C++
the variable declarations may be mixed with the instructions.
Name the file echo_args.c. Compile and run it with the command
The output is
The first argument is always the name of the command that invoked the program.
To go further with C or with C++ you will need to look elsewhere than this article. See the page C and C++ Resources on this web site for a list of popular and user suggested online resources.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.