Introduction to C Language

C was developed by Dennis Ritchie at Bell Laboratories in 1972. Most of its principles and ideas were taken from the earlier language B, BCPL and CPL. CPL was developed jointly between the Mathematical Laboratory at the University of Cambridge and the University of London Computer Unit in 1960s. CPL (Combined Programming Language) was developed with the purpose of creating a language that was capable of both machine independent programming and would allow the programmer to control the behavior of individual bits of information. But the CPL was too large for use in many applications.

Basic Combined Programming Language

In 1967, BCPL (Basic Combined Programming Language) was created as a scaled down version of CPL while still retaining its basic features. This process was continued by Ken Thompson. He made B Language during working at Bell Labs. B Language was a scaled down version of BCPL. B Language was written for the systems programming. In 1972, a co-worker of Ken Thompson, Dennis Ritchie developed C Language by taking some of the generality found in BCPL to the B language.

Unix Kernel Development

The original PDP-11 version of the Unix system was developed in assembly language. In 1973, C language had become powerful enough that most of the Unix kernel was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly.

System Programming Language

During the rest of the 1970’s, C spread throughout many colleges and universities because of its close ties to UNIX and the availability of C compilers. Soon, many different organizations began using their own versions of C Language. This was causing great compatibility problems. In 1983, the American National Standards Institute (ANSI) formed a committee to establish a standard definition of C Language. That is known as ANSI Standard C. Today C is the most widely used System Programming Language.

First C Program, Hello World

Following C program will simply print “Hello World” on screen. Write carefully because C language is a case sensitive language.

#include < stdio.h>
void main()


printf(“Hello World\n”);


Press ALT+F9 to compile your program. If you have any error in your program, you will get the message, remove your errors and then execute your program you will got the out put.

Hello World

The printf()

The printf() function prints output to stdout, according to format and other arguments passed to printf(). The string format consists of two types of items – characters that will be printed to the screen, and format commands that define how the other arguments to printf() are displayed.

printf( “Hello World”);

The scanf()

The scanf() function reads input from stdio, according to the given format, and stores the data in the other arguments. It works a lot like printf(). The format string consists of control characters, white-space characters, and non-white-space characters.

void main(void)

int i;


C Variables & Constants

C Constants

The alphabets, numbers and special symbols when properly combined form constants, variables and keywords. A constant is an entity that does not change.

C Variables

Variables in c are names which refers to a location in memory which is used to holds some value with which we are working. A variable is an entity that may change its value. In any program we typically do lots of calculations. The results of these calculations are stored in computer memory locations. To make the retrieval and usage of these values we give names to the memory locations. These names are called variables.

C Keywords

A keyword is a word that is part of C Language itself. These words have predefined meanings and these words cannot be used as variable names.

char signed break for
auto const sizeof case
if extern double struct
continue goto register enum
typedef default return static
float union do switch
volatile int unsigned else
while long void short

Types of Variables

There are two main types of variables in C: numeric variables that hold only numbers or values, and string variables that hold text, from one to several characters long.

Basic fundamental data types in C language

char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false. 1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)

C Loops

Loops are used to repeat one statement or set statements more than one time. Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C.

for Loop

For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. The general form is

for (initialization; condition; increment/decrement)

statements block


If the statement block is only one statement, the braces are not necessary. Although the for allows a number of variations, generally the initialization is used to set a counter variable to its starting value. The condition is generally a relational statement that checks the counter variable against a termination value, and the increment increments (or decrements) the counter value. The loop repeats until the condition becomes false.



int i;

for(i = 0; i < count; i++)




while Loop

The while loop repeats a statement until the test at the top proves false.

The while loop has the general form:


statement block


The while tests its condition at the top of the loops. Therefore, if the condition is false to begin with, the loop will not execute at all. The condition may be any expression. An example of a while follows. It reads characters until end-of-file is encountered.



int t = 0;





do-while Loop

This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.

void main(void) {

int val;

printf(“Enter 1 to continue and 0 to exit :”);
scanf(“%d\n”, &val);

} while (val!= 1 && val!= 0);


C Control Structure

C language possesses such decision making capabilities and supports the following  statements known as c control structure or decision-making statements.

  • if statement
  • switch statement
  • Conditional operator statement
  • goto statement

