Вы находитесь на странице: 1из 12

IGNOU MCA Semester-1 Practical

C-Programming
Date: 31/08/2014(Sun) Time : 11:00AM
onwards
Reference Book on C-language: ANSI C by E.
Balagurusamy, Publ: Tata McGraw Hill Book
Company
Some important Features of C-language:
1.
C is a high level language.
2.
C is very much case sensitive.
Normally we write C-program using small
letters. Most of the keywords of Clanguage consists of small letters.
3.
C is a function oriented language.
There should be at least one function
called as main(). We can use our own
functions in any C-program. A program
comprises of set of instructions.
4.
Any statement in C-language must be
terminated by a semicolon(;). Example :
y=5; x=2*x;
5.
All variables in C-language must be
defined first otherwise we can not use
those variables. Example:
int a,b;

float x,y;
6.
We can write comments in C-program
as follows:
/* This my comment */ .
So therefore a comment line will begin
with /* and should end with */
/* Write a program to calculate sum of
First n natural numbers */
7.
Arithmetic operators in C-language:
(i)
Addition operator : +
(ii) Subtraction operator : (iii) Multiplication operator : *
(iv) Division Operator : / (forward slash)
(v) Modulo operator : %
(vi) Pre-increment
operator/
Postincrement operator : ++
Example :
Post increment operator :x++; This
is equivalent to x=x+1;
Pre increment operator : ++x; It
means x=x+1;
(vii) Pre-dcrement
/
Post-decrement
operator : -Example:
--x
x=x-1 pre
decrement
x-- x=x-1 post
decrement

(viii) Assignment operator : =


Example : y=x+5; assigning x+5
to a location Y.
Note: In c-language we do not have
any exponent operator. If you want
to calculate x2
Then you have to write a program to
calculate power of x.
(ix) Relational operators in C-language:
(1) Greater than : >
(2) Less than
:<
(3) Equal
: ==
(4) Greater than or equal to : >=
(5) Less than or equal to : <=
(6) Not equal
: !=
We use relational operators to form
some condition in a program.
Example:
if(x>y)
printf(x>y)
else
printf(x<=y);
(x)

Logical Operator:
(1) Logical Not : !

(2) Logical And : &&


(3) Logical Or
: ||
Logical Not operator : !
!(true) = false
!(false)=true
Logical And operator:
T
T
F
F

&& T
&& F
&& T
&& F

=T
=F
=F
=F

Logical Or operator :
T
F
T
F
(xi)

||
||
||
||

F
T
T
F

=T
=T
=T
=F

Basic Data types in C-language:


(1) Int type variable/data : Any
integer type variable or data to
be defined as int.
Example : int a, b, c;

a=5;
b=2*a;
c=a+b;
In 16-bit compiler the storage of
int type variable in memory is
equal to 2 bytes or 16 bits.
Calculate maximum value in 16bits where the most significant
bit(MSB) is used for the sign of
the number. If MSB=0 then the
value will be considered as +ve
value and if it is 1 then the value
will be considered as negative.
Maximum value we can store in
16 bits=215-1=32767
Minimum value we can store in
16 bits
=-215 =-32768
So we can store any number from
-32768 to +32767.
(2) float type variable or data: To
define any fractional data such as
1.234, -3.456 etc we have define
the variables as float a,b,c;
Example:
float a,b,c;
a=2.25; /* a=2.25 */

b=2;
/* b=2 */
c=a*b; /* c=2.25*2=4.5 */
Floating variables will be stored
in 4 bytes in memory. Out of 4
bytes 3 bytes are reserved for
factional part of a number and 1
byte is reserved for the exponent:
Example : x=123 1.23E02
1.23*102
The precision of number is up to
7 significant digits and the 7-th
digit is always rounded off.
Example :
float p=3.14159265358979284;
=3.141593 this value
will be stored in memory.
The maximum value of the order
of 1038
Note: floating variables are also
called single precision type
variable.
(3) double type variable/data :
To
define double type variable we
use a statement called double.
Example:

double
p=3.14159265358979284;
Now the storage will be made in
8 bytes or in 32 bits and the
precision of a number is up to 16
significant digit and the last digit
is always rounded off.
So there the value of p here will
be
stored
as
p=3.141592653589793
(4) long double type variable : To
define long double type variable
or data we use a statement
called long double.
Example : long double a,b,c;
Long double variable will occupy
10 bytes in the memory. The
precision of a number is up to 18
significant digit and last digit is
always rounded off. This is the
highest possible precision in Clanguage.
(5) long int type variable or data:
we define variable as

long int a,b,c;


a=123456789;
b=3456789;
c=a+b;
The long int type variable will
occupy 4 bytes in the memory.
The maximum value we can store
in 4 bytes=231-1
Minimum value we can store in 4
bytes
=-231
(6) char type variable or data : Any
character type data or variable
can be defined by a statement
called char.
Example: char ch1=A, ch2=B,
ch3=C;
Here ch1,ch2,ch3 are 3 character
type variables. Storage=1 byte.
Q-1(i):AS-1: Write algorithm to calculate
S=(1)+(1+2)+(1+2+3)++(1+2+3+
+n). Print s.
Step-1: start
Step-2: Input n
Step-3: s1=0
Step-3a: s2=0

Step-4: c=1
Step-5: s1=s1+c
Step-6: s2=s2+s1
Step-7: c=c+1
Step-8: if c<=n then go to step-5
Step-9: Print s2
Step-10: end

Вам также может понравиться