Introduction to C programming Language

This is a beginner’s introduction to C programming language. The C programming language is one of the most important and widely used programming language for creating computer programs.

A brief history of C programming language

The C programming language was created by Dennis M. Ritchie, at Bell Laboratories in the early 1970s as the system programming language for Unix. It was meant to run on machines that had limited resources back then. So one of the language priorities was to build a language that used as few resources as possible, both in the compiler and in the resulting code.

This can be seen in the features, and also lack of certain features of C. C programs place almost no demands on system resources and allow the programmer greater control over their execution. That is why programs like operating system kernels and device drivers that were previously written in assembly language are now implemented in C.

C has gone through four major versions:

  • Original K&R C defined in the 1978 first edition of Kernighan and Ritchie’s book The C Programming Language.
  • ANSI C, from 1988, which fixed some oddities in the syntax and which is documented in the 1988 second edition of The C Programming Language.
  • C99, from 1999, the ISO/IEC 9899:1999 standard for C, which added some features from C++ and many new features for high-performance numerical computing.
  • C11, from 2011, the ISO/IEC 9899:2011 standard for C, which relaxed some of the requirements of C99 that most compilers hadn’t bothered implementing and which added a few extra features.

Benefits of learning C programming language

Some of the benefits of learning C programming language are that C:

  • runs on everything, from very small microcontroller systems to large complex computer systems.
  • lets you write programs that use very few resources.
  • gives you control over the low level functions of the system.
  • doesn’t force you to use a certain programming style.
  • is used to build interpretors and compilers of many programming languages, such as Visual Basic, perl, python, ruby, PHP, etc.
  • gives you the ability to code for a large number of platforms.
  • enables you to master other languages easily because almost all popular programming and scripting languages, such as C++, Java, Python, Objective-C, Perl, Ruby, PHP, Lua, and Bash, borrow syntaxes and functions from C.
  • in terms of employment, there is an enormous code-base of C programs developed over the last few decades. Which means there are many systems that will need to be maintained for many years to come.
  • C gives you the organised structure of the high-level programming languages and the speed of development they offer and at the same time you also get the minimal program size and high speed of a low-level language.
  • C is a general purpose programming language which can be used to create games, business software, mathematical models, word processors, spreadsheets, etc.

Limitations of C programming language

Some limitations of C programming language are a product of the language missing a lot of features of many modern program languages, including lack of:

  • a garbage collector.
  • minimal programmer-protection features such as array bounds-checking or a strong type system.
  • certain important built-in data structures.
  • language support for exceptions, namespaces, object oriented programming, etc.

What is C programming language?

C programming language is a high-level programming language that you can use to create applications or programmes for a computer system. It has been around for several decades and has survived the test of time due to its flexibility and efficiency.

C is a compiled language which means that a C program, once written, has to be run through a compiler which turns the program into an executable file that can then be run by the computer or whatever system it was created for.

The C program is written as a human readable text file with a .c extension. The C compiler converts the C program into an executable binary file that is the machine readable and executable.

As compared to other popular programming languages, C is a small language in the sense that it has less to learn. Due to its small size, C lacks many high level features. This can be frustrating but it also means that C imposes relatively few built-in ways of doing things on the programmer. Which means in a way, C doesn’t get in the way of the developer.

To make the core of C language very small and simple, special functionality is provided as libraries of ready-made functions. Some standard libraries are provided for you so you don’t have to recreate standard ways of doing things. However, you can also build your own libraries.

In C, there are libraries for things such as:

  • mathematical functions
  • string handling functions
  • input/output functions
  • graphics libraries
  • etc

Writing our first C program

In order to grasp the basics of C programming, let us write our first C program and compile it. Open your favourite text editor and type the code below. Then save the file as something like firstProg.c (in fact, any name of your choosing as long as it has a .c extension).

#include <stdio.h>

int main(){
	
	printf("Welcome to Sytech Learning Academy");
	
	 return 0;	
	}

Compiling our first C program

Compiling C code on a Linux machine is a simple as follows:

  • cd to the directory where you saved the firstProg.c file and type the following:
gcc firstProg.c −o firstProg

The line of code above:

  • invokes the C compiler called gcc
  • commands gcc to compile firstProg.c
  • tells gcc to name the output executable file firstProg.

Running our first C program

Now to run the executable file created, type

./firstProg

The output will be "Welcome to Sytech Learning Academy", due to the statement printf("Welcome to Sytech Learning Academy"); in the compiled code.

Explaining our first C program

This is our first C program:

#include <stdio.h>	

int main(){
	
	printf("Welcome to Sytech Learning Academy"); 
	
	 return 0;	
	}				

… and I am going to split it into 5 important talking points

#include <stdio.h>

In C, any line of code that starts with # is called a preprocessor directive. The line containing the directive #include gets replaced completely by the contents of the file whose name is between the < and > brackets. In our case it gets replaced by the contents of the stdio.h header file.

This way, the contents of standard header files are copied into our program without having to manually type them in ourselves.

Our example starts with the #include <stdio.h> directive which includes the contents of the standard input/output library into our program. The standard I/O library allows our program to :

  • read input from the keyboard (standard in)
  • write output to the screen (standard out)
  • process text files stored on the disk
  • etc

int main()

int main() declares the main function of our program. The main() function is the entrance point of our program during execution and hence every C program must have a main() function. Program execution starts at the first line of the main() function.

int is the return type of the main() function. The return value of the main() reports the exit status of the program to the caller as an integer value.


The main() function body

The opening brace { and closing brace } symbols mark the beginning and end of the main() function body.

printf("Welcome to Sytech Learning Academy");

The printf() function in C is used to send output to standard out (the screen, in our example). In our example, the function will print out "Welcome to Sytech Learning Academy" on the screen. It is a must in C to terminate statements with a semicolon (;).

return 0

The return 0; line returns an error code 0 (no error) to the calling program, in our example, to the shell that started the program execution.

The return statement works the same way as calling the exit function and returning 0 or EXIT SUCCESS is the way of indicating success, anything else indicates failure.

Summary of important things about a C program

  • All statements are terminated by semicolons.
  • Indentation is ignored by the compiler.
  • C is case sensitive.
  • Strings literals are enclosed in double quotes.