Introduction to C Programming and Data Structure.
Tags: coding, c, beginners, learning
Published: 2023-08-20 10:02:54

From the smallest electronic devices to the largest supercomputers, computers are an essential part of our daily lives. Despite the wide variety of computer types, they all share one thing in common: they are all driven by the C programming language (S, 2023).

Four important aspects of any programming language are: 
- the way it stores data, 
- the way it operates upon the data, 
- how it accomplishes input and output, and 
- how it lets you control the sequence of execution instructions in a program.

C is a programming language developed at AT & T's Bell's Laboratories of USA in 1972 by Dennis Ritchie and became popular because it is simple and easy to use. The C progrmaming language has been one of the most popular programming languages for the past 40 - 45 years. 

Here are several reasons why you too should learn C as one of yor first programmming languages:

**1.** It makes more sense to learn C first and then migrate to C++, C#, or Java. Though this two-step learning process may take more time, at the end of it, you will definitely find it worthwhile of the trouble. C++, C#, or Java make use of the Object Oriented Programming (OOP) principle to organize programs which offers many advantages. While using this organizing principle, you need basic programming skills (Kanetkar, 2020).

**2.** Major components of well-known operating systems like Windows, UNIX, Linux, and Android are written in C. In addition, device driver programs, which are only written in C, are necessary to extend the operating system to work with new devices (Kanetkar, 2020).

**3.** Common consumer goods like microwaves, washing machines, and digital cameras are becoming smarter on a daily basis. This smartness comes from a microprocessor, an operating system, and a program embedded in these goods. These programs have to run quickly and operate in a limited amount of memory. C is the language of choice when building such operating systems and programs (Kanetkar, 2020).

**4.** The popular gaming frameworks (like DirectX) that are used for developing such games are written in C. You must have seen several professional 3D computer games where the user navigates some object, say a spaceship, and fires bullets at invaders (Kanetkar, 2020).

I hope that these are very convincing reasons why you should adopt C as the first step in your quest for learning programming.


## Features of the C Language

