SlideShare a Scribd company logo
1
Fundamental Data Types
http://improvec.blogspot.in
2
Declaration
• All variables must be declared before
being used.
– Tells compiler to set aside an appropriate
amount of space in memory to hold a value.
– Enables the compiler to perform operations
using the declared variables.
http://improvec.blogspot.in
3
Basic Data Types
• char character
• int integer
• float floating-point
• double double floating-point
• void valueless
http://improvec.blogspot.in
4
Modifying the Basic Types
• Type modifier:
signed unsigned long short
• When a type modifier is used by itself, then int is
assumed.
Modifier Same As
signed signed int
unsigned unsigned int
long long int
short short int
http://improvec.blogspot.in
5
Data Type char
Have Seen:Chars are treated as small integers &
conversely small ints are treated as chars.
char c = ‘a’;
printf (“%c”, c + 1) b
printf (“%d”, c + 2) 99
Each char variable stored in 1 Byte 8 Bits:
27
26
25
24
23
22
21
20
128 64 32 16 8 4 2 1
a
0000110 1http://improvec.blogspot.in
6
• String of binary digits are called bit
strings.
• A bit string is interpreted as a binary
number. bnbn-1
……
b2b1b0
0 1 1 0 0 0 0 1 a
• This bit string has the value:
1×26
+ 1×25
+ 1×20
= 97http://improvec.blogspot.in
7
3 types:
1) char 2) unsigned char 3) signed char
Each uses 1 Byte.
Signed char -128 to 127
Unsigned char 0 to 255
http://improvec.blogspot.in
8
INTEGERS : Include the natural counting
numbers and their negatives.
INTEGRAL DATA TYPES:
int short long unsigned
Data Type int
http://improvec.blogspot.in
9
• The magnitude of the number that an int
variable can hold depends on
___________?
2 Byte word ~ -32 K To +32K
-215
, …, -3, -2, -1, 0, 1, 2, 3, …, 215
-1
4 Byte word ~ -2 Billion To +2 Billion
-231
, …. -3, -2, -1, 0, 1, 2, 3, …, 231
-1
word size(bits) of the machine is machine dependent
http://improvec.blogspot.in
10
• Integer overflow:
– Value too large for defined Storage Location.
– Typically program continues to run but
incorrect results.
– The programmer must strive at all times to
avoid integer overflow.
http://improvec.blogspot.in
11
Types short, long, unsigned
• short - used where conserving storage is a
concern (usually 2 Bytes).
-32 Thousand To +32 Thousand
-215
, -215
+1, …, -3, -2, -1, 0, 1, 2, 3, …, 215
-1
• long - needed for larger integers (usually 4
bytes).
-2 Billion To +2 Billion
31 31 31
http://improvec.blogspot.in
12
• Unsigned - no sign bit, same number
bytes as int.
The range, u, of values:
0 ≤ u ≤ 2wordsize
-1
2 Byte word ~ 0 To + 64 Thousand
0, 1, 2, 3, …, 216
-1
4 Byte word ~ 0 To + 4 Billion
0, 1, 2, 3, …, 232
-1http://improvec.blogspot.in
13
Suffix:
u - 20u
l – 20l
ul - 20ul
(case not significant)
IF no suffix with constant - system will choose the
first of : int - long - unsigned long
That can hold the value.
http://improvec.blogspot.in
14
float double long double
• Suffixes for constants
f or F float 3.7 F
l or L long double 3.7 L
• Any unsuffixed floating constant is of type
double (working type).
The Floating Types
http://improvec.blogspot.in
15
• Notation: exponential or decimal
Must have Exponent or Dec pt or Both.
1.234567e5 123456.7
1.234567e-3 0.001234567
0e0 correct 0.0
.e0 wrong
May not contain any blanks or special characters.
• Typical Storage: Float < Double
http://improvec.blogspot.in
16
Precision: The number of significant decimal
digits that floating value carries.
Range: Limits of largest & smallest possible
values that can be in a variable of that
type.
Float: 4 Bytes - about 6 decimal places of
accuracy- single precision.
Double: 8 Bytes - about 15 decimal places
of accuracy- double precision.http://improvec.blogspot.in
17
Internal Representation
FLOAT:
01 8 9 31
SEEEEEEEE mmmmm…m
DOUBLE:
01 11 12 63
SEEEEEEEEEEE mmmmmmm…m
Implied mantissa of 1 is not stored.
Sign bit (1 for neg-otherwise pos).http://improvec.blogspot.in
18
Float:
Precision: 6 significant digits.
Range: 10-38
to 10+38
0.d1d2d3d4d5d6× 10n
Double:
Precision: 15 significant digits (252
≈ 1015
)
Range: 10-308
to 10+308
0.123451234512345 × 103
http://improvec.blogspot.in
19
Note:
1. Not all Real numbers are exactly
representable in binary memory.
2. Floating Arithmetic ops, unlike integer
arithmetic, may not be exact.
http://improvec.blogspot.in
20
• Unary operator used to find the number of Bytes
needed to store an object.
sizeof(object)
• Object
– Data type int , float, …
– Expression a + b
Array
Will cover
later
Structure
Compile-Time Operator sizeof
http://improvec.blogspot.in
21
Assuming that integers are 4 bytes and
doubles are 8 bytes.
double f;
printf("%d ",sizeof (f)); 8
printf(''%d", sizeof(int)); 4
http://improvec.blogspot.in
22
sizeof(char) = 1
sizeof(short) ≤ sizeof(int) ≤ sizeof(long)
sizeof(signed)=sizeof(unsigned)=sizeof(int)
sizeof(float)
<= sizeof(double )
<= sizeof( long double)
http://improvec.blogspot.in
23
Conversions
• When constants and variables of different types
are mixed in an expression, the compiler converts
all operands to the type of the largest operand-
Called Type Promotion.
- First, all char and short values are automatically
elevated to int. Called integral promotion.
- int + int = int
- short + short = int
- Arithmetic Conversion (See pg. 217)
http://improvec.blogspot.in
24
char ch;
int i;
float f;
double d,result;
result= (ch / i) + (f * d) – (f + i);
int double float
double
http://improvec.blogspot.in
25
• Casts - Explicit conversions.
(type) expression
If i is int:
(float) i will change expression value to
float, i is not changed.
Casts
(float) i/2
http://improvec.blogspot.in
26
• Apply to an Expression:
(float) (‘c’ + 3)
• Cannot apply to an Assignment:
(int) f = 3: illegal
• As an unary operator, a cast has the same
precedence as any other unary operator.
(float) i + 3 ( (float) i ) + 3
http://improvec.blogspot.in
27
/* print i and i/2 with fractions */
int main(void)
{
int i;
for(i=l; i<=100; ++i)
printf(''%d //2 is: %f
n", i, (float) i /2);
return 0;
}
http://improvec.blogspot.in
28
General forms:
(int) char expression- ordinal value of char
expression
(char) int expression- character with the
ordinal value of int
expression
(int) float expression- truncates the float
expression
(float) int expression- converts int to float
(double) float exp- converts float to
doublehttp://improvec.blogspot.in

More Related Content

PPTX
Patterns of 64-bit errors in games
PDF
Lesson 11. Pattern 3. Shift operations
PPTX
Computer arithmetic
PPT
Computer architecture
PDF
Lesson 24. Phantom errors
PPTX
Arithmetic for Computers
PPTX
Digital and logic designs presentation
PDF
Highlevel assemly
Patterns of 64-bit errors in games
Lesson 11. Pattern 3. Shift operations
Computer arithmetic
Computer architecture
Lesson 24. Phantom errors
Arithmetic for Computers
Digital and logic designs presentation
Highlevel assemly

What's hot (20)

PPTX
Computer arithmetic
PDF
Module 00 Bitwise Operators in C
PDF
Using Cipher Key to Generate Dynamic S-Box in AES Cipher System
PPT
Bitwise operators
PPTX
Bitwise Operations in Programming
PDF
In what way can C++0x standard help you eliminate 64-bit errors
PDF
Aes128 bit project_report
PDF
Конверсия управляемых языков в неуправляемые
PPTX
Parallel Adder and Subtractor
PPTX
Binary parallel adder
PPTX
PPTX
Adder & subtractor (Half adder, Full adder, Half subtractor, Full subtractor)
PPTX
Chapter 03 arithmetic for computers
PPS
Arithmetic Operations
PPTX
A (brief) overview of Span<T>
PDF
SunPy: Python for solar physics
PPTX
Signed Addition And Subtraction
PPTX
Computer Arithmetic
PDF
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Computer arithmetic
Module 00 Bitwise Operators in C
Using Cipher Key to Generate Dynamic S-Box in AES Cipher System
Bitwise operators
Bitwise Operations in Programming
In what way can C++0x standard help you eliminate 64-bit errors
Aes128 bit project_report
Конверсия управляемых языков в неуправляемые
Parallel Adder and Subtractor
Binary parallel adder
Adder & subtractor (Half adder, Full adder, Half subtractor, Full subtractor)
Chapter 03 arithmetic for computers
Arithmetic Operations
A (brief) overview of Span<T>
SunPy: Python for solar physics
Signed Addition And Subtraction
Computer Arithmetic
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Ad

Viewers also liked (20)

PPT
User defined data type
PPT
Types of memory 10 to11
PPT
Types of memory
PDF
Chapter 2 basic element of programming
PDF
C++ Programming
PPTX
Hibernate example1
PPT
38 paged segmentation
PPT
Fundamentals
PPTX
File management53(1)
PPT
35. multiplepartitionallocation
PPT
36 fragmentaio nnd pageconcepts
PPT
40 demand paging
PPT
Thrashing allocation frames.43
PPT
C++ Language
PPT
39 virtual memory
PPTX
Data types
PPT
37 segmentation
PPTX
Segmentation in Operating Systems.
PPT
04 cache memory
User defined data type
Types of memory 10 to11
Types of memory
Chapter 2 basic element of programming
C++ Programming
Hibernate example1
38 paged segmentation
Fundamentals
File management53(1)
35. multiplepartitionallocation
36 fragmentaio nnd pageconcepts
40 demand paging
Thrashing allocation frames.43
C++ Language
39 virtual memory
Data types
37 segmentation
Segmentation in Operating Systems.
04 cache memory
Ad

Similar to Data type (20)

PPTX
Lecture 2 introduction to Programming languages C.pptx
PPT
Datatypes
PPT
Mesics lecture 3 c – constants and variables
PDF
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
PPTX
Learn C LANGUAGE at ASIT
PPTX
COM1407: Variables and Data Types
PPT
simple notes for ug students for college
PPTX
Lecture 02 Programming C for Beginners 001
PPTX
Data Type in C Programming
PPT
Primitive Data Types and Variables Lesson 02
PPT
FP 201 Unit 2 - Part 2
PPTX
Lecture 2
PPT
C language Unit 2 Slides, UPTU C language
PPTX
Data types
PPT
C++ data types
PPT
Unit 1 Built in Data types in C language.ppt
PDF
Data types
PPTX
Data types slides by Faixan
PPTX
C Session 2.pptx for engninering students
Lecture 2 introduction to Programming languages C.pptx
Datatypes
Mesics lecture 3 c – constants and variables
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Learn C LANGUAGE at ASIT
COM1407: Variables and Data Types
simple notes for ug students for college
Lecture 02 Programming C for Beginners 001
Data Type in C Programming
Primitive Data Types and Variables Lesson 02
FP 201 Unit 2 - Part 2
Lecture 2
C language Unit 2 Slides, UPTU C language
Data types
C++ data types
Unit 1 Built in Data types in C language.ppt
Data types
Data types slides by Faixan
C Session 2.pptx for engninering students

More from myrajendra (20)

PPTX
Jdbc workflow
PPTX
2 jdbc drivers
PPTX
3 jdbc api
PPTX
4 jdbc step1
PPTX
Dao example
PPTX
Sessionex1
PPTX
Internal
PPTX
3. elements
PPTX
2. attributes
PPTX
1 introduction to html
PPTX
Headings
PPTX
Forms
PPT
PPTX
Views
PPTX
Views
PPTX
Views
PPT
Starting jdbc
PPTX
Properties
PPTX
Java.sql package
PPTX
Interface callable statement
Jdbc workflow
2 jdbc drivers
3 jdbc api
4 jdbc step1
Dao example
Sessionex1
Internal
3. elements
2. attributes
1 introduction to html
Headings
Forms
Views
Views
Views
Starting jdbc
Properties
Java.sql package
Interface callable statement

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Institutional Correction lecture only . . .
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Complications of Minimal Access Surgery at WLH
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Pre independence Education in Inndia.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Business Ethics Teaching Materials for college
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Institutional Correction lecture only . . .
Renaissance Architecture: A Journey from Faith to Humanism
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
Complications of Minimal Access Surgery at WLH
O5-L3 Freight Transport Ops (International) V1.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
O7-L3 Supply Chain Operations - ICLT Program
Pre independence Education in Inndia.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Business Ethics Teaching Materials for college

Data type

  • 2. 2 Declaration • All variables must be declared before being used. – Tells compiler to set aside an appropriate amount of space in memory to hold a value. – Enables the compiler to perform operations using the declared variables. http://improvec.blogspot.in
  • 3. 3 Basic Data Types • char character • int integer • float floating-point • double double floating-point • void valueless http://improvec.blogspot.in
  • 4. 4 Modifying the Basic Types • Type modifier: signed unsigned long short • When a type modifier is used by itself, then int is assumed. Modifier Same As signed signed int unsigned unsigned int long long int short short int http://improvec.blogspot.in
  • 5. 5 Data Type char Have Seen:Chars are treated as small integers & conversely small ints are treated as chars. char c = ‘a’; printf (“%c”, c + 1) b printf (“%d”, c + 2) 99 Each char variable stored in 1 Byte 8 Bits: 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1 a 0000110 1http://improvec.blogspot.in
  • 6. 6 • String of binary digits are called bit strings. • A bit string is interpreted as a binary number. bnbn-1 …… b2b1b0 0 1 1 0 0 0 0 1 a • This bit string has the value: 1×26 + 1×25 + 1×20 = 97http://improvec.blogspot.in
  • 7. 7 3 types: 1) char 2) unsigned char 3) signed char Each uses 1 Byte. Signed char -128 to 127 Unsigned char 0 to 255 http://improvec.blogspot.in
  • 8. 8 INTEGERS : Include the natural counting numbers and their negatives. INTEGRAL DATA TYPES: int short long unsigned Data Type int http://improvec.blogspot.in
  • 9. 9 • The magnitude of the number that an int variable can hold depends on ___________? 2 Byte word ~ -32 K To +32K -215 , …, -3, -2, -1, 0, 1, 2, 3, …, 215 -1 4 Byte word ~ -2 Billion To +2 Billion -231 , …. -3, -2, -1, 0, 1, 2, 3, …, 231 -1 word size(bits) of the machine is machine dependent http://improvec.blogspot.in
  • 10. 10 • Integer overflow: – Value too large for defined Storage Location. – Typically program continues to run but incorrect results. – The programmer must strive at all times to avoid integer overflow. http://improvec.blogspot.in
  • 11. 11 Types short, long, unsigned • short - used where conserving storage is a concern (usually 2 Bytes). -32 Thousand To +32 Thousand -215 , -215 +1, …, -3, -2, -1, 0, 1, 2, 3, …, 215 -1 • long - needed for larger integers (usually 4 bytes). -2 Billion To +2 Billion 31 31 31 http://improvec.blogspot.in
  • 12. 12 • Unsigned - no sign bit, same number bytes as int. The range, u, of values: 0 ≤ u ≤ 2wordsize -1 2 Byte word ~ 0 To + 64 Thousand 0, 1, 2, 3, …, 216 -1 4 Byte word ~ 0 To + 4 Billion 0, 1, 2, 3, …, 232 -1http://improvec.blogspot.in
  • 13. 13 Suffix: u - 20u l – 20l ul - 20ul (case not significant) IF no suffix with constant - system will choose the first of : int - long - unsigned long That can hold the value. http://improvec.blogspot.in
  • 14. 14 float double long double • Suffixes for constants f or F float 3.7 F l or L long double 3.7 L • Any unsuffixed floating constant is of type double (working type). The Floating Types http://improvec.blogspot.in
  • 15. 15 • Notation: exponential or decimal Must have Exponent or Dec pt or Both. 1.234567e5 123456.7 1.234567e-3 0.001234567 0e0 correct 0.0 .e0 wrong May not contain any blanks or special characters. • Typical Storage: Float < Double http://improvec.blogspot.in
  • 16. 16 Precision: The number of significant decimal digits that floating value carries. Range: Limits of largest & smallest possible values that can be in a variable of that type. Float: 4 Bytes - about 6 decimal places of accuracy- single precision. Double: 8 Bytes - about 15 decimal places of accuracy- double precision.http://improvec.blogspot.in
  • 17. 17 Internal Representation FLOAT: 01 8 9 31 SEEEEEEEE mmmmm…m DOUBLE: 01 11 12 63 SEEEEEEEEEEE mmmmmmm…m Implied mantissa of 1 is not stored. Sign bit (1 for neg-otherwise pos).http://improvec.blogspot.in
  • 18. 18 Float: Precision: 6 significant digits. Range: 10-38 to 10+38 0.d1d2d3d4d5d6× 10n Double: Precision: 15 significant digits (252 ≈ 1015 ) Range: 10-308 to 10+308 0.123451234512345 × 103 http://improvec.blogspot.in
  • 19. 19 Note: 1. Not all Real numbers are exactly representable in binary memory. 2. Floating Arithmetic ops, unlike integer arithmetic, may not be exact. http://improvec.blogspot.in
  • 20. 20 • Unary operator used to find the number of Bytes needed to store an object. sizeof(object) • Object – Data type int , float, … – Expression a + b Array Will cover later Structure Compile-Time Operator sizeof http://improvec.blogspot.in
  • 21. 21 Assuming that integers are 4 bytes and doubles are 8 bytes. double f; printf("%d ",sizeof (f)); 8 printf(''%d", sizeof(int)); 4 http://improvec.blogspot.in
  • 22. 22 sizeof(char) = 1 sizeof(short) ≤ sizeof(int) ≤ sizeof(long) sizeof(signed)=sizeof(unsigned)=sizeof(int) sizeof(float) <= sizeof(double ) <= sizeof( long double) http://improvec.blogspot.in
  • 23. 23 Conversions • When constants and variables of different types are mixed in an expression, the compiler converts all operands to the type of the largest operand- Called Type Promotion. - First, all char and short values are automatically elevated to int. Called integral promotion. - int + int = int - short + short = int - Arithmetic Conversion (See pg. 217) http://improvec.blogspot.in
  • 24. 24 char ch; int i; float f; double d,result; result= (ch / i) + (f * d) – (f + i); int double float double http://improvec.blogspot.in
  • 25. 25 • Casts - Explicit conversions. (type) expression If i is int: (float) i will change expression value to float, i is not changed. Casts (float) i/2 http://improvec.blogspot.in
  • 26. 26 • Apply to an Expression: (float) (‘c’ + 3) • Cannot apply to an Assignment: (int) f = 3: illegal • As an unary operator, a cast has the same precedence as any other unary operator. (float) i + 3 ( (float) i ) + 3 http://improvec.blogspot.in
  • 27. 27 /* print i and i/2 with fractions */ int main(void) { int i; for(i=l; i<=100; ++i) printf(''%d //2 is: %f n", i, (float) i /2); return 0; } http://improvec.blogspot.in
  • 28. 28 General forms: (int) char expression- ordinal value of char expression (char) int expression- character with the ordinal value of int expression (int) float expression- truncates the float expression (float) int expression- converts int to float (double) float exp- converts float to doublehttp://improvec.blogspot.in