C Programming Tutorial

Complete C Programming Tutorial for beginners and professionals. This C tutorial covers all the topics that you should learn before start programming with C Language.

Introduction to C programming

The development of C programming language is credited to Dennis Ritchie who helped to shape the computer world by introducing a general-purpose programming language that has numerous features that include low-level access to memory, simple keywords, useful concepts like data types and functions that makes C programming language suitable for system programming.

  • C is a structured, high-level, machine-independent programming language that provides flexibility and portability to the user to write programs with ease.
  • C is a powerful and elegant programming language that has a sophisticated programming style that enables a user to write complex programs.
  • C programming language delivers high-quality programs with great efficiency that provides the development of system software to large business packages.

What is C Programming?

C programming language is often called the mother of all languages as most of the well-known programming languages that are popular among the developers nowadays are built around C. It is the most widely known programming language in the world of development.

History of C Language

Many languages were introduced in the early 1960s such as ALGOL, BCPL, and B, each of them having its own developed concepts that became known in only a few parts of the world but never became globally recognized.

Then the C language was introduced in the programming world. It served the purpose of all kinds and gained widespread support from the software developers.

  • C programming language was evolved using concepts from older languages like ALGOL, BCPL, and B.
  • It introduced its own new concepts like data types and other unique features.
  • Later, with the release of many C compilers for commercial purposes, it became the most used programming language by the software developers to build a large variety of operating systems and modern hardware programs.

C Programming Tutorial

The c programming language is of great help for learning to program for the beginners. It’s easy to learn because of its execution capabilities on almost all types of machines. It doesn’t need any particular hardware or machine to write programs that raise the bar of its versatility among other languages.

C programming language is a structured, high-level, machine-independent programming language. It enables the developers to develop and execute programs with ease as it can be implemented on any hardware platform.

In layman’s terms, C programming language is a fundamental programming language that can be used as a foundation to learn many other modern programming languages due to its similar syntax or programming structure. The concepts used in C programming language are used in modern languages like Java, PHP, JavaScript, and many more.

C programming language was developed by Dennis M. Ritchie in 1972 at Bell Telephone Laboratories. It was developed to be implemented on the UNIX operating system which is a great platform for product development because of its efficiency, accessibility, and portability to perform operations that were made possible by the power of C programming language.

Concepts of C programming language

Following are the important concepts of C programming language that is used in developing programs:

  • Variables and Datatypes
  • Operators
  • Conditions
  • Loops
  • Arrays
  • Strings
  • Functions
  • Pointers
  • Structures
  • Unions

Importance of C programming language

C programming language is highly efficient for writing programs to develop system software.

  • It is highly portable and fast.
  • It has many useful built-in functions and operators.
  • It can be used to write any complex program due to its rich features.
  • It combines the features of assembly language and high-level language to deliver system software and business packages.
  • It can be written on one platform and can be run on many different software platforms with little to no modification.
  • It has rich functions in its built-in libraries that make it easy to write complex programs. It also allows users to add user-defined functions.

Advantages of C programming language

  • C programming language is used for developing system software but it is also used to develop other useful software products too.
  • Using C programming language, we can develop complex programs as C has rich features and techniques that help to design efficient and error-free programs.
  • C programming language is capable of producing high quality and fast executing programs which is why much large software and business packages are built using C programming language.

Applications of C programming language

  • Operating system
  • Compilers
  • Databases
  • Network drivers
  • Graphical User Interface

Environment setup for C programming

Let’s learn how to set up an environment before we execute our first C program.

Environment setups can be put together to work on a variety of platforms like Windows, Linux, Mac OS, and others.

From writing a C program to executing it

Following are the 3 steps to execute a program:

  • Writing a program
  • Compiling the program
  • Executing the program

For this, we need a text editor for writing our programs and a C compiler to compile the C program.

Using text editor

Step 1: Choosing the right text editor

A text editor like Notepad on Windows, Vim, or Vi on macOS and Linux can be used to write codes, and then the source files must be saved in “.c” extension which is a standard C program executable file extension.

Step 2: Installing a compiler

Now the source code file needs to be compiled for the system to execute it. GNU GCC compilers are available on different operating systems that can be downloaded and installed.

Install C on Windows

GNU GCC compiler can be downloaded from the link below:

https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/

Once installed, it is ready to execute C program source files on the windows command line.

Install C on Linux

On Linux, we can check whether GNU GCC is pre-installed or not by writing the following commands on the terminal:

$ gcc --version
$ g++ --version
$ make --version

If it is installed it will show output like this:

gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

If GNU GCC is not installed then one can use the following command on the terminal to download it:

$ sudo apt-get install build-essential

Or

$ sudo apt install build-essential

Install C on macOS

We can download the Xcode development environment from here:

https://developer.apple.com/xcode/

Once installed, we can use the GNU compiler for executing C programs.

We will execute our first C Program in the next section.

How to run the first program in C?

Before writing a program in C we need to get familiar with few in-built functions of C programming language:

  • printf() – It prints the message inside the double quote on the output screen.
  • main() – It instructs the computer about the starting point of a program.
  • Return 0 – When a program returns with 0, it indicates that the program is executed with zero errors.

Write a C program to print a simple message “This is my first C program”.

Sample Code:

#include<stdio.h>    
int main()
{    
printf("This is my first C program");    
return 0;   
}

Output:

This is my first C program

Explanation:

Line 1:

#include<stdio.h>

#include is a C directive and it is called as the pre-processor. It instructs the compiler to include a header file e.g. stdio.h (Standard Input Output) which contains functions printf(),scanf(), etc that are to be used during the execution of the program.

Line 2:

int main()

int (Integer) is the return type of the function main().

Line 3:

{

The curly braces are called parentheses that are enclosed around the body of the function main(). All the executable codes are written inside the parentheses.

Line 4:

printf("This is my first C program");

printf() function prints the text present inside the double quotes to the output screen. A semicolon ‘;’ marks the end of a statement in that line.

Line 5:

return 0;

A return statement indicates the end of a C program. The value 0 means the program is successfully compiled with zero errors.

Line 6:

}

This closing brace marks the end of the program inside function main().