C Programming Language - Introduction

C is a procedural programming language that was designed by American computer scientist Dennis Ritchie. The language - which first appeared in 1972 - was developed by Ritchie in association with Bell Labs. Despite being close to 50 years old, the C programming language has not lost its relevance in the modern day computer software development scene. It's still used in time and speed critical areas like OS and firmware development (to name a few).

Several of the newer languages like C++, Java, PHP, and more are based on C. And that's the reason students are still advised to get proficient in C in order to have good command on any of the newer languages based on C. So keeping this importance in mind, we will kick-start a series of tutorials where we will be discussing the basics of the C programming language.

Please note that we'll be using Linux for all our examples and explanation. Specifically, we'll be using Ubuntu 18.04 LTS.

Basic C program

To begin with, let's take a look at a simple C program.

#include <stdio.h>

int main (void)
{
    printf("\n Hello World \n");
    return 0;
}

So you can see the program begins with a #. In C programming language, any line that begins with a # is dealt by preprocessor at the first stage of compilation of the program. We won't go in to the specifics of compilation stages, but for the time being, keep in mind that the first thing that will happen during compilation of this program is that the line beginning with # will get replaced by whatever is there in the stdio.h header file.

Then comes the next line: 'int main (void)'. This is basically beginning of a function called 'main' which returns an integer (int) value and accepts nothing (void). It's worth mentioning here that every C program that you'll see consists of one or more functions. The 'main' function is the one where the execution starts once the program is run. While all other functions get called from within main or other functions (meaning you have control over their calling sequence), main itself gets called as the first function from within the system.

Moving on, then you see a curly bracket ( { ). This basically defines the beginning scope of a function. Of course, towards the end, you'll see a reverse curly bracket ( } ), which defines the end of scope of the function. All the instructions inside these brackets are treated as part or body of the function.

Here, there are two lines of code in the body of the 'main' function. The first is 'printf("\n Hello World \n");'. Printf is a system library function that prints formatted strings on STDOUT. For now, just keep in mind that it prints anything you supply within double quotes (" ") except escape sequences (like '\n', which is translated into a newline). The second line of the body is 'return 0'. It basically marks the end of the 'main' function and sends '0' as the result to the function that called 'main'.

So all in all, we should expect this program to print 'Hello World' in output.

Compile and execute C program

Now that we have understood a simple C program, lets go ahead and execute it. So the first step would be save the code in a file named, say, hello-world.c. You can use the Vim editor to do this. Once you save the code in hello-world.c, make sure you have the gcc tool installed on your Linux system. If not, you can download and install (at least on Ubuntu) using the following command:

sudo apt install gcc

Gcc is basically a GNU compiler for the C programming language. Once it's there on your system, just use it in the following way to compile the hello-world.c program:

gcc -Wall hello-world.c -o hello

Here, -Wall is a gcc command option that enables a lot of compilation time warnings that the compiler would otherwise not throw. The other option you see is -o, which is used to specify an output file name. So in this case, we want the output file to be named 'hello'.

As you run the command mentioned above, you'll see a file named 'hello' will be produced. It's an executable file. you can run it in the following way:

./hello

For example, when I executed this command, the following output was produced:

Hello World Program in C Programming Language

So you can see, 'hello world' was produced in the output.

Conclusion

In this article, we learned the very basics of C programming language using a demo program. Now that we know how to write, compile and execute a C program, we will step into other aspects in the next article. Stay tuned. 

Part 2 - C Preprocessors

Share this page:

1 Comment(s)