Introduction to C Programming

This lesson will give you a complete introduction to C programming among other things such as why we write programs, and the need for a programming language like C which is a MUST for students and working professionals to become great software engineers especially when they are working in the software development domain.

Need for a Programming Language

Computers are general-purpose machines that can do various tasks with one caveat i.e. we need to instruct them on how to perform the task. These instructions must be given in a machine-readable language (binary streams of 0 and 1) for the computer to understand the task. For example, if we want the computer to add two numbers, we can’t just tell the computer to do that in English as the computer will not understand it. Thus comes the need for programming languages which are required to write instructions (programs) that a computer can understand and then execute to perform the task.

Types of Programming Languages

All programming languages can be broadly classified into two categories as mentioned below:

  • Low-Level Languages
  • High-Level Languages

Low-Level Programming Languages

A low-level language is also sometimes referred to as a computer’s native language. It is writing machine instructions (0 and 1) or something very close to it, that a computer can understand with minimum or no translation. To write code in a low-level programming language, one needs extensive knowledge of the hardware system and architecture of the specific CPU, the code is going to run on. Different CPUs have different architectures based on a variety of needs, and one will need to learn each of them first before one can write low-level programs for them. By this, it must be clear that low-level programming languages are machine-dependent as well as machine oriented. A low-level program written for one machine may not be compatible with another machine if its architecture is different.

There are broadly two types of Low-Level programming languages:

  • Machine Language: The code here is written as a string of binary values (0 and 1) and stored directly in the CPU memory without further processing. This is machine-level code that the CPU can understand but is very hard for humans to comprehend. The main advantage of a program written in machine language is that it can be directly executed on hardware without requiring translation or interpretation. This also results in the program running with a minimal memory footprint and being very fast in its execution time. The major disadvantage of programs written in machine language is their difficulty in comprehension by humans and their inability to be ported between various hardware with different architectures.
  • Assembly Language: To overcome the drawback of machine language being incomprehensible to humans, assembly language was developed which promoted the use of specific mnemonic words that are human-readable and understandable. Assembly language works as a bridge between high-level programming languages and machine-level languages. But it also requires a language translator such as an assembler to convert the code into machine code before execution is possible. Assembly languages are not universal and are created with a specific computer architecture in mind. This means that, similar to machine language, code written in an assembly language is not portable between different CPUs with different architecture types.

High-Level Programming Languages

To overcome the two major drawbacks of low-level languages i.e. incomprehensible to humans and no portability among different machines with different architectures; high-level programming languages were developed. Programs written in these languages are easily understandable to humans as their syntaxes are quite close to plain English. Also, programs are portable and not machine dependent (but they may be OS-dependent). Some common and widely used examples of high-level programming languages are C++, Java, Python, Pearl, Ruby, PHP and more. All modern programming languages in use today are mostly high-level programming languages. The main reason behind their popularity is that they provide the ability for the programmer of writing machine-independent code without having the knowledge of the underlying architecture of the hardware, which can be tricky and time-consuming to learn.

As the programs are machine independent, they need to be processed with a variety of steps to convert them into machine code and make them executable on the hardware. The three major types of programs used for this task are assemblers, compilers and interpreters. This dependency on the conversion of code before execution is also one of the major drawbacks of high-level languages as this makes them slower and inefficient in terms of execution time.

Language Translators

As discussed above, the programs written in high-level languages and even assembly languages need to be converted into machine code before their execution. This task of conversion is provided through the use of special programs called language translators. The three major types of translators are discussed below:

Assembler

An assembler is a special program that is required to convert codes written in the assembly language into machine code. As each assembly language is hardware specific set of instructions; therefore each assembler is designed with the target hardware in mind. Also, codes written in other high-level languages need and use assembler for their final level conversion into machine code (more on this later).

Complete Introduction to C Programming 1

Compiler

A compiler is again a special program which is designed to convert codes written in high-level languages into machine code. A compiler will covert the entire source code, written in a high-level language, into a machine executable object file in one go before the actual execution of the program. This object file can then be loaded into the memory of the target CPU and executed multiple times without the need for the compiler or the source code to be present in the memory of the target hardware. C/C++ and Java are some popular languages that use compilers.

compiler-block-diagram

Major advantages of compilers:

  • Execution time is faster in high-level languages that use compilers as the code is executed in one go after the entire source code has already been compiled.
  • As discussed above, once the compilation process is completed and the executable file has been created; there is no need for the source code or the compiler during the execution process. The executable object file can directly be stored in the target CPU and executed as many times as required. This results in less memory footprint when working with compilers.

Major disadvantages of compilers:

  • It is difficult to debug errors with compilers as the list of errors is generated at the last during the compilation process for the entirety of the source code.

Interpreter

Similar to a compiler, an interpreter is also designed to convert code written in high-level languages into machine code. But it does this by executing each line of source code one at a time instead of the entire code as done by the compiler. So the conversion into machine code and its execution take place in parallel for each line of source code. As a result, both the source code and the interpreter program are required in the memory of the target CPU whenever the code is being run by that CPU. Python is a good example of a popular programming language that uses interpreters.

Complete Introduction to C Programming 2

Major advantages of interpreters:

  • Debugging errors is easier in comparison to compilers as errors are presented for each line as each line is being executed.

Major disadvantages of interpreters:

  • Execution time is slower in high-level languages that use interpreters as the code is executed one line at a time and not in its entirety.
  • The source code and the interpreter program need to be present in the CPU memory at all times during the execution process which results in more memory use in comparison to compilers.

Brief Introduction to C Programming