if Statement

The if statement is a powerful decision making statement and is used to control
the flow of execution of statements. It is basically a two-way decision statement
and is used in conjunction with an expression.

if Statement Syntax

if (conditional)


block of statements executed if conditional is true;



block of statements if condition false;


if Statement Example


int x=5

if (x > 1)



printf(“%d”, x);


if…else statement

The if….else statement is an extension of the simple if statement. The general form is

if (condition)

True-block statement(s)


False-block statement(s)


If the condition is true, then the true-block statement(s), immediately following the if
statement are executed; otherwise the false-block statement(s) are executed.

void main(void)

int a, b;
char ch;

printf(“(A) Add, (S) Subtract, (M) Multiply, or (D) Divide?\n”);
ch = getchar();

printf(“Enter a: “);
scanf(“%d”, &a);
printf(“Enter b: “);
scanf(“%d”, &b);

if(ch==’A’) printf(“%d”, a+b);
else if(ch==’S’) printf(“%d”, a-b);
else if(ch==’M’) printf(“%d”, a*b);
else if(ch==’D’ && b!=0) printf(“%d”, a/b);


if-else-if statement

void main(void)

int numb;

printf(“Type any Number : “);
scanf(“%d”, &numb);

if(numb > 0) {

printf(“%d is the positive number”, numb);

} else if(numb < 0) {

printf(“%d is the Negative number”, numb);

} else {

printf(“%d is zero”,numb);



Switch Statement

The switch and case statements help control complex conditional and branching operations.
The switch statement transfers control to a statement within its body.

Switch Statement Syntax

switch (expression) {

case item:


case item:


case item:





Switch Statement Example


int numb;
printf(“Type any Number”);

switch(numb %2)

case 0 :

printf(“the number %d is even \n”, numb);

case 1 :

printf(“the number %d is odd \n”, numb);



Ternary condition

The ? (ternary condition) operator is a more efficient form for expressing simple if
statements. It has the following form:

expression1 ? expression2: expression3

Ternary condition Example

res = (a>b) ? a : b;

if a is greater than b than res has the value a else the res has value b.

break statement

break statement is used to exit from a loop or a switch, control passing to the first
statement beyond the loop or a switch.

With loops, break can be used to force an early exit from the loop, or to implement a
loop with a test to exit in the middle of the loop body. A break within a loop should
always be protected within an if statement which provides the test to control the exit condition.

break statement example










continue statement

continue is similar to the break statement but it only works within loops where its effect
is to force an immediate jump to the loop control statement.

Like a break, continue should be protected by an if statement.

continue statement example










The goto statement

The goto is a unconditional branching statement used to transfer control of the program from one statement to another.

One must ensure not to use too much of goto statement in their program because its
functionality is limited. It is only recommended as a last resort if structured solutions are
much more complicated.

C Functions

Function is a block of statements which perform some specific task and always return single value to the calling function. Functions in c are used to minimize the repetition of code.

Some languages distinguish between functions which return variables and those which don’t. C assumes that every function will return a value. If the programmer wants a return value, this is achieved using the return statement. If no return value is required, none should be used when calling the function.

There are two types of functions in c language.

Library Functions

A function which is predefined in c language is called library function. printf(), scanf(), getch() etc are examples of built-in or library functions.

User Defined Functions

A function written by a programmer is called user defined function.

User Defined Function Example

int add (int x, int y) {

int z;
z = x + y;
return (z);


main ()

int i, j, k;
i = 15;
j = 5;

k = add(i, j);
printf (“The value of k is %d\n”, k);



The value of k is 30

Scope of Function

Only a limited amount of information is available within the body of each function. Variables declared within the calling function can’t be accessed from the outside functions unless they are passed to the called function as arguments.

Global Variables

A variable that is declared out side all functions is called Global variable. Global variables don’t die on return from a function. Their value is retained, and is available to any other function in whole program.

Local Variables

A variable that is declared within a function is called Local variable. They are created each time the function is called, and destroyed on return from the function. The values passed to the functions (arguments) are also treated like local variables.

Static Variables

Static variables are like local variables but they don’t die on return from the function. Instead their last value is retained, and it becomes available when the function is called again.

C Pointers

A pointer is a variable suitable for keeping memory addresses of other variables. The values you assign to a pointer are memory addresses of other variables or other pointers.

C pointers are characterized by their value and data-type. The value is the address of the memory location the pointer points to, the type determines how the pointer will be incremented/decremented in pointer or subscript arithmetic.

Pointers are used to manipulate arrays and they can be used to return more than one value from a function.

Pointers are declared by using the asterisk(*).
int *p;

Each variable has two attributes: address and value. The address is the location in memory. In that location, the value is stored. During the lifetime of the variable, the address is not changed but the value may change.

void main (void)

int i;
int * a;
i = 10;
a = &i;
printf (” The address of i is %8u \n”, a);
printf (” The value of i is %d\n”, i);
printf (” The value at that location is %d\n”, *a);



The address of i is 631672
The value of i is 10
The value at that location is 10

Arrays and Pointers

An array is actually a pointer to the 0th element of the array. Dereferencing the array name will give the 0th element. This gives us a range of equivalent notations for array access. In the following examples, arr is an array.

arr[0] *arr
arr[1] *(arr+1)
arr[n] *(arr+n)

C Arrays

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
Arrays in C is a data structure of multiple elements with the same data type. Array elements are accessed using subscript. The valid range of subscript is 0 to size -1.

Declaration of Array

int arr[10];


void main(void)

int a[5];
int i;
for(i = 0;i<5;i++)



for(i = 0;i<5;i++)

printf(“%d value of I is = %d\n”,i,a[i]);




1 value of I is = 0
2 value of I is = 1
3 value of I is = 2
4 value of I is = 3
5 value of I is = 4

Multidimensional Arrays

A multi-dimensional array of dimension n (i.e., an n-dimensional array or simply n-D array) is a collection of items which is accessed via n subscript expressions. Multidimensional arrays can be described as “arrays of arrays”.

Multidimensional Arrays Example

void main(void){

int a[3][2];
int i,j;

for(i = 0;i<3;i++){

for(j=0; j<2 ;j++) {




for(i = 0;i<3;i++){

for(j=0;j<2;j++) {

printf(“value in array %d\n”,a[i][j]);




C Strings

C strings are character type array and ends with the NULL character. %s placeholder is used in the printf() function to display string values.

String Declaration

char name[50];


void main (void )

char *st1 = “abcd”;
char st2[] = “efgh”;
printf( “%s\n”, s1);
printf( “%s\n”, s2);



void main(void){

char myname[] = {‘J’,’h’,’o’,’n’};

printf(“%s \n”,myname);


String input and output

The gets function relieves the string from standard input device while put S outputs the string to the standard output device.

The function gets accepts the name of the string as a parameter, and fills the string with characters that are input from the keyboard till newline character is encountered.

The puts function displays the contents stored in its parameter on the standard screen.

Syntax of the gets() function

gets (str_var);

Syntax of the puts() function

puts (str_var);

String input and output Example

# include < stdio.h >
void main ()

char myname [40];
printf (“Type your Name :”);
gets (myname);
printf (“Your name is :”);


C String Functions

Function Description
strcpy(string1, string2) Copy string2 into string1
strcat(string1, string2) Concatenate string2 onto the end of string1
length = strlen(string) Get the length of a string
strcmp(string1, string2) Return 0 if string1 equals string2, otherwise nonzero
strchr(string1, chr); will find the first matching character in a string.

C Structures and Unions

A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together.

Declaring Structures

struct mystruct

int numb;
char ch;


Structure has name mystruct and it contains two variables: an integer named numb and a character named ch.

Declaring structure variable

struct mystruct s1;

Accessing Member Variables




typedef can also be used with structures. The following creates a new type sb which is of type struct chk and can be initialised as usual:

typedef struct chk

char name[50];
int magazinesize;
float calibre;

} sb;

ab arnies={“adam”,30,7};

Unions in C

A union is an object that can hold any one of a set of named members. The members of the named set can be of any data type. Members are overlaid in storage. The storage allocated for a union is the storage required for the largest member of the union, plus any padding required for the union to end at a natural boundary of its strictest member.

union {

char n;
int age;
float weight;

} people;