Understanding C Programming Basics


So, finally you decided to learn C programming either because it is in your academic syllabus or you to get a job or it is really interesting to you. Whatever may be the case, as learning the Basics of C Programming is the first step, I will make sure you learn the right things in the right way.

I will take you through an introduction of the C Language and then explain you the basics of it using a simple Hello World program.

Introduction to C Programming

Introducing C, a programming language designed to program the electronics systems (computer, Washing Machine, Phone etc.), just like the English language is meant to be spoken and understood by the humans.

It is a high level language because it is more human readable and less dependent on system’s architecture (more on this in a separate article).

You take any programmable device or an Operating system, and you will find C Programming is their foundation.

What do I mean by a Programmable Device?
An electronic device that runs on some sort of software programs when powered on.

For example: an Induction Cooker, a Wifi router, a Computer system, an AC, they are all running C code at their core.

Similarly, Unix OS and Linux Kernel are completely written in C (and Assembly language).

And rest all, be it MacOS or FreeBSD (not sure of Windows though), they are just derivatives of one of the above hence all have C code.

So, let’s begin understanding C Programming basics from a simple Hello, World Program.

Starting with a Basic Program in C

When you start learning to ride a bicycle, you do not learn the nuts and bolts first, right? You just hold the handle, sit on the cycle and start riding it.

Similarly, to teach you the basics of C programming, I will take the example of this simple Hello World program.

If you go to the above link, the post explains everything about the program in-detail.

But to keep the context intact and teach you the basics, I will go through the program again but very briefly yet without losing any important thing.

The C Code – Hello World

#include <stdio.h>

/* The entry point function in C */
int main () {
    /* The Hello World print statement */
    printf ("Hello, World!");

    return 0;
}

Create and save this code in a filename called let’s say hello.c

The above code contains preprocessor directive ( #include ), comments ( that starts with /* ), function definition ( main ( ) ), function call ( printf ( ); ) and a simple statement ( return 0; ).

Let’s compile and execute this program and then explore more about it.

Compile the Code

If you are using an IDE like Code Blocks or C Eclipse IDE, then you need to click on the compile button.

If you are using Linux, MacOS, or FreeBSD you can compile the same using gcc compiler. You can execute the below command to compile the above Hello World code:

gcc hello.c

The above command will generate a filename a.out

If you want to modify the name of the output executable file, then execute the below command:

gcc hello.c -o hello.out

The above command will generate a filename hello.out

Run the Executable File

Depending on your IDE, click on the Run / Execute button.

If you are using Linux, MacOS, or FreeBSD, run the below command based on your output filename:

./a.out

or

./hello.out

 

The Output

You will see the below output on your IDE console window or the command prompt window:

Hello, World!

This program looks pretty simple but for a beginner, there are a lot of things inside.

Understanding C Programming Fundamentals

We will go line by line to understand the above Hello World program. But before that, let’s go for a little bit of theory.

Below are the key points to remember before we go for the code walk through:

  • C language is a modular programming language and each module is represented in a function.
  • Any executable code in C programming must reside inside a function.
  • A function in C is represented by its name followed by two parenthesis. Example: main ( )
  • A function is first declared before it is defined or used. That is why you may notice 3 kinds of different representation of a single function in C. (This statement might seem confused right now, but I will explain about this in the right time, may be in a new post)
  • Every statement written in a C program is case sensitive
  • Any C statement must end with a semicolon. ;

 

The # include:

#include <stdio.h>

This statement tells the compiler to include the file stdio.h

We have used or called the the printf ( ); function inside the main function. So, as I told above, we need to first declare the function type before we can use it.
And the declaration lies inside the stdio.h file, so we directed the compiler to include that file first before moving further.

The main ( ) function:

/* The entry point function in C */
int main () {

main ( ) is the first function of a user program to be executed.

So, anything that we want to execute in a C program, must reside inside the main ( ) function.

We wanted to print Hello, World! so we used a printf (); statement inside the main ( ) function.

I hope things are clear till now.

The printf ( ); statement:

    /* The Hello World print statement */
    printf ( "Hello, World!" );

Printf function prints anything that is passed in the double quote " " inside the 2 parenthesis.

This is called as an argument to the function. Think of an argument as an input to it.

So, "Hello, World!" is an argument to function printf ( );

The return 0; statement:

Imagine you have assigned a task to someone. So, you would like that person to return a status of the task, right?

As, in C, the executable module is a function, it has to return a status back to the caller function.

After main ( ) function is done with its execution, it must send a status back to who has called main ( ).

So, we return 0; in our case. Returning 0 means returning SUCCESS.

[su_note note_color=”#D7E3F4″ text_color=”#728095″ radius=”3″ class=”” id=””] NOTE:
The way we have called the printf ( ) function from main ( ), similarly, some other function have called the main ( ) function to be executed.

We can choose what we call inside main ( ) but we cannot change who and how the main ( ) function is called.
[/su_note]

So, What are the Basics of a C Program?

C program is a modular language. A function is its basic module.

Of course there are more than a function in C, but if you understand the importance and basics about functions in C, you can get a hold of almost everything in C in a later time.

Any Doubts?

It is pretty hard to convey what exactly I wanted to tell and on the other hand what have you understood.

That’s where the comment box comes into picture.

If you were unable to understand few things here and there, you can leave a comment so that I could help you out.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.