From the introduction, you can already deduce most of the interesting features of the C language. 
![Features of C Language](https://media.geeksforgeeks.org/wp-content/uploads/20200214125122/Features-of-C-Programming-Language.jpg)

1. **Modularity**: C is a general-purpose structured language. This feature of C language allows you to break a code into different parts using functions which can be stored in the form of libraries for future use and reusability.. Structuring the code using functions increases the visual appeal and makes the program more organized and less prone to errors.

2. **Dynamic Memory Management**: One of the most significant features of C language is its support for dynamic memory management (DMA). It means that you can utilize and manage the size of the data structure in C during runtime. C also provides several predefined functions to work with memory allocation. For instance, you can use the free() function to free up the allocated memory at any time. Similarly, there are other functions such as malloc(), calloc(), and realloc() to perform operations on data structure and memory allocations.

3. **Extensibility**: You can easily (and quickly) extend a C program. This means that if a code is already written, you can add new features to it with a few alterations. Basically, it allows adding new features, functionalities, and operations to an existing C program.

4. **Portability:** Another feature of the C language is portability. To put it simply, C programs are machine-independent which means that you can run the fraction of a code created in C on various machines with none or some machine-specific changes.

5. **Simple and Efficient:** The basic syntax style of implementing C language is very simple and easy to learn. This makes the language easily comprehensible and enables a programmer to redesign or create a new application. 

6. **Recursion:** Recursion means that you can create a function that can call itself multiple times until a given condition is true, just like the loops. C language provides the feature of recursion and recursion in C programming provides the functionality of code reusability and backtracking.

7. **Pointers:**  Pointers point to a specific location in the memory and interact directly with it. With the use of pointers in C, you can directly interact with memory which enables you to operate with memory, arrays, functions, and structures.


## Getting Started with C

Learning the English language and learning the C language are very similar, as illustrated in the figure below:

![Figure illustrating similarities between learning English and learning C language.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01wxqjaj3n7o4a5126yj.png)
`(Source: Kanetkar, 2020).`

### Alphabets, Digits and Special Symbols
Figure below shows the valid alphabets, numbers and special symbols allowed in C.

![Figure showing valid alphabets, numbers and special symbols allowed in C.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u0vnr1u7zd8gij8mwk7.png)
`(Source: Kanetkar, 2020).`

### Constants, Variables and Keywords
The alphabets, digits and special symbols when properly combined form constants, variables and keywords.

#### Constants

A constant is an entity that doesn’t change. Constants in programming are often called literals, and in C, they can be divided into two major categories:

(a) Primary Constants

(b) Secondary Constants

These constants are further categorized as shown in the Figure below:

![Figure showing major categories of constants](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ratd53j255lh3h4kj2ll.png)
`(Source: Kanetkar, 2020).`

#### Variables

A variable, also known as identifiers in programming, is an entity that may change. There are as many types of variables in C as there are types of constants in it. This is because any C program performs many calculations, the results of which are stored in some cells (locations) of the computer's memory. To make the retrieval and usage of these values easy, the memory cells are given names. Since the value stored in each location may change, the names given to these locations are called variable names.

A variable name can be any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Do not create unnecessarily long variable names as it adds to your typing effort.

It's important to note also that the first character in the variable name must be an alphabet or underscore (_).

#### Keywords

A keyword is a word that carries special meaning. There are only 32 keywords available in C; they are words whose meaning has already been communicated to the compiler (or, more broadly, to the computer) (Kanetkar, 2020). 

The figure below gives a list of these keywords.

![Figure showing keywords in C](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0uxok3pcvjfd1lir1hqw.png)
`(Source: Kanetkar, 2020).`

Although it is not recommended to use the keywords as variable names, some C compilers allow you to create variable names that closely resemble the keywords (Kanetkar, 2020).

## Structure of a C program

![C Program Code Structure](https://media.geeksforgeeks.org/wp-content/uploads/20221219163357/Structure-of-C-Program.png)

The structure of a basic C Program code consists of the Header and the Body. 

```c
#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}

```

To explain futher,

### The Header

This is the part that consists of all the header files inclusions, an example of this in our **Hello World** code is the `#include <stdio.h>`. This line of code here means that we are including a header file, indicated by the **.h** file extension, with the name **stdio**. In adding this line, we have made all the function declarations and macro definitions in that file to now be accessible for our use in our program coding.

> Do you understand?

We will be seeing, exploring and using many more header files as we continue.

### The Body

Yes, everything else apart from the head (this time, header). In thi C program, all codes that comes after the last line of the header files inclusion is part of the body.

The body can consist of various components: variable declarations, functions, statements etc. Most importantly, in a basic C program, our Hello World, there must be a `int main() {}`. This is the entry point for any C program and execution typically starts here.

The curly braces in that line of code contains the statement
that needs to be executed by the C program. In our case, we need our C program to print **Hello World**, therefore, our entry point function became:

```c

int main()
{
    printf("Hello World!");
    return 0;
}

```

The `return 0` is to indicate to the interpreter that our function ran successfully.

## The `prinf()` function

The you notice that expression in our lines of code? Yes, the `print("")`, it is a very powerful function in a C program that is used to


## Writing a C Program

🤔 Right now! You too want to write your first Hello World Program? 

There are a number of online IDEs that are available for free that you can use to start writing your C program without installing a compiller. An example of this is [GeeksforGeeksIDE](https://ide.geeksforgeeks.org/).

For windows users, there are many IDEs available. Th most popular of them are CodeBlocks and Dev-CPP. [CodeBlocks](http://www.codeblocks.org/downloads/26) has strongly been recommended by the netizen community for development in C.

> Don't know what Netizen is? 🤔 It's the Internet Community.

Linux users, GCC compiler comes bundled with Linux which compiles C programs and generates executables for us to run, and [CodeBlocks](http://www.codeblocks.org/downloads/26) can also be used with Linux.

🤓 Mac users, macOS already has a built-in text editor where you can just simply write the code and save it with a “.c” extension.

My favourite code editor is the VsCode editor. I write more on our to download and setup on Mac, and possibly on Windows for development in the C language.

## Share your thoughts and comments

I am critically open for your constructive criticisms. Enjoy
as you explore the world of programming in C.

✌🏾 Peace.

[**Paul Ishaili C.**](https://www.linkedin.com/in/mrpaulishaili/)


## References

1. Kanetkar, Y. (2020). Let Us C: Authentic Guide to C PROGRAMMING Language 17th Edition (English Edition). Let Us C.

2. S, R. A. (2023). Use of C language: Everything you need to know. Simplilearn.com. https://www.simplilearn.com/tutorials/c-tutorial/use-of-c-language