is a general-purpose, procedural, machine-independent, structured programming language. It was created by Dennis M. Ritchie at the Bell Telephone Laboratories in 1972 for developing the UNIX operating system. C was derived from a language called B created by Ken Thompson. As it was a successor of Language B, hence the name C.

Contrary to popular belief, C is not a high-level language. It is a mid-level language with the simplicity of a high-level language but the power of a low-level language. Programs written in C are wicked fast. It is also a small language compared to others, with only 32 keywords. Keywords are reserved words used by a language for doing certain tasks. We will talk about them in detail, later in the course. Due to the small library of keywords, C can be learned easily.

In the Computer Science community, it is said that “C is God’s, Programming Language”. This is said as many later languages heavily borrowed syntax and features from C. Syntax of Java, PHP, JavaScript, and many more languages are based on C. C++, on the other hand, is nearly a superset of C language. You will be hard-pressed to find programs which will compile in C but not in C++.

Nearly four decades after its creation, C is still used to write everything from Operating Systems (Windows) to complex programs like the Oracle database, GIT, Python interpreter and more. The whole world of Embedded Systems is still heavily reliant on C owing to its fast execution time in safety-critical real-time systems.

History of C Language

Dennis M. Ritchie (Father of C Programming)
Dennis M. Ritchie (1941-2011): Father of C Programming

The development of the UNIX operating system is closely tied to the origin of C. UNIX was originally written in assembly language on a PDP-7 by Dennis M. Ritchie and Ken Thompson. Thompson wanted a programming language to make utility applications for the new platform. He tried to make a Fortran Compiler but soon gave up on that idea. Later he created a cut-down version of the recently developed BPCL systems Programming Language. This new language had no official description as of yet, and Thompson modified its syntax to be less wordy, producing a new similar but simpler language B. However, B was slow and only a few utilities were ever written on it.

Ritchie started to improve upon this new language created by his friend in 1972. This resulted in the creation of the new language C. Later the C Compiler and some utilities written in it were included in Version 2 of UNIX OS.[1]

K&R C

Brain Kernighan and Dennis Ritchie published the first edition of the “C Programming Language”, in 1978. This book, known as K&R, served for many years as the informal specification for this language.

ANSI C and ISO C

In the early 1980s, C was getting popular as some versions of it were implemented in mainframe computers, minicomputers and microcomputers including the IBM PC. Therefore, in 1983, the American National Standards Institute(ANSI)[2] formed a committee, X3J11, to establish a standard specification of C. They based it on UNIX implementation and this version is widely known as ANSI C or C89. It is also called simply C and is taught in schools as the first Introduction to C Programming Language.

Ken_Thompson_and_Dennis_Ritchie--1973
Ken Thompson and Dennis Ritchie- 1973

In 1990, the International Organization for Standardization[3] adopted the ANSI standard as ISO/IEC 9899:1990, which is sometimes called C90. Therefore, the terms C89 and C90 refer to the same programming language.

C99

The C standard was further revised in the late 1990s, leading to the publication of ISO/IEC 9899:1999 in 1999, which is commonly referred to as C99.

C11

There was another revision of the standard C language in 2007 which was informally called C1X. It was officially published in 2011 and called C11.

C18

This version is the current C standard and it was published in 2018. It introduced no new language features, only technical corrections and clarifications of the shortcomings in C11.

Embedded C

Embedded C Programming requires some nonstandard extensions of the current C language to support features such as fixed and floating-point arithmetic, I/O Operations, multiple memory bank operations etc. In 2008, the C standards committee released a technical report which extended the C language to include these features and create a common standard for Embedded C.

Where is C Language used?

  • Linux OS, Apple’s OS X and Windows have been written in C. Other Operating Systems such as Symbian for mobile OS, is also written in C.
  • Web Developing Tools such as PHP has been written in C.
  • C is widely used in Embedded Systems due to its fast execution time.
  • It is also used to develop system and desktop applications.
  • Most of the applications by Adobe such as Photoshop and After-Effects are developed using the C programming language.
  • It is also used to develop databases. MySQL, for example, is the most popular database software which is built using C.
  • C is also used for developing browsers and their extensions. Google’s Chromium is built using the C programming language.
  • C is used for producing Compilers for other Programming Languages.
  • C is now widely used in IoT applications.

All these applications in a wide range of fields make it one of the most widely used languages today. In fact, in 2019, C was the third most commonly used language even after almost 40 years of its creation. Not bad…!

Top Programming Languages 2019
Top Programming Languages in 2019

Introduction to C Programming Language Features

C is a Compiled Language

As discussed above, a compiler is a piece of software or program that converts code written in a high/mid-level language into machine code (1 and 0). C uses a compiler which makes its execution faster as well as takes less memory in the target machine.

Efficiency of C

C is very efficient in terms of memory management and is very fast. It was designed for UNIX OS, so it was essential for a C program to run quickly and with a limited amount of memory.

Portability of C Programs

Code written in C is extremely Portable. What this means is, that a program written in C can run in a wide variety of operating systems with little or no modifications.

Powerful

Even after almost forty years since its creation, this language is still used to write Operating Systems, and create compilers and interpreters for other Programming Languages such as Python, Perl, PHP, BASIC etc.

This concludes the first lesson “Introduction to C Programming”. I hope you found it helpful. Feel free to share this course using the sharing buttons below and do Enrol if you haven’t already. In the next lesson, we will learn how to install the Codeblocks IDE.

References

  1. History of C Programming(Wikipedia)
  2. American National Standard Institute(Wikipedia)
  3. International Organisation for Standardization(Wikipedia)

Best C Programming Books

List of C Programming books curated for beginners as well as experienced programmers.

C Programming Books

Sharing is Caring

If you liked this post, then feel free to share it with your loved ones!

Leave a Reply