You are on page 1of 18

HISTORY OF C

1. Dennis Ritchie designed and implemented the first C compiler on a PDP-11 (a


very old machine by today's standards, yet one which had huge power on
modern scientific computation).
2. The C language was based on two (now outdated) languages: BCPL, written by
Martin Richards, and B, written by Ken Thompson in 1970 for the first UNIX
system on a PDP-7.
3. The original ``official'' C language was the ``K & R'' C, the nickname coming from
the names of the two authors of the original ``The C Programming Language''. In
1988, the American National Standards Institute (ANSI) adopted a ``new and
improved'' version of C, known today as ``ANSI C''.
4. It is a middle level language. It has the simplicity of a high level language as well
as the power of low level language. It means C makes application programs and
system programs.
5. C is small language, consisting of only 32 english words known as keywords. C
has its own library functions and also allow the users to add their own library
functions.
6. C contains control statements needed to write a structure programs hence it is
considered a structured programming language. It include structures for
selection(if...else, switch), repetation(while, do...while) and for loop.
7. The programs written in C language are portable i.e. programs written for one
type of computer can be run on another computer.

Characteristics of C
The following are some characteristics of C:

Small size

Extensive use of function calls

Loose typing

Structured language

Low level (BitWise) programming readily available.

Pointer implementation - extensive use of pointers for memory, array, structures


and functions.

It has high-level constructs.

It can handle low-level activities.

It produces efficient programs.

It can be compiled on a variety of computers.

The C language is case dependent. The C compiler distinguishes between small


letters and capital letters. If a letter is typed in the wrong case, the compiler will
complain and it will not produce an executable program.

Operators
A operator specifies an operation to be performed between two or more operands. An
operand is a data item on which an operator acts. C includes a number of operators as
follows1.
2.
3.
4.
5.
6.
7.
8.
9.

Arithmetic Operators
Assignment Operators
Increment and Decrement Operators
Relational Operators
Logical Operators
Conditional Operators
Bitwise Operators
Comma Operators
Sizeof Operators

1. Arithmetic Operators
Arithmetic operators are used for numeric calculations. They are two types:
(1) Unary arithmetic operators
(2) Binary arithmetic operators
(1) Unary arithmetic operator required only one operand. For example: +a, -b
(2) Binary arithmetic operator required two operands. There are five arithmetic
operators
Purpose
Operators
+

Addition

Subtraction

Multiplication

Division

Modulus(gives the remainder)

2. Assignment operators
A value can be stored in a variable with the use of assignment operators.
The value of right hand operand is assigned to the left hand operand.
For example:
x=10
y=x
s=x+ y
a=a+2

(i.e. a+=2)

b=b-4

(i.e. b-=4)

c=c/5

(i.e. c/=5)

d=d*6

(i.e. d*=6)

e=e%7

(i.e. e%=7)

3. Increment and Decrement Operators


They are unary operators. The increment operator (++) increments the value of
the variable by 1 and decrement operator (--) decrements the value of variable
by 1. These operators are of two types(1) Prefix increment/decrement (++a and - -a)
First the value of variable is increment/decrement then the new value is used
in the operation.
(2) Post increment/decrement (a++ and a- -)
First the value of variable is used in the operation and then
increment/decrement is performed.
++a/a++ means a=a+1 and - -a/a- - means a=a-1
4. Relational Operators
They are used to compare values of two expressions depending on their
relations. If relation is true then the value of relational expression is 1 and if
relation is false then the value of expression is 0. The relational operators are as
followsOperators
Meaning
<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

==

Equal to
Not equal to

!=

5. Logical or Boolean Operators


An expression that combines two or more expressions is termed as a logical
expression. These operators return 0 for false and 1 for true. C has three logical
operatorsOperators
Meaning
&&

AND

||

OR

NOT

Logical NOT is a unary operator while the other two are binary operators.
AND Operators
Expression1

Expression2

Result

False

False

False

True

False

False

False

True

False

True

True

True

OR Operators
Expression1

Expression2

Result

False

False

False

True

False

True

False

True

True

True

True

True

NOT operator
Expression

Result

False

True

True

False

