Complete Introduction to C Programming

Introduction to C Programming

This lesson provides a comprehensive introduction to C programming. It also explains why we write programs and why a programming language like C is essential. Learning C is a must for students and working professionals who aspire to become skilled software engineers, especially those pursuing a career in software development.

Need for a Programming Language

Computers are general-purpose machines capable of performing a wide variety of tasks. However, they can only do so when given explicit instructions. These instructions must ultimately be represented in a machine-readable format (binary sequences of 0s and 1s) that the computer can understand and execute.

For example, if we want a computer to add two numbers, we cannot simply tell it to do so in English because it does not understand human languages. This is where programming languages come in. They allow us to write instructions, known as programs, in a form that can be translated into machine code, enabling the computer to execute the desired 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 programming language is often referred to as a computer’s native language. It involves writing machine instructions (0s and 1s) or code that is very close to machine language, allowing the computer to execute it with little or no translation.

Writing programs in a low-level language requires an in-depth understanding of the underlying hardware and the architecture of the target CPU. Since different CPUs have different architectures designed to meet various requirements, a programmer must first understand the specific architecture before writing low-level programs for it.

As a result, low-level programming languages are both machine-dependent and machine-oriented. A program written for one type of machine may not run on another if the two machines have different architectures.

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

  • Machine Language: Machine language consists of instructions written as binary digits (0s and 1s), which are stored directly in the computer’s memory. Since these instructions are in the CPU’s native format, they can be executed directly by the processor without any translation or interpretation. The primary advantage of machine language is its execution speed and minimal memory overhead, as the CPU can execute the instructions directly. However, writing and debugging machine language programs is extremely difficult because binary code is not human-readable. Another major limitation is that machine language is machine-dependent—a program written for one CPU architecture cannot run on another with a different architecture.
  • Assembly Language: To overcome the readability limitations of machine language, assembly language was developed. Instead of writing binary instructions, programmers use mnemonic instructions (such as ADD, MOV, and SUB) that are much easier for humans to read and understand. Assembly language serves as a bridge between machine language and high-level programming languages. Before an assembly program can be executed, it must be translated into machine code by a program called an assembler. Like machine language, assembly language is also machine-dependent. Each assembly language is designed for a specific CPU architecture, which means that an assembly program written for one processor architecture cannot be executed on another with a different instruction set without being rewritten.

High-Level Programming Languages

To overcome the two major drawbacks of low-level programming languages—being difficult for humans to understand and being machine-dependent—high-level programming languages were developed.

Programs written in high-level languages are much easier for humans to read and write because their syntax is closer to natural English. They are also largely machine-independent, meaning the same source code can often run on different hardware platforms with little or no modification (although some programs may still be operating system-dependent).

Some of the most popular high-level programming languages include C, C++, Java, Python, Perl, Ruby, and PHP. Almost all modern programming languages in widespread use today are high-level languages. Their popularity stems from the fact that they allow programmers to write applications without needing an in-depth understanding of the underlying hardware architecture, making software development faster, easier, and more maintainable.

Since computers can only execute machine code, programs written in high-level languages must first be translated into machine code before they can run. This translation is performed using special programs known as language translators, the most common of which are assemblers, compilers, and interpreters.

Although this translation process introduces some execution overhead compared to low-level languages, the benefits of readability, portability, maintainability, and faster development far outweigh the performance trade-offs for most applications.

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