Sunday, 3 January 2016

First program in C

Now, we've installed C compiler on our system and also verified that it is working fine. Now let's write another program which takes input from the user and greets him/her. Side by side we'll look into different building blocks of the program.

So, here we go with our first program which greets the user by his/her name

#include <stdio.h>

 * This program takes user input and greets the user by his/her name.
 * Author: Palash Kanti Kundu
 * Since: February 25, 2016
int main(){
        // This is variable declaration
        char name[25];
        printf("Enter your name: ");
        //This line takes input from the user
        fgets(name, 25, stdin);
        //This line prints the greeting message
        printf("Hello %s", name);
        return 0; 

Let's first run this program and see it working, then we'll go into the analysis of the program.

From here onwards, keep in mind that there are two steps to run a C program,
  1. Compile the program
  2. Run the program
We'll know why we've to perform these steps. Let's first perform these tasks and give it some try.

$ ls
$ gcc hello_user.c
$ ls
a.out  hello_user.c

For convenience, I started with the file list before compiling.
So, as you know that in the first line I am checking what are the available files in the present directory.

Then, I have compiled the program with the file name. The program has successfully compiled and generated an output file a.out.

So, there is a problem, if you compile more than one program, then the system will retain the last compiled program output file and will overwrite the previous one.

A big problem right ?

Well, the compiler helps us with an option to specify the output file name. Let's take a look on this option.

$ ls
a.out  hello_user.c
$ gcc hello_user.c -o hello_user
$ ls
a.out  hello_user  hello_user.c

Now, if you see that there is another file in the directory with the name hello_user.
So, if you want to safely compile your program without omitting any other program, use -o.

Similarly you can also specify your output files to be stored in any particular directory if you wish. For example, the following line of command compiles the file in src directory and stores the output file in executables directory.

palash@ubuntu:~/git_repo/c/src/blog$ pwd
palash@ubuntu:~/git_repo/c/src/blog$ gcc hello_user.c -o ../../executables/blog/hello_user.out
palash@ubuntu:~/git_repo/c/src/blog$ ls
palash@ubuntu:~/git_repo/c/src/blog$ cd ../../executables/blog
palash@ubuntu:~/git_repo/c/executables/blog$ ls

For program files this option may not excite you but if you have multiple programs in a directory, then you will find this option very much helpful.

Well that was all about compilation for the time being, now let's run this program. In UNIX/Linux/Mac OS, you can run any executable using ./ command.

For example, we have made hello_user.out output file which is an executable file. So we'll run this program with the following command.

palash@ubuntu:~/git_repo/c/executables/blog$ ./hello_user.out
Enter your name: Palash kanti Kundu
Hello Palash kanti Kundu
palash@ubuntu:~/git_repo/c/executables/blog$ ./hello_user.out
Enter your name: Totan Das Kundu
Hello Totan Das Kundu
palash@ubuntu:~/git_repo/c/executables/blog$ ./hello_user.out
Enter your name: Annu Singh   
Hello Annu Singh
palash@ubuntu:~/git_repo/c/executables/blog$ ./hello_user.out
Enter your name: Debasis Das
Hello Debasis Das
palash@ubuntu:~/git_repo/c/executables/blog$ ./hello_user.out
Enter your name: Mita
Hello Mita
palash@ubuntu:~/git_repo/c/executables/blog$ ./hello_user.out
Enter your name: Debabrata Halder
Hello Debabrata Halder

These are sample run of the same program.

For Windows System you will find generated .exe files in the directory. Simply type the output file name in PowerShell and you'll find the similar output.

Now we've run the program and have verified the output against different input.

It's time to dissect the program and verify line by line of the program. We'll look into this section with each of the building blocks of the program.

Preprocessor Directive

The first line of the program includes another file. These type of commands are known as preprocessor directives. This basically instruct the compiler to load some other files from library. Like in our case the library file has the name stdio.h this file comes with any C distribution. It has some built-in functions for system calls.

If you modify the program and remove this line, then compiler won't understand the system calls and will not compile.

Try this yourself.

Blank Lines

The second line is left blank. This line will be ignored by the compiler. In fact you can have as much blank lines as you want and all these lines will be ignored by the compiler.


The third line starts with /* and the seventh line ends with */, this is known as block comments. Any lines within these two constructs are ignored by the compiler. Comments facilitate the purpose of human reading of a particular source code. So that anyone reading the program can understand what purpose a particular piece of code serves. Better comments are necessary for readability and maintainability of the code.

Apart from the paragraph comments, C also supports line comments. Any line starting with // are also ignored by C Compiler. As in our case, we have used line comments on 9, 12 and 14 to describe the purpose of each line of code.

You can use any type of comments any where in the program but conventionally, block comments are used to describe a block of code where as the line comments are generally used for fewer lines of code.


In line 8, we've written int main(). Actually this is a function declaration.
Function is an important building block for a C program. This is a bigger topic to discuss. For now just remember that, we need a function to execute our code. and to run our program we need int main(). Without this line our code will not compile.
If you are curious to know the consequence, just try it out. Its cool thing to try.

Variable Declaration

In line 10, we have declared a variable name. If you have knowledge of some other language you will understand this part. If not, then remember that for performing any input/output or calculation, we need variables.
Variables are nothing but a reference of a location in the memory for fine grained control. We'll discuss this topic in detail in our subsequent section.

Well, if you can't wait you can take a look on the variables. Although the article is targeted for Java, you will get some concept.

Function calls

In line 11, 13 and 15, we've used function calls. We are basically calling built in functions to facilitate working with the hardware. These functions actually deal with the Operating System. We use these functions to instruct Operating System to perform some specific task.

Return Statement

In line 17, we've written return 0;. This is known Return statements. This statements indicates that the function has completed its tasks and if required also returns some output from the function.
This is also a part of functions. When we'll talk about functions, we'll check this guy out.


In line 8 and 17 we've used {}. These known as parentheses/braces. These indicate a block of code. These come in pairs. In a program the number of { should match the number of }.
Keep an eye on these. They are often the cause of compilation failure.

Well, that's all about our first program.

After this article you should be able to 
  1. Compile and run a C program
  2. Identify different type of comments
  3. Identify function calls
  4. Determine the error caused by missing parentheses

Source Code Distribution

The source code of this article can be found on GitHub. You can check out the source code for verification purpose.
Although you can download, compile and run the source code, it is not a recommended practice and defeats the purpose of learning. The source code is meant for verification purpose only.

Source Code Link:

Prev     Next

No comments:

Post a Comment