6. Conditional Operators
Conditional operator is a ternary operator (? And :) which requires three
expressions as operands. This is written asTestExpression ? expression1 : expression2
Firstly the TestExpression is evaluated.
(1) If TestExpression is true, then expression1 is evaluated and it becomes the
value of the overall conditional expression.
(2) If TestExpression is False, then expression2 is evaluated and it becomes the
value of the overall conditional expression.
For example: Let a=5 and b=8
X= if(a<b) ? a : b
Here, condition a<b is true then x=a i.e. x=5
7. Bitwise Operators
C has the ability to support the manipulation of data at the bit level. Bitwise
operators are used for operations on individual bits. Bitwise operators operate
on integers only. They are as followsOperators
Meaning

Bitwise AND
P

&

Bitwise AND

Bitwise OR

Bitwise NOT

<<

Left shift

>>

Right sift

Bitwise XOR
Q

Result

0
1

For example:
A
0010 1001 0011 1011 (0x293B)
B
0001 1010 0010 1111 (0x1A2F)
A&B 0000 1000 0010 1011 (0x082B)
Bitwise OR
P

Result

For example:
A
0010 1001 0011 1011 (0x293B)
B
0001 1010 0010 1111 (0x1A2F)
A|B 0011 1011 0011 1111 (0x3B3F)
Bitwise NOT
P

Result

Bitwise XOR
P

Result

For example:
A
0010 1001 0011 1011 (0x293B)
B
0001 1010 0010 1111 (0x1A2F)
A^B 0011 0011 0001 0100 (0x3314)
Bitwise Left Shift

This operator is used for shifting the bit left. On shifting the bits left, an equal
number of bit positions on the right are vacated. These positions are filled in with 0 bits.
For example:
A= 0x1346 i.e.

0001 0011 0100 0110

A<<4 = 0011 0100 0110 0000


Bitwise Right Shift
This operator is used for shifting the bit right. On shifting the bits right, an equal
number of bit positions on the left are vacated. These positions are filled in with 0 bits.
For example:
A= 0x1346 i.e.

0001 0011 0100 0110

A<<4 = 0000 0001 0011 0100


8. Comma operators
This operator is used to permit different expressions to apper in situations
where only one expression would be used. The expressions are separated by the
comma operators.
For example:
a=8, b=7, c=9, a+b+c
Here, four expressions are present. Initially 8 is assigned to a, then 7 is assigned
to b, 9 is assigned to c and after this a+b=c is evaluated. So it isx=(a=8, b=7, c=9, a+b+c)
x=24
9. Sizeof Operator
This is an unary operator. This operator gives the size of its operand in term of
bits. The operand can be a variable, constant or any datatype. For examplesizeof(int) gives 2.
#include<stdio.h>
Int main()
{
Int a;
printf( Size of int = %d, sizeof(int));
printf( Size of float = %d, sizeof(float));
printf( Size of Char = %d, sizeof(char));
printf( Size of a = %d, sizeof(a));
return 0;
}
Output;
Size of int = 2
Size of float = 4
Size of Char= 1
Size of a=2

Datatype
C supports different types of data. There are four fundamental datatypes in C
which are int, char, float and double. char is used to store any single character,
int is used to store integer value, float is used for storing single precision
floating point number and double is used for storing double precision floating
point number. C support type qualifiers with these basic types to get some more
types. There are two types of type qualifiers1. Size qualifiers
short, long
2. Sign qualifiers
signed, unsigned
When the qualifier unsigned is used the number is always positive and when
signed is used number may be positive or negative. If the sign qualifier is not
mentioned, then by default signed qualifier assumed. The range of values for
signed data types is less than that of unsigned type. This is because in signed
type, the leftmost bit is representing the sign.
Basic Datatype
Char
Int

Float
Double

With type qualifiers


signed char
unsigned char
signed int
unsigned int
short int
long int

Size (bytes)
1
1
2
2
1
4

float
double
long double

4
8
10

Range
-128 to 127
0 to 255
-32768 to 32767
0 to 65535
-128 to 127
-2147483648 to
2147483647
3.4E-38 to 3.4E+38
1.7E-308 to 1.7E+308
3.4E-4932 to 1.1E+4932

