Istory of C Language
Istory of C Language
History of C Language:
C is a programming language developed at AT&T’s Bell Laboratories of USA in 1972. It
was designed and written by a man named Dennis Ritchie.Possibly, why C seems so
popular is because it is reliable, simple and easy to use. C stands In between these two
categories. That’s why it is often called a Middle level language, since it was designed to
have both: a relatively good programming efficiency (as compared to Machine oriented
languages) and a relatively good machine efficiency (as compared to Problem oriented
languages).
Structure of A C Program:
Any C program consists of one or more distinct units called
‘functions’. These functions consist of valid C statements and are linked together through
‘function calls’. A ‘function’ is analogous to a subroutine or a procedure in other high
level
languages. Every ‘function’ in a program has a unique name and is designed to perform a
specific task. The task to be accomplished by the function is defined by a group or block
of instructions or statements. Each function with its block of statements is treated as one
single unit in C language and can be placed anywhere in the program.
Each instruction in a function is written as a separate statement.
These statements must appear in the same order in which we wish them to be executed;
unless of course the logic of the problem demands a deliberate ‘jump’ or transfer of
control to a statement which is out of sequence.However big a C program, the following
rules are applicable to all statements present in it:
(a) Blank spaces may be inserted between two words to improve the readability of
the statement. However, no blank spaces are allowed within a word.
(b) Usually all C statements are entered in small case letters.
(c) C has no specific rules about the position at which different parts of a statement
are to be written. Not only can a C statement be written anywhere in a line, it
can also be split over multiple lines. That is why it is many a times called a free
form language.
(d) Any C statement always ends with a semicolon (;).
The skeleton of a C program:
main()
{
statement 1;
statement 2;
}
function1()
{
variable declarations;
statement 1;
statement 2;
}
Digits 0, 1,2,3,4,5,6,7,8,9
Data types:
There are two fundamentally different types of data in C programming-
integer and floating point. From these two we can derive two others- characters and
double precision. There also exists a data type called void, which isn’t used as often as
the other four.
The bytes occupied by each of these data types as well as the name used to refer to them
is shown below:
Data type Meaning Size(bytes)
Char a character 1
int an integer 2
float a single precision real number 4
double a double precision real number 8
void valueless 0
These data types put together are often known as ‘primary’ data types. Several other data
types can be derived from these primary data types. Such data types are known as
‘secondary’ data types or ‘derived’ data types.
C Data types
Character Array
Integer pointer
Float structure
double union
void enum
Operators:
Operator is a symbol which represents a particular operation that can be
performed on some data.
Relational Operators:
These operators are used to compare two operands to see whether they are
equal to each other ,unequal, or whether one is greater than the other. The operands can
be variables, constants, or expressions that ultimately get evaluated to a numerical value.
The operators include <(less than), >(greater than), <= (less than equal to ), >= (greater
than equal to), ==(equal to), != (not equal to).
Logical Operators:
An expression evaluates to either true or false. In a number of situations it
may become necessary to combine the results of evaluation of such expressions. For this
purpose C provides three operators &&, || and ! ,standing for ‘logical and’, ‘logical or’
and ‘logical not’.
Conditional operators:
The conditional operators ? and : are sometimes called ternary operators since
they take three operands. Their general form is
Expression 1 ? expression 2 : expression 3
Sizeof Operator:
The Sizeof operator returns the number of bytes the operand occupies in
memory. The operand may be variable , a constant or a data type qualifier.
Control Statements
As the name suggests the ‘ control statements’ enable us to specify the order in which the
various the various instructions in a program are to be executed by the computer. The
control statements determine the ‘flow of control’ in a program.
There are four types of control statements in C. They are :
The sequence control statement ensures that the instructions in the program are executed
in the same order in which they appear in the program. Decision and Case statements
allow the computer to take a decision as to which statement is to be executed next. The
Loop control statement helps computer to execute a group of statements repeatedly till a
condition is satisfied.
3.Nested if’s:
The general form of the statement is look like this :
if (condition1)
Statement 1;
else
{
if(condition 2)
statement 2;
else
statement 3;
}
Here statement1, statement 2 and statement3 are single statements or group of
statements.
if (condition 1)
Statement1;
else if (condition 2)
Statement 2;
- ---- ---------
- -------- -----
else if (condition n)
statement n;
else
statement n+1;
The switch executes the case where a match is found and all the subsequent cases and the
default as well. Each and every statement should be followed by a break statement.
There is no need for a break statement after the default, since the control comes to the
end anyway.
Loops in C:
There are three methods by way of which we can repeat a part of a program.
They are (a) Using a while statement
(b) Using a for statement
(c) Using a do-while statement
do
{
this ;
and this ;
and this ;
} while (this condition is true);
FILES
Having dealt with the various console input/output functions like printf(),
scanf(), getch() etc., the disk i/o operations are performed on entities called files.
Opening A File:
Before we can write information to a file on a disk or read it, we must open
the file. When we request the operating system to open a file, what we get back, is a
pointer to the structure FILE. That is why, we make the following declaration before
opening the file,
FILE *fp;
Reading A File:
To read a file, the file must be opened in the read mode. The syntax will be
as follows:
fp = fopen(“pr1.c”, “r”);
The contents in the file are read by using the function called, fscanf(). The
syntax is as follows :
fscanf(fp, format, values);
Writing A File:
Before writing data into a file, the file must be opened in write mode.
fp= fopen(“pr1.c”,”w”);
The data is written into the file , by using the function called, fprintf().The
syntax is as follows :
fprintf (fp, format, values);
Closing A File:
Closing of file is done by using the function called, fclose(). The syntax is
as follows:
fclose (fp);