CodingBison

This module discusses C, which is undoubtedly one of the most popular programming languages. Several languages are based on C. C++ extends C by adding object-oriented paradigm, among others. Java uses a similar syntax as that of C. CPython -- one of the popular versions of Python -- is written on top of C.

This module begins with a coverage of basic C concepts like variables, macros, arrays, strings, enums, loops, conditional statements, etc., and then moves on to advanced concepts like pointers, data-structures, linked-lists, socket-programming, POSIX threads, and many more! We supplement all of these discussions with a lot of code examples to facilitate understanding.

Elements of a C program

Stated simply, a C program is essentially a collection of statements that follow of rules (syntax). Let us dive right into it and take a look at a simple C program -- we use this program to understand some of the common syntax element. So, here is the program:

 #include <stdio.h>

 int main () {
     /* This will print "Hello World". */
     printf("Hello World\n");

     return 0;
 }

The above program begins by including a header file ("stdio.h") -- "stdio" is short for standard input output (IO). Header files, like "stdio.h", include definitions of functions, data structures, constant values, etc. For example, one of the function definitions provided by "stdio.h" is that of the printf() call, which is used for printing values on the standard output. If we were to call printf() function without including the "stdio.h", the compiler would complain that it can not find definition of printf() function. We will revisit functions and printf() later.

Next, every C-program needs to have a main() function. When we run the program, the program runs this function before any other function. A C program would typically have a (whole) lot of functions and all of these functions are called directly or indirectly by the main() function; the latter means that function gets called by a function that gets called by the main function. You get the idea! And if we do not have the main() function, then the compiler would be certain to complain.

Moving on, we notice the line that contains the printf() function; the reason why we included "stdio.h"! C programs contains several such lines, each of them are called statements.

The next thing to note in the above program is that the line containing printf(), ends with a semicolon. A semicolon tells the compiler that it has reached the end of the current statement and thus, is different from the next statement (or the previous) statement. Using a semicolon to terminate a statement is a popular style that is used in other languages as well: C++, Java, PHP, JavaScript, etc. However, in JavaScript using semicolon to terminate a statement is optional! So, if you have a JavaScript background, now would be a good time to take a note.

C supports additional types of statements as well. Some statements assign values to variables. Some statements can take input from the user. C also has control statements to provide control-logic to programs: conditional clauses like "if-else", looping clauses like "while", "for" etc. Please note that control statements typically do not require a semicolon at the end. These statements are the building blocks of any C program.

Further, the program contains text enclosed between "/*" and "*/" -- this is a comment and the compiler ignores it. Thus, using these opening and closing tags, we can add helpful notes explaining the program logic. In real life, it is not uncommon from programmers to skip adding comments; lack of comments can make code less readable for future programmers who need to maintain or extend it. Trust me, adding good comments would help you earn a lot of karma points!

In the end, the program contains a "return 0" statement -- this statement returns 0 to the caller of this program (e.g. the shell from where we run this program). In C, each function can return a value and the main() being a function itself, can also do the same. A return value of 0 from the main() function means that the program was run successfully.

Compiling and Running a C program

Once we have written the program and saved it to a file (let us call it "hello.c"), the next step is to compile it using a C compiler; it is this step that converts a simple text file storing the program into an executable that we can run. C differs from scripting languages like Python, Perl, JavaScript, where no compilation is necessary -- for those languages, the programs are checked for syntax errors during the run-time itself.

Compilation is a necessary step since if the program has any syntax errors (like, no semicolon at the end of a statement), then the compiler would tell us that. And the compiler would allow us to go only after we fix all of those errors. For beginner programmers, it is common to have frequent nasty fights with the compiler; if we run into a compilation error, we should just revisit the syntax rules. If we still face the issue, then we should try to google the error -- it is almost guaranteed that some poor soul must have run into the same issue! We should take comfort that as we write more code, spotting syntax errors starts to come naturally.

For our example, we use gcc compiler -- gcc stands for GNU compiler collection and contains compiler for various languages. To be more precise, we use gcc version 4.6.3 on Linux for this example and for all examples in this module. Besides gcc, there are other compilers as well (like Unix cc or Borland's Turbo cc (tcc)).

We have to keep in mind the operating system where we wish to compile C programs. If we are using systems other than Linux, then we might need the relevant compiler or a compiler bundle; for example, for Mac OSX systems, we need to install Xcode Developer Tools that contains the gcc compiler. If we are using Windows, then we can use Borland's Turbo C. Note that it is likely that these compilers may be already installed on the system; if not, then we need to do the hard-work and install them.

When we compile a program, the output is stored in "a.out" by default. If we want, we can use the "-o" option to store the output in another location ("hello_o"). Once we have compiled, we can run the executable, "hello_o".

 $ gcc --version
 gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)
 Copyright (C) 2011 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 $ 
 $ ls -alh hello.c 
 -rw-rw-r--. 1 user  user 105 Jan 12 22:12 hello.c
 $ 
 $ gcc hello.c -o hello_o 
 $                       
 $ ./hello_o 
 Hello World 




comments powered by Disqus