Control Statements
Control Structure as its shown by its name- That controls the structure.
Meaning- Program is flowing like a river & control structures are like a dam, which control
the flow of river. In other words we can say that, Control Structures are those which can
control or mould the flow of program.
If we connect control structure with our life, so the concept of control structure is:Our daily schedule is the biggest example. In our daily life we repeat some work, we take
some decision & we apply some conditions for that, if we want to make a program, we have
two options:Suppose a program is for monthly schedule:
A) One option is to repeat the some steps again & again.
B) Just instruct computer at once & it shows our monthly schedule.
So which one is better than which obviously, the second one isnt assume it is just for 30 to
31 times if uses need is greater than this, than think.
Control Structures are based on the concept of the second option.

Need of Control Structures: - Now the question arises, why control structures are
needed or what are those requirements, which are fulfilled by control structures.
As we explained before & the details are as follows:
A) Make C popular for all the time, as well as it was in its nascent stage.
B) Decrease complexity of program.
C) Make C software more compatible & more resistible in compare to the other software
of languages.
D) To control the memory consumption.
E) The most important quality of C is its compactness to maintain that control structures
are made.
F) Through some very unique function of C, in which Control structure are also be
counted, C can be said as The mother of many language
G) Control the wastage of time.
H) Control Structures are those which can mould the program according to uses need.

Uses of Control Structures:- There are many uses of control structure. Basically its uses
are to make C language, a friendly language for users. Now take a look on the uses of Control
Structure:
A) It is very useful & helpful to make decision.
B) It offers some following statement like If, if else if- helps in choosing option in the form of Boolean expression.
Switch- helps to select any option from many options.
For- iterative loop & required certain limit.
While & do while- iterative loop & required condition.
goto, break, return, continue- use to jump the statements.

Advantages:- From some uses & need of control structure is very much clear that it is a
very nice feature of C with many more advantages within it.
1) It makes program very easy to write as well as to understand.
2) It provides features like if, switch, goto, for etc.
3) Nesting of loops & statements.
4) It provides some jump statements which are explain later.
5) Decrease users effort.
6) Provide better results.
7) Increase efficiency.
8) Increase impact of program.
9) User friendly feature.
10) It makes program integrated.
11) The biggest advantages of control structure are that there is no barrier or condition
required for using loops & statement; mean not a particular structure is designed for
using control structure.
Example- Suppose firstly user while loop & than use for then if & than go to as inner
loops or statements. So the point is that, there is no limit or condition is applied for
using CS.

Sections & Sub-sections of CS:Control Structure


Selected

Repeative or Itrative

Jump

1.If
2.If else
3.Ifelseif
4.Switchcase

while
do while
for

goto
continue
break
return

Selective:- Those statements which are help to select any option from many choices(option)
is known as Selective Statements.
Suppose, you need a bag. So, what will you do? Obviously first of all, you will go to the
market & select a bag according to you or your moms choice from many options. This is
called Selection.
The selective statements are:If statement- If is a selective statement, it is use to check the condition & if condition is true
it print the result.
Syntax: if (condition)
{
Statements;
}
Eg: /* WAP to test & print even numbers */
# include< stdio-h>
# include< conio-h>
void main ( )
{
int a ;
clrser ( );
printf (Insert the number : );
scanf (% d, &a);
if (a%2= =0)
{
printf (%d, a);
}
getch ( );
}
If else & if else if statement: - By these options, many options can be written together
.Suppose the given condition is not true than go to the other conditions.
Syntax: - if (condition)
{
Statements;
}
else if (condition)
{
Statements;
}
else
{
Statements;
}
Eg: /* WAP to find out largest among 3 numbers */.
# include <stdio.h>
# include <conio.h>
void main( )

{
int a, b, c;
printf( insert three numbers:);
scanf(%d %d 5d%, & a, &b, &c);
if (a>b && a>c)
{
printf (a is largest);
}
else if (b>a && b>c)
{
printf (b is largest);
}
else
{
printf (c is largest);
}
getch( );
}
Switch case: - It is use to write many conditions within a pair of curly brasses.
These conditions are written with a keyboard & jump statement break. Break helps user to
come out of loop if condition is true.
Syntax: - switch (condition)
{
case 1:
Statement;
Break;
,
,
,
default:
Statement;
}
Eg: /* WAP to find out the day with the help of sequence number.*/
# include <stdio.h>
# include <conio.h>
void main ( )
{
int seq;
printf( insert the sequence);
scanf (%d, & seq);
switch (seq)
{
case 1:
printf (Sunday);
break;
case 2:
printf(Monday);
break;
,

,
,
default:
Print f (no day);
}
getch( );
}
Explanation:- In this example ,if we enter 3, then case 1 & 2 are false, than condition is
check for 3 which is right, so the result is Tuesday & than program is come out from the loop
. That is the work of switch case & break.
Difference between Nested if & Switch case
S.No
Nested if
1.
Many condition can be written
together
2.
3.

