Sei sulla pagina 1di 10

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;
}

The C Character Set:


A character denotes any alphabet, digit or special symbol used to represent
information. Following table shows the valid alphabets, numbers and special symbols
allowed in C:

Alphabets A, B,…………, Y,Z


a , b,…………., y, z

Digits 0, 1,2,3,4,5,6,7,8,9

Special symbols ~‘!@# %^&()_-+=


|\{}[]:;“‘<>,.?/

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

Primary data types secondary 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.

Depending on the function performed the operators can be classified as:


Arithmetic operators:
The symbols + for addition, - for subtraction, * for multiplication, / for
division are used. All these are ‘binary’ operators since they operate on two operands at a
time. Each of these operators work with ints, floats, chars.

Increment And Decrement Operators:


C offers two special operators ++ and – called increment and decrement
operators, respectively. These are unary operators since they operate on only one
command.

Modulo Division Operator:


C provides one more binary arithmetic operator % called Modulo Division
operator. This operator is a supplement to the division operator. We use it for division.
But unlike division where we get the quotient on division, here we get the remainder.

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 :

(a) Sequence control statement


(b) Selection or Decision control statement
(c) Case control statement
(d) Repetition or Loop control statement

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.

Selection or Decision control statement – IF:

1.General form of ‘if ‘:


The general form of if statement looks like this :

If (this condition is true)


Execute statement;

The statement may be a single statement or a group of statements.

2.The ‘if-else ‘ statement:


The general form of ‘ if-else’ statement is:
If(condition )
Statement 1;
Else
Statement 2;
If ‘condition’ is true then ‘statement 1’ is executed otherwise ‘statement 2’ is executed.
Here also ‘statement 1’ and ‘ statement 2’ may be single statements or group of
statements.

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.

4.The if-else-if Ladder:


The general form of the statement is look like this:

if (condition 1)
Statement1;
else if (condition 2)
Statement 2;
- ---- ---------
- -------- -----
else if (condition n)
statement n;
else
statement n+1;

All the above statements are single or multiple statements.

Case control statement – switch:


The control statement which allows us to make a decision from the
number of choices is called a switch.
The general form of is as follows:

Switch (integer expression)


{
case constant 1:
do this ;
case constant 2:
do this ;
default:
do this ;
}

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

The while loop:


The general form of while is as follows:

Initialize loop counter;


While(test loop counter using a condition)
{
do this;
and this;
increment loop counter;
}
The parenthesis after the while contains a condition. So long as this
condition remains true, all statements within the body of while loop keep getting
executed repeatedly.

The for loop:


The general form of for statement is as follows:

for (initialize counter ; test counter ; increment counter)


{
do this ;
and this ;
}

The do-while loop:


The do-while loop looks like this:

do
{
this ;
and this ;
and this ;
} while (this condition is true);

The break statement:


When break is encountered inside any C loop, control automatically
passes to the first statement after the loop.

The continue statement:


When the keyword continue is encountered inside any loop, control
automatically passes to the beginning of the loop.

The exit ( ) function:


exit( ) is a standard library function that comes ready-made with the C
compiler. Its purpose is to terminate the execution of the program.

The goto statement:


A goto statement can cause program control to take the control where you
want to.
Structures
Structure is a combination of different data items which are stored in
consequent locations.

The general format of structures is as follows:


Struct <structure name>
{
structure element 1;
structure element 2;
….
….
}

The variables of structure are declared as ,


Struct <structure name> var1, var2, …..

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;

fopen() is used to open a file. The syntax is as follows:


fp = fopen(“pr1.c”,”r”);

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);

File Opening Modes:

1. “r” Searches file. Operations possible are reading from a file


2.”w” Searches file. Operations possible are writing to the file.
3.”a” Searches file. Operations possible are appending new contents at end of file
4.”r+” Searches file. Operations possible are reading existing contents, writing new
contents, modifying existing contents of the file.
5.”w+” Searches file. Operations possible are writing new contents, reading them
back and modifying existing contents of the file.
6.”a+” Searches file. Operations possible are reading existing contents, appending
new contents to end of file. Cannot modify existing contents.

Potrebbero piacerti anche