Tuesday, June 13, 2017

Introduction to C Language

Introduction to C Programming Language

C is a computer programming language, which was developed at AT & T Bell Labs of USA in 1972. It was designed and developed by Dennis Ritchie. Soon, C became popular without any publicity. Many programmers preferred C to so many popular programming languages of that time like FORTRAN, PL/I, ALGOL, PASCAL, etc.

Features of C:

  1. C is a general purpose programming language.
  2. C is simple, reliable, and easy to learn and use.
  3. C is often called middle level language, because it has the features of both type of languages i.e. high level languages and low level languages.
  4. Programs written in C are fast in execution and take less space in memory. That’s why, programs for smart machines like smart phones, microwave ovens, washing machines, etc. are written in C.
  5. Many operating systems like windows, linux, unix, etc. are written mainly in C programming language.
  6. Because of C’s fastness and compactness, many gaming frameworks like DirectX are written in C.
  7. For writing device driver programs, C is the first choice. It is because that C has many programming elements, by which, it can directly interact with hardware.



C in Action:

Here, I assume that you have Turbo C installed on your computer. You can install other C compilers of your choice. I have made programs using Turbo C IDE (Integrated Development Environment). 

First C Program:

I think that the best way of learning is learn by example. That’s why, I am giving a simple C program first. Please type this program in your editor.

/* My First Program */
#include<stdio.h>
int main()
{
      printf("Hello World!");
      return 0;
}

Save this program in this format - <name.c>. You can give any meaningful name with .c extension. In Turbo C, press <F2> key for saving.

Compile and execute this program. In Turbo C, for compilation, press <alt + F9> keys together and for execution, press <ctrl + F9> keys. You will not see any output. To see output or user screen, press <alt + F5> keys. Now, you will see the message Hello World! on the screen.

The first line of the program is for comment. Comments are included to clarify anything in the program. They can be given anywhere in the program and are totally ignored by the compiler. In C computer language anything written between /* */ is not executed. We shall see more stuff about comments later.

/* My First Program */

The second line of the program tells the compiler to include a header file “stdio.h”. This file is needed for some instructions like “printf()”. I’ll explain about header files later on. Till then, please use this instruction in the start of the program the way I have used, whenever you use “printf()”.

#include<stdio.h>

The next line of the program is a function main(). Every C program has at least one function called main() and execution starts from the function main(). The return type of this function in this program is integer. It means the value, function main() returns to the operating system, is of type integer. The way of telling this in the C programming language is as given below:

int main()

What are return types and data types, you will see in later posts.

A function is a container, which contains the instructions or statements. In the C programming language, a function starts with a opening curly brace “{” and ends with a closing curly brace “}”. A pair of round brackets is given after the function name. The form of a simple function is given below:

func_name()
{
       statement 1;
       statement 2;
       … ;
       … ;    
}

A detailed explanation about functions will be given in the later posts.

The first statement in the function main() is the function printf(). Function printf() is a library function. It’s declaration is given in the header file “stdio.h”  therefore this file is included in the program in the start. Function printf() is one of the functions, which outputs a message or the result of any mathematical expression or simply the value of any variable. The format of this function is given below:

printf(“format string”, list_of_variables);

In our program, function printf() is given in the simplest form. It displays a message Hello World! on the screen.

printf("Hello World!");

The next statement returns the value ‘0’ to the operating system. Since return type of the function main() is of integer type, therefore it should return a number in the last after execution of all the statements. If it returns ‘0’, it means the program executed successfully. If a nonzero value is returned, it signifies a failure.

 return 0;

Please note-
  1. Every C statement ends with a semicolon. Semicolon signifies the end of the statement.
  2. Every C statement or instruction is written in a separate line.
  3. All instructions should be written in lower case letters.
  4. The statements in a C program should be written in the same order in which we wish them to be executed except some situations like jump instructions or function calls etc.







C Operator Precedence and Associativity

C Operator Precedence and Associativity Precedence of Operators in C: Operator precedence tells the order, in which operations are per...