Vous êtes sur la page 1sur 14

C was originally developed in the 1970s by Dennis

Ritchie at Bell Telephone Laboratories ,(now a part of


AT & T).C is a well structured High level programming
language and gained more popularity in programming
environment because of its reliable , simple and easy
to user. C stand s between HLL (High level language)
and LLL(Low level language).Thats why it is called
middle language .Since it was degined to have both , a
relatively good programming efficiency for system
oriented language and application oriented language.
Characteristics / Advantage of C language:
The following characteristics of C made this language
more power full and popular.
a)

C is a structured programming language.

b)

C is a general purpose language.

c)C has rich and powerful set of operators.


d)
It supports many sets of data declaration and
data type.
e)

C has very less numbers of reserve words.

f) C has ability to extend itself by adding more


functions to its library.
g)
It has more powerful ability to perform pointer
arithmetic and pointer manipulation.

Because of the above characteristics made the C


programming is the more simple,clear and also
increase the efficiency of the program execution.
Elements of C program:
Character set
Following are the valid character set of c language.
They are alphabets numbers and special characters.
Alphabets

A,B,C..X,Y,Z
ab,c.x,y,z

Numbers

0,1,2..9

Special
~,!,@,#,$,%,^,&,*,(,)_,+,=,|,\,},{,],
Characters [,;,,,<,.,?,/.
Keywords of C
Keywords are the words whose meaning has already
been explained to the C compiler. The keywords
cant be used as variables . there are only 32 key
words in c . they are :
Auto break case char const
default
do double
Else
enum extern float
long register

continue

for goto

if

int

Return
short signedsizeof
switch typedef
union
Unsigned void

volatile

static struct
while

Some compilers also include some or all the


following key words.
ada
huge

asm
near

entry
pascal

far

fortran

Data types
C supports several different types of data
,each of which may be represented differently
with in the computer memory.The basic data
types are listed below .the memory
requirement are also given .

Data type
Description
memory requirements
Char
1 byte

Single character

Int
2 byte

integer quantity

Float
byte

floating point number

Double
8 byte

double precision

Constants and variables of C :


Constants:
The value doesnt change during execution the
program called constants. Type C constants are
following:
-Primary constant:-integer, real (float, double),
character and string
-Secondary constant:-array, pointer, union,
structure etc..
Integer constants:Rules for integer constants:
-it must have at least one digit
-it shouldnt have decimal point
-it could be either +ve or ve
-special character and alphabets are not
allowed
-the range of constant is -32768 to +32767
-except + and -no other special characters
are allowed.
Example- 678, +500,-23, +765, -345 etc.

Rules for real constants:


- It must have at least one digit
-it must have a decimal point
-in this default size is +ve but it could be either +ve or
ve.
-no special characters and alphabets are allowed except
+,-and .
Example:- 555.0, -46.456, +3.543 etc.
Rules for character constants:
It must be single character (alphabets, digits or special
characters) enclosed with in single inverted comma.
Example: - A

# etc

Rules for string constants:


The group of characters or sequence of character s
enclosed with in a pair of double quotes.
Example:
ME & M.Tech,#@#@#@,76564548,Hetauda etc
Variables:
A variable as an identifier that is used to represent
some specified type of information with in a designated
portion of the program. It represent a single data item

i.e. a numerical quantity or a character constant. The


data item must be assigning to the variable at some
point in the program and can be accessed later in the
program simply by referring to the variable name. A
given variable can be assigned different data items at
various places with in the program. Thus the
information represented by the variable can be changed
during the execution of the program .however the data
type associated with the variable cannot change.
Example :Int a,b,c;
.. a =3; b =5;
=6; c=b-a; d=y;

c=a+b; .. a =4; b

etc.
Rules for constructing variables name:-Variable name must be begun with any alphabets.
-except digits, underscore and alphabets, no other
special character are allowed.
-C key words should not be used as variables name.
-both uppercase and lower case variable are significant
in C.

variables

Numeric
variables

Non numeric
variables

real

integer
Short int

long int

character

string

Float

double

Unsigned
int

Escape sequence
Certain nonprinting characters, as well as the backslash (\) and
apostrophe (), can be expressed in terms of escape sequence. And
escape sequence always begins with a backward slash and is followed
by one or more special characters.

The commonly used escape sequences are listed below:


Escape sequence

character

\a

bell (alert)

\b

backspace

\t

horizontal tab

\v

vertical tab

\n

new line

quotation mark ()

apostrophe (,)

\?

Question mark

\\

Backslash (\)

I/O function
An input output function can be accessed from any where with in the program simply by writing
the function name, followed by a list of arguments enclosed in parentheses. The arguments
represent data items that are sent to the function. Some input/output function do not require
arguments, through the empty parentheses must still appear.

Single character input- The getchar function


Single character can be entered into the computer using C library function getchar. The getchar
function is a part of the standard C i/o library. It returns a single character from a standard input
device (typically a keyboard). The function doesnt require any arguments, through a pair of
empty parentheses must follow the word getchar.
In generals terms, a function reference would be written as:
Character variable = getchar ();
Eg.

char c;
.
c=getchar();

The first statement declared that c is a character type variable. The second statement causes a
single character entered from keyboard and assigned to c.

Single character output- The putchar function


Single character can be displayed using C library function putchar. putchar function , like a
getchar is a part of C I/O library. It transmits a single character to a standard output device
(monitor). The character being transmitted will normally be represented as a character type
variable. It must be expressed as an argument to the function, enclosed in parentheses, following
the word putchar.
In general, a function would be written as:
char c;

..
Putchar (c);
The first statement declared that c is a character type variable. The second statement causes
the current value of c to be transmitted to the monitor where it will be displayed.

Entering input data- the scanf function


Input data can be entered into the computer from a standard input device by
means of c library function. This function can be used to enter any combination of
numerical values, single character and strings.
In general terms, the scanf function written as:
scanf (control string, arg1, arg2,);
Where control string refers to a certain required formatting information, and arg1,
arg2..are argument refers individual input data items. The control strings means
required formatting string starting with percentage sign (%) are
%c

single character

%d

decimal integer

%e

floating point value with its exponent

%f

floating point value without exponent

%o

octal value

%s

string

%u

unsigned decimal integer

%x

hexadecimal

Eg.

#include<stdio.h>
void main()
{
int a,b;
char c;
float e;

Scanf( %d %d %c %f,&a,&b,&c,&e);
.
..
}
Within the scanf function, the control string are %d %d %c %f. it contains three different groups.
The %d indicates the first two arguments(&a,&b), %c indicates the third argument(&c) and %f
indicates last arguments(&e).

Writing ouput data-the printf function


Output data can be written from the computer onto a standard output device using library
function printf. This function can be used to output any combination of numerical values, single
character and strings. It similar to the input function scanf, except that its purpose is to display
data rather than to enter it to computer.
In general terms, the printf function written as:
printf (control string, arg1,arg2,..)

Where control string refers to a certain required formatting information, and arg1,
arg2..are argument refers individual input data items. The control strings means
required formatting string starting with percentage sign (%) are
%c

single character

%d

decimal integer

%e

floating point value with its exponent

%f

floating point value without exponent

%o

octal value

%s

string

%u

unsigned decimal integer

%x

hexadecimal

Eg. 1
#include<stdio.h>
void main()
{

int a,b;
char c;
float e;
a=20;
b=30;
c=A;
e=3.1416;
printf(%d, %d, %c, %f,a,b,c,e);
.
}
Within the printf function, the control string are %d %d %c %f. it contains three different groups.
The %d indicates the first two arguments(a,b), %c indicates the third argument(c) and %f
indicates last arguments(e).

Eg 2
/* program to calculate simple interest*/
#include<stdio.h>
#include<conio.h>
Void main()
{
Float p,t,r,I;
Clrscr();
Printf(enter the principle);
Scanf(%f,&p);
Printf(enter the time);
Scanf(%f,&t);
Printf(enter the rate);
Scanf(%f,&r);
I=(p*t*r)/100;

Printf(\n the interest is %.2f,i);


Getch();
}
Eg 3
/* to calculate area of circle*/
#include<stdio.h>
#include<conio.h>
Void main()
{
Float a,r,pi=3.1416;
Clrscr();
Printf( enter the radius);
Scanf(%f,&r);
A=pi*r*r;
Printf(\nthe area is %.2f,a);
Getch();
}
/*program to calculate perimeter*/
#include<stdio.h>
#include<conio.h>
Void main()
{
Float p,l,b;
cLrscr();
printf(enter the value of breath);
scanf(%f,&b);
printf(enter the value of length);
scanf(%f,&l);

p=2*(l+b);
printf(the perimeter is %.2f,p);
getch();
}
Eg.5
/*program to calculate volume*/
#include<stdio.h>
#include<conio.h>
Void main()
{
Float v,l,b,h;
cLrscr();
printf(enter the value of breath);
scanf(%f,&b);
printf(enter the value of length);
scanf(%f,&l);
printf(enter the value of height);
scanf(%f,&h);
v=l*b*h;
printf(the volume is %.2f,v);
getch();
}

Vous aimerez peut-être aussi