Friday 23 November 2012

Some More Data Types in C

Programming with C Unit 4

Unit 4 Some More Data Types
Structure
4.0 Introduction
4.1 Floatingpoint
Numbers
Self Assessment Questions
4.2 Converting Integers to Floatingpoint
and viceversa
Self Assessment Questions
4.3 Mixedmode
Expressions
Self Assessment Questions
4.4 The type cast Operator
Self Assessment Questions
4.5 The type char
Self Assessment Questions
4.6 Keywords
Self Assessment Questions
4.7 Summary
4.8 Terminal Questions
4.9 Answers to Self Assessment Questions
4.10 Answers to Terminal Questions
4.11 Exercises
4.0 Introduction
Integer is one of the fundamental data types. All C compilers support four
fundamental data types, namely integer (int), character (char), floating point
(float), and doubleprecision
floating point (double). Like integer data type,
other data types also offer extended data types such as long double and
signed char.
Programming with C Unit 4
Sikkim Manipal University Page No.: 48
C supports a rich set of operators. We have already used several of them,
such as =, +, ,
*, / and %. An operator is a symbol that tells the computer to
perform certain mathematical or logical manipulations. Operators are used
in programs to manipulate data and variables. They usually form a part of
the mathematical or logical expressions.
It is possible to combine operands of different data types in arithmetic
expressions. Such expressions are called mixedmode
arithmetic
expressions.
Objectives
At the end of this unit, you will be able to:
· Understand the concept of Real Numbers in C
· Understand the concept of Characters in C
· Combine different data types and form more complicated arithmetic
expressions
4.1 Floatingpoint
Numbers
Floating point (or real) numbers are stored in 32 bit (on all 16 bit and 32 bit
machines), with 6 digits of precision. Floating point numbers are defined in
C by the keyword float. When the accuracy provided by a float number is
not sufficient, the type double can be used to define the number. A double
data type number uses 64 bits giving a precision of 14 digits. These are
known as double precision numbers. To extend the precision further, we
may use long double which uses 80 bits. The following table shows all the
allowed combinations of floating point numbers and qualifiers and their size
and range on a 16bit
machine.
Programming with C Unit 4
Sikkim Manipal University Page No.: 49
Type Size (bits) Range
Float
Double
long double
32
64
80
3.4E38
to 3.4E+38
1.7E308
to 1.7E+308
3.4E4932
to 1.1E+4932
Table 4.1
Program 4.1: The following program illustrates typical declarations,
assignments and values stored in various types of variables.
main()
{
/* …….DECLARATIONS……………………..*/
float x, p;
double y, q;
unsigned k;
/* ……………….DECLARATIONS AND ASSIGNMENTS………..*/
int m=54321;
long int n=1234567890;
/*…………..ASSIGNMENTS……………………*/
x = 1.234567890000;
y = 9.87654321;
k = 54321;
p=q=1.0;
/*…………….PRINTING………………….*/
printf(“m=%d\n”,m);
printf(“n=%ld\n”,n);
printf(“x=%.12lf\n”,x);
printf(“x=%f\n”,x);
printf(“y=%.12lf\n”,y);
printf(“y=%lf\n”,y);
Programming with C Unit 4
Sikkim Manipal University Page No.: 50
printf(“k=%u p= %f q=%.12lf\n”,k,p,q);
}
Output
m = 11215
n = 1234567890
x = 1.234567880630
x = 1.234568
y = 9.876543210000
y = 9.876543
k = 54321 p = 1.000000 q= 1.000000000000
Program 4.2: Program to calculate the average of N numbers
#define N 10 /* SYMBOLIC CONSTANT */
main()
{
int count; /* DECLARATION OF
float sum, average, number; VARIABLES */
sum = 0; / * INITIALIZATION OF
count = 0; VARIABLES*/
while (count<N)
{
scanf(“%f”, &number);
sum = sum + number;
count = count + 1;
}
average = sum / N;
printf(“N = % d Sum = %f “, N, sum);
printf(“Average = %f”, average);
Programming with C Unit 4
Sikkim Manipal University Page No.: 51
Output
1
2.3
4.67
1.42
7
3.67
4.08
2.2
4.25
8.21
N= 10 Sum= 38.799999 Average= 3.880000
Program 4.3: Program to compute the roots of a quadratic equation
#include <math.h>
main()
{
float a,b,c,discriminant, root1, root2;
printf(“input the values of a,b and c\n”);
scanf (“%f %f %f”, &a, &b, &c);
discriminant = b * b – 4 * a *c;
if (discriminant<0)
printf(“roots are imaginary\n”);
else
{
root1 = (b
+ sqrt(discriminant)) / (2 * a);
root2 = (b
sqrt(
discriminant)) / (2 * a);
printf (“Root1 = %5.2f \n Root2 = %5.2f \n”, root1, root2);
}
}
Programming with C Unit 4
Sikkim Manipal University Page No.: 52
Output
input the values of a,b and c
2 4 16
Root1 = 2.00
Root2 = 4.00
input the values of a,b and c
1 2 3
roots are imaginary
Self Assessment Questions
i) State true or false.
When the accuracy provided by a float number is not sufficient, the
type long float can be used to define the number.
ii) A double data type uses __________ bits.
4.2 Converting Integers to Floatingpoint
and viceversa
C permits mixing of constants and variables of different types in an
expression, but during evaluation it adheres to very strict rules of type
conversion. We know that the computer considers one operator at a time,
involving two operands.
If the operands are of different types, the ‘lower’ type is automatically
converted to the ‘higher’ type before the operation proceeds. The result is of
higher type.
Given below is the sequence of rules that are applied while evaluating
expressions.
All short type are automatically converted to int ; then
1. If one of the operands is long double, the other will be converted to
long double and the result will be long double;
Programming with C Unit 4
Sikkim Manipal University Page No.: 53
2. else, if one of the operands is double, the other will be converted to
double and the result will be double;
3. else, if one of the operands is float, the other will be converted to float
and the result will be float;
4. else, if one of the operands is unsigned long int, the other will be
converted to unsigned long int and the result will be unsigned long
int;
5. else if one of the operands is long int and the other is unsigned int,
then:
· if unsigned int can be converted to long int, the unsigned int
operand will be converted as such and the result will be long int;
· else, both operands will be converted to unsigned long int and the
result will be unsigned long int;
6. else, if one of the operands is long int , the other will be converted to
long int and the result will be long int;
7. else, if one of the operands is unsigned int , the other will be converted
to unsigned int and the result will be unsigned int;
The final result of an expression is converted to type of the variable on the
left of the assignment sign before assigning the value to it. However, the
following changes are introduced during the final assignment:
1. float to int causes truncation of the fractional part.
2. double to float causes rounding of digits.
3. long int to int causes dropping of the excess higher order bits
Self Assessment Questions
i) State true or false.
If the operands are of different data types, the ‘lower’ type is
automatically converted to the ‘higher’ type before the operation
proceeds.
Programming with C Unit 4
Sikkim Manipal University Page No.: 54
ii) During the final assignment ________ to int causes dropping of the
excess higher order bits.
4.3 Mixedmode
Expressions
When one of the operands is real and the other is integer, the expression is
called a mixedmode
arithmetic expression. If either operand is of the real
type, then only the real operation is performed and the result is always real
number. Thus
25 / 10.0 = 2.5
Where as 25 / 10 =2
Self Assessment Questions
i) The value of the expression 22.0/10 is ________
4.4 The type cast Operator
C performs type conversions automatically. However, there are instances
when we want to force a type conversion in a way that is different from the
automatic conversion. Consider, for example, the calculation of ratio of
doctors to engineers in a town.
Ratio = doctor_number / engineer _number
Since doctor _number and engineer_number are declared as integers in
the program, the decimal part of the result of the division would be lost and
Ratio would represent a wrong figure. This problem can be solved by
converting locally one of the variables to the floating point as shown below:
Ratio = (float) doctor_number / engineer _number
The operator (float) converts the doctor_number to floating point for the
purpose of evaluation of the expression. Then using the rule of automatic
conversion, the division is performed in floating point mode, thus retaining
Programming with C Unit 4
Sikkim Manipal University Page No.: 55
the fractional part of the result. Note that in no way does the operator (float)
affect the value of the variable doctor_number. And also, the type of
doctor_number remains as int in the other parts of the program.
The process of such local conversion is known as casting a value. The
general form of cast is:
(typename)
expression
where typename
is one of the standard C data types. The expression may
be a constant, variable or an expression. The Table 4.2 shows some
examples of casts and their actions:
Example Action
X=(int) 8.5 8.5 is converted to integer by truncation.
A=(int) 21.3 / (int) 4.5 Evaluated as 21/4 and the result would be 5.
B=(double) sum/n Division is done in floating point mode.
Y= (int) (a+b) The result of a+b is converted to integer.
Z= (int) a+b a is converted to integer and then added to b.
P=cos(( double)x) Converts x to double before using it.
Table 4.2: Use of Casts
Program 4.4: The following program shows the use of casts
main()
{
/* Program to find average of two integers */
float avg;
int n=2,n1,n2;
printf(“enter any 2 numbers\n”);
scanf(“%d %d”,&n1,&n2);
avg=(n1+n2)/(float)n;
printf(“ their average is\n”,avg);
}
Programming with C Unit 4
Sikkim Manipal University Page No.: 56
Casting can be used to roundoff
a given value. Consider the following
statement:
X= (int) (y+0.5);
If y is 37.7, y+0.5 is 38.2 and on casting, the result becomes 38, the value
that is assigned to X. Of course, the expression , being cast is not changed.
When combining two different types of variables in an expression, never
assume the rules of automatic conversion. It is always a good practice to
explicitly force the conversion. It is more safer and more portable. For
example, when y and p are double and m is int , the following two
statements are equivalent.
y = p + m;
y = p + (double)m;
However, the second statement is preferable. It will work the same way on
all machines and is more readable.
Self Assessment Questions
i) State true or false
Casting can be used to roundoff
a given value.
ii) The value of A in the expression A=(int) 11.35 / (int)
14.5 is ___________.
iii) If the value of X is 35.2, what is the value of A in the following
expression?
A = (int)(X+0.5);
4.5 The type char
A single character can be defined as a character(char) type data.
Characters are usually stored in 8 bits (one byte) of internal storage. The
qualifier signed or unsigned may be explicitly applied to char. While
Programming with C Unit 4
Sikkim Manipal University Page No.: 57
unsigned chars have values between 0 and 255, signed chars have
values from 128
to 127.
A character constant is formed by enclosing the character within a pair of
single quote marks. So ‘b’, ‘.’ and ‘5’ are all valid examples of character
constants. Note that a character constant, which is a single character
enclosed in single quotes is different from a character string, which is any
number of characters enclosed in double quotes.
The format characters %c can be used in a printf statement to display the
value of a char variable at the terminal.
Program 4.5: The following program illustrates how to use char data
type.
#include<stdio.h>
main()
{
char c=’A’;
int a=65;
printf(“%c\n”, c);
printf(“%d\n”, c);
printf(“%c\n”,a);
}
Output
A
65
A
Note that with the format characters %d, the ASCII number of the character
is displayed. With the format character %c, the character corresponding to
the given ASCII number is displayed.
Programming with C Unit 4
Sikkim Manipal University Page No.: 58
Self Assessment Questions
i) What is the format character to display the value of a char variable?
ii) What is the output of the following C statement?
printf(“%c”, 70)
4.6 Keywords
Keywords are the reserved words of a programming language. All the
keywords have fixed meanings and these meanings cannot be changed.
Keywords serve as basic building blocks for program statements. The list of
all keywords in ANSI C are listed in the Table 4.3
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Table 4.3: ANSI C Keywords
All keywords must be written in lowercase. Some compilers may use
additional keywords that must be identified from the C manual.
Self Assessment Questions
i) All keywords must be written in ____________.
ii) State true or false:
default is not a valid keyword in C.
Programming with C Unit 4
Sikkim Manipal University Page No.: 59
4.7 Summary
Floating point(or real) numbers are stored in 32 bit (on all 16 bit and 32 bit
machines), with 6 digits of precision. Floating point numbers are defined in
C by the keyword float. When the accuracy provided by a float number is
not sufficient, the type double can be used to define the number.
Characters are usually stored in 8 bits (one byte) of internal storage. Like
integer data type other data types also offer extended data types such as
long double and signed char. C permits mixing of constants and variables
of different types in an expression, but during evaluation it adheres to very
strict rules of type conversion. When one of the operands is real and the
other is integer, the expression is called a mixedmode
arithmetic
expression. There are instances when we want to force a type conversion
in a way that is different from the automatic conversion. That is, by using
type cast operator. All keywords have fixed meanings and these meanings
cannot be changed.
4.8 Terminal Questions
1. Which of the following arithmetic expressions are valid? If valid , give the
value of the expression; otherwise give reason.
a) 7.5 % 3 b) 14 % 3 + 7 %2
c) 21 % (int) 4.5 d) 15.25 + 5.0
2. Find errors, if any, in the following declaration statements:
Int x;
float letter, DIGIT;
double = p, q
exponent alpha, beta;
m,n,z:INTEGER
short char c;
Programming with C Unit 4
Sikkim Manipal University Page No.: 60
long int m; count;
long float temp;
3. What would be the value of x after execution of the following
statements?
int x, y = 10;
char z = ‘a’;
x = y + z;
4. The _______ chars have values from 128
to 127.
4.9 Answers to Self Assessment Questions
4.1 i) False
ii) 64
4.2 i) True
ii) long int
4.3 i) 2.2
4.4 i) true
ii) 0
iii) 35
4.5 i) %c
ii) F
4.6 i) lowercase
ii) false
Programming with C Unit 4
Sikkim Manipal University Page No.: 61
4.10 Answers to Terminal Questions
1. a) invalid, because % can be used only with integers.
b) valid, answer is 3
c) valid, answer is 1
d) valid, answer is 10.25
2. Errors in the following statements
i) Int x;
Can be written as
int x;
ii) double = p, q
Can be written as
double p,q;
iii) exponent alpha, beta;
There is no data type exponent in C.
iv) m,n,z:INTEGER
Can be written as
int m,n,z;
v) short char c;
There is no data type short char in C.
vi) long int m; count;
Can be written as
long int m,count;
vii) long float temp;
There is no data type long float in C
3. 107
4. signed
Programming with C Unit 4
Sikkim Manipal University Page No.: 62
4.11 Exercises
1. Represent the following numbers using scientific notation:
a) 0.001 b)1.5
2. Represent the following scientific numbers into decimal notation:
a) 1.0E+2 b) 0.001E2
3. What is unsigned char? Explain.
4. What is short char? Explain.
5. Distinguish between float and double data types.

No comments:

Post a Comment