0% found this document useful (0 votes)
8 views

02 Data Types

Uploaded by

Jagadish B
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

02 Data Types

Uploaded by

Jagadish B
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Data Types

Course Leader:
Jishmi Jos Choondal

1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives
• At the end of this lecture, student will be able to
– Explain data representation and limitations for Integer, floating
point and Character data types
– Explain user defined data types

2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Contents
• Integer Data Type
• Floating Point Data Type
• Character Data Type

3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types
• Data types indicate the type of data that a variable can
hold

• In C, data types are categorized into


– Built-in data types/Primitive data types
– Derived data types
– User-defined data types

4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Primitive Data Type

Primitive Data
Type
Size Data
(of memory Organization
Allotted) (How Bits are
Arranged and What
they Represent)

5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Mathematical Numbers
• Mathematics
– Integers
• -∞, …. -2, -1, 0, +1, +2, … + ∞
– Real Numbers
• Integers, Fraction and square roots
• -∞, …. -2,…,-1,…,-1/2, 0, …,+1,…, +2, … + ∞

6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Data Types for Numbers
• Computers - Integers
– Every data type has a fixed size based on the processor
– A processor can process a ‘word’ at a time
– The size of an integer is equal to word size of the system
– In‘64-bit’ processor, 64 bits or 8 bytes is the word size

• Computers – Real Numbers (Floating Point)


– Fixed size and hence limited range
– Representation of all real numbers in decimal form
• Example: 1.414, 3.12

7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integer Data Type Declarations
• They’re messages to the compiler

• For example the definition int i;


tells the compiler the type of variable i and instructs the compiler
to reserve space in memory for the variable

8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integer Data Type Declarations contd.
•Declaration
int <variable list>;

• Example
• int a; /*Single variable declaration*/
• int a,b,c; /*Multiple variables*/
• int a = 5; /*Single variable with initialisation, giving initial
value to variable*/
• int a = b = c = 5; /*multiple assignment statement*/

9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integers – Variables and Constants
int a = 5;
• 5 is an Integer constant
• a is an Integer variable

#define SIDES 4
• 4 is an Integer constant
• SIDES is a Symbolic constant

10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Integer Data Type

• We can represent (10)10 as (1010)2


• How do we represent (-10) 10 ?
(hint: 1’s complement +1=2’s complement)
-10=0110
-5=1011
-4=1100

11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Signed Integers
• Assume MSB represents sign – Call it sign bit
0 means ‘+’ and 1 means ‘-’
• Remaining numbers represent the actual number in
binary form
• Results in +0 and -0

• For example, in an 8-bit system


– 1000 0000 = - (0)2
– 0000 0000 = + (0)2

12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Unsigned Integers
• Whole numbers
0 to INT_MAX (Infinity in mathematics)
• No need for sign bit
• Equivalent binary form
• Declaration
unsigned int <variable list>;
• Example
• unsigned int a, b, c; /*Multiple variables*/
• unsigned int a; /*Single variable*/
• unsigned int a = 5; /*Single variable with initialisation*/

13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Range of data type
• Range of data type
– Signed : -2n-1 to 2n-1-1
– Unsigned : 0 to 2n-1
• Where ‘n’ is word size in bits
• If n is 8 bits (char):
– Signed : - 128 to +127
– Unsigned : 0 to + 255
• What if n is
– 16 bits (int or short int)
– 32 bits (long int, float)
– 64 bits (double)
14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Long and Short Modifiers
• Long
– When applied to int, it doubles the length in bits of the base
type that it modifies
– If an int is of 16-bits, then long int is of 32-bits
– When applied to double, it roughly doubles the precision
• Short
– Makes the size of an int half (important role by OS architecture,
2bytes for 32bits-OS, 4bytes for 64 bits-OS)

15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Floating Point Data Type
• Think of how money is represented 300.50 (Three hundred
Rupees and fifty Paisa)
• Consider
• 3.1413
• 2.0
• 12.18
• 122.1
– These are numbers where the decimal point moves or ‘floats’

• Such a representation of real numbers is called floating


point number system
16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Floating Point Data Type contd.
•Consider
• 3.1413 = 31413.0x10-4
• 2.0 = 2.0x100
• 12.18 = 1218.0x10-2
• 122.1 = 1221.0x10-1
• This representation is known as scientific notation
– The fixed number is called ‘Mantissa’ and the power of 10 as
‘Exponent’
• Floating point numbers are represented in computers as
one memory area having 2 numbers: the exponent and
mantissa
17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Floating Point Data Type contd.
• Both mantissa and exponent are represented in
– Binary form if positive
– 2’s complement notation if negative
• Hence both numbers have a sign bit

• How many bits are allotted is left to the designer of the


compiler
– Range cannot be estimated
• Some compilers implement a standard form of
representation known as IEEE-754

18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Double
• Used to indicate a double precision floating-point number
• Used when more accuracy in required in representing a
floating-point number

• Equivalent to float
– but the number of significant digits stored after the decimal
point is double that of the float

19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Floating Point Data Type - Declaration
• Declaration
– float a;
– float b = 5.0f;
– float a = 5.1f, b = 2.2f;
– double a;
– double b = 5.0;
– double a = 5.1, b = 2.2;

• Constants
– 2.0f is a float constant
– 2.0 is a double constant

20
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Character Data Type
• Character data types store numbers representing
characters
• These numbers are given by ANSI and called ASCII codes
‘A’ has the ASCII value 65

• Character is an Integer data type with fixed size of 1 byte

• All operators that Integers can be used with can also be


used with character operators

21
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Character Constants
• A single character enclosed in a pair of apostrophes
• Signed (-128 to +127) or unsigned(0 to 255)
• Example
char c = ‘A’ ; //notice the single quotes
int x=10+’A’ //arithmetic operations can be done
printf(“character is %c and its ASCII value is %d\n”,c,c);
printf(“Result is %d\n”,x);
• Output will be
character is A and its ASCII value is 65
Result is 75
22
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Type-cast
• (type) is known as type cast or cast operator
– Its use indicates that the programmer explicitly is converting
value between incompatible types
• Beware
– Do not convert between variables of data types with greater
storage space and lesser storage space
– smaller types can be automatically casted to bigger types
– Use cast operator if you know what you are doing
–Example:
int b = 10; char a = (char) b;
– a is a char variable and b is an int variable
– 10 is an integer constant

23
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Type-cast
• Use cast operator to convert from float to integer
– int a = (int) 10.5f;

• Beware
– Integer only expressions return integer values
– An expression must have at least one floating point
variable or constant to yield a floating point result

24
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• Data types specify the size, representation and
organisation of data in the computer’s memory
• Primitive Data types are the basic data types that are
provided by the programming language
• Three primitive data types are
– Integer
– Float
– Character

25
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Further Reading
Kernighan, B. W. and Richie, D. (1992) The C Programming
Language. 2nd ed., New Delhi:PHI.

26
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy