DEV Community

Cover image for Introduction to C Programming and Data Structure.
Paul C. Ishaili
Paul C. Ishaili

Posted on • Edited on

Introduction to C Programming and Data Structure.

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

  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.
(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.
(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
(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
(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

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

#include <stdio.h>

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

Enter fullscreen mode Exit fullscreen mode

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:


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

Enter fullscreen mode Exit fullscreen mode

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.

For windows users, there are many IDEs available. Th most popular of them are CodeBlocks and Dev-CPP. CodeBlocks 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 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.

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

Top comments (5)

Collapse
 
pauljlucas profile image
Paul J. Lucas
  1. Your example is missing the \n.
  2. macOS also ships with vim.
  3. There are much better intro-to-C materials out there. This one brings nothing new to the table.
Collapse
 
michaeltharrington profile image
Michael Tharrington

Heyo Paul!

So, I know the author asked for constructive criticism here. #1 & #2 are a bit blunt but helpful. That said, #3 is pretty rude. Please try to keep things friendly... it's absolutely possible to be honest, critical, and kind at the same time.

Collapse
 
mrpaulishaili profile image
Paul C. Ishaili

Thank you @michaeltharrington πŸ™πŸ½. I hope you found it refreshing or possibly helpful to any beginner system/software engineer?

Collapse
 
mrpaulishaili profile image
Paul C. Ishaili

Thank you, @pauljlucas . I am glad we are name-sake. I've spent a number of years in software engineering, core in Nodejs, Typescript, and have always found it scary in dablling into anything Java or C.

My aim for this article and the series it will be attached with is to eliminate what may have stopped other aspiring software engineers from going full chest in adding C language to their arsenal too; becuase I believe that many others are in the same shoes as I was.

  • To have a complete interest guide with fundamental explanation
  • To eleminate unneccesary jargons that can not be explained to understanding.
  • It's also a way of reeducating myself too.

Yeah, true @pauljlucas the \n is to return a new line but the command statement will still print out the 'Hello World'.

MacOS indeed ships vim; I use MacOs too.

And thank you so much for your sincere constructive criticism, it's indeed helpful. I look forward discussing with you in many of my other articles.

πŸ™πŸ½ Cheers!

Collapse
 
pauljlucas profile image
Paul J. Lucas

To have a complete interest guide with fundamental explanation.

It's not complete. You never say what printf() actually is, nor that it is declared inside stdio.h (which is why you need to include it, which you also don't say), nor what stdio actually means.

To eleminate unneccesary [sic] jargons that can not be explained to understanding.

IMHO, that's doing people a disservice. If there are particular terms that are commonly used in C programming, it's better to introduce those terms and define them so the reader understands what they mean. If you don't explain them, then when somebody reads those terms elsewhere, they won't know what they mean.

... the \n is to return a new line but the command statement will still print out the 'Hello World'.

I never said it wouldn't print Hello World. By not including \n, if you run the program in a shell command-line environment, you'll get:

Hello Worldhostname$
Enter fullscreen mode Exit fullscreen mode

where the hostname$ is a typical shell prompt (where hostname is the actual name of the host computer and $ is the typical prompt character for a non-root shell).

Some comments may only be visible to logged-in visitors. Sign in to view all comments.