More than one condition can be


True.
It is a complexed program.

Switch case
many condition can be written
together within only a pair of
curly brasses.
Only one condition can be true.
It is a simple program.

Repetitive or Itrative: - These loops are those loops from which user can get results
according to his desire, but without repeating steps.
These are the two divisions of Iterative loops
1.For loopfor (initialization; condition; increment/decrement)
{
Statements;
}
Example:
/* Program to generate fibonacci series*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
long x, y, z;
clrscr();
x=0;
y=1;
printf("Enter the number of terms : ");
scanf("%d", &n);
printf("%ld", y);
for (i=1; i<n; i++)
{
z = x + y;
printf("%ld", z);
x = y;
y = z;
}
printf("\n");

getch();
}
Nested for:/* Program to print the following pattern:
*
***
*****
*******
*********
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
printf("Enter n : ");
scanf("%d", n);
for(i=1; i<=n; i++)
{
for(j=1; j<=n-i; j++)
{
printf(" ");
}
for(j=1; j<i; j++)
{
printf("*");
}
for(j=1; j<i; j++)
{
printf("*");
}
printf("\n");
}
getch();
}
2.While loop :while (condition)
{
Statements;
Increment/decrement;
}
Example:
/* Program to print the sum of digits of any number.*/
#include<stdio.h>
#include<conio.h>

void main()
{
int n, sum = 0, r;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
while(n>0)
{
r = n % 10;
sum = sum + r;
n = n / 10;
}
printf("%d", sum);
getch();
}
3. do....while :do
{
Statements;
} while (condition);
Example:
/* Program to print the sum of numbers entered.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum = 0;
clrscr();
do
{
printf("Enter the number (0 to stop): ");
scanf("%d", &n);
sum = sum + n;
} while(n!=0);
getch();
}
Difference between while and do...while

1. In while loop, first the condition is checked and then the statements are
executed whereas in a do while loop, first the statements are executed and
then the condition is checked.
2. If initially the condition is false the while loop will not executed at all,
whereas the do while loop will always executed at least once.
3. In do while loop, a semicolon is placed after the condition, whereas in while
loop no semicolon is placed after the condition.
Nested Loop:
/* Program to print the armstrong numbers from 100 to 999.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int m, n, sum, c, d;
clrscr();
printf("Armstrong numbers are : \n ");
for(m=100; m<=999; m++)
{
n = m;
sum = 0;
while(n>0)
{
d = n % 10;
n = n / 10;
c = d * d * d;
sum = sum + c;
}
if(m == sum)
{
printf(" %d\n", m);
}

}
getch();
}
Break statement:
When break statement is encountered, loop is terminated and the control is
transferred to the statement immediately after the loop.
/* Program to find the number is prime or not.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, flag=1;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
for(i=2; i<= sqrt(n); i++)
{
if(n % i == 0)
{
printf("Not Prime.");
flag=0;
break;
}
}
if(flag == 1)
{
printf("Prime Number.");
}
getch();

}
Continue statement:
The continue statement is used when we want to go to the next iteration of the
loop after skipping some statements of the loop.
Example:
/* Program to find the sum and average of 10 positive integers.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i=1, sum=0;
float avg;
clrscr();
printf("Enter 10 positive numbers : ");
while (i<=10)
{
scanf("%d", &n);
if(n<0)
{
printf("Enter only positive numbers \n");
continue;
}
sum = sum + n;
i++;
}
avg=sum/10.0;
printf("Sum = %d and Average = %d", sum, avg);
getch();
}

Goto statement:
This is an unconditional control statement that transfers the flow of control to
another part of the program.
Syntax:
goto label:
...............;
.................;
label:
....................;
......................;
Example:
/* Program to print whether the number is even or odd.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
if(n%2==0)
goto even;
else
goto odd;

even :
printf("Number is EVEN.");
goto end;
odd :

printf("Number is ODD.");
goto end;
end :
printf("\n");
getch();
}

You might also like