SlideShare uma empresa Scribd logo
1 de 37
Balochistan University of IT & MS
Lecture 3
Data Types in C
short, int, long, float, double, char
CS106
Introduction to Programming
Methodologies & Abstraction
Fall 2005
Balochistan University
of I.T & M.S
Faculty of System Sciences
Sadique Ahmed Bugti
Balochistan University of IT & MS
The Data Type int
• int is short for integer
• Integers are whole numbers, which have
no fractional part, or decimal point.
– They can be both positive and negative.
– Unsigned integers (positive values only) are
supported.
– In addition, there are short and long
integers.
• Examples of integers include:
0 57 4567 128933
Balochistan University of IT & MS
The Data Type int
• The keyword used to define integers is:
int
• Integer variables are declared using:
int variable_name;
• For example:
int a;
• declares that you want to create an int
variable called a
Balochistan University of IT & MS
The Integral Types
• int is considered the “natural” type for
working with integers.
– Other integral types such as char, short and
long are intended for specialized use.
Balochistan University of IT & MS
long int
• Sometimes extra digits are needed to
store numbers larger than those allowed
in a normal int.
long int variable_name;
• The long qualifier informs C that extra
storage is required for the integer.
• Long integers are normally in the range:
-231… 231-1 or -2147483648...2147483647
Balochistan University of IT & MS
short int
• Sometimes less digits are needed to
store a number:
short int variable_name;
• The short qualifier informs C that less
storage is required for the integer.
• Short integers are normally in the range:
-215… 215-1 or -32768..32767
Balochistan University of IT & MS
unsigned integers
• Unsigned integers contain positive values only
– An integer with n bits uses n bits for the number
– An integer with n bits → 2n integers in the range
[0,2n-1]
e.g. if n=16 → 65536 integers, 0..65535
• Unsigned integers are declared:
unsigned int variable_name;
unsigned short variable_name;
unsigned long variable_name;
Balochistan University of IT & MS
signed integers
• Signed integers allow both positive and
negative values:
– An integer with n bits uses n-1 bits for the
number and 1 bit for the sign
• (+ve = 0, -ve = 1)
– 2n integers in the range [-(2n-1),(2n-1-1)]
e.g. n=16 → 65536 integers, -322768..32767
Balochistan University of IT & MS
signed integers
• All int declarations default to signed.
• Signed integers are declared:
signed int variable_name;
which is equivalent to:
int variable_name;
Balochistan University of IT & MS
Integers in C
On a 16-bit System
Type Approximate Range
short -32768..32767
unsigned short 0..65535
int -32768..32767
unsigned int 0..65535
long -2147483648..2147483647
unsigned long 0..4294967295
Balochistan University of IT & MS
A note on ints
• Suffixes can be appended to an integer
constant to specify its type.
– Can be used with int, long, or unsigned long
Example Type Suffix
u or U unsigned 37U
l or L long 37L
ul or UL unsigned long 37UL
Balochistan University of IT & MS
Integer Overflow
int b = 2000000000, c =2000000000;
a = b + c; /* out of range */
• At run-time the variable a may be assigned an
incorrect value. The logical value of the
• expression b+c is 4 billion, which is greater
• than the maximum value which can be stored
in a variable of type int.
• This is known as integer overflow.
Balochistan University of IT & MS
Integer Overflow
• How do we fix this problem?
• What if we declare:
long a, b = 2000000000, c = 3000000000;
– Does this work?
• What about trying:
unsigned long a, b = 2000000000, c = 2000000000;
Balochistan University of IT & MS
Floating Point Numbers
• A real or floating-point number has an integral
part and a fractional part which are separated
by a decimal point.
e.g. 0.01 70.456 3e+5 .5067e-16 3.14159
• Floating-point numbers can be either positive
or negative
– Floating-point numbers in C are always signed
• ANSI C provides three floating types:
1. float
2. double
3. long double.
Balochistan University of IT & MS
Floating Point Numbers
• Exponential (scientific) notation can be used to
represent real numbers:
e ± exponent
123000.0 = 1.23x105 = 1.23e5 = 1.23E5
• e or E is read as “times 10 to the power”
e.g. 0.34e-4 = 0.000034
2500 = 25e2
e.g. 333.7777e-22
Integer Fraction Exponent
333 7777 e-22
Balochistan University of IT & MS
Floating Point Numbers
Examples
Number Scientific Notation Exponential Notation
1000000000 1.0x109 1.0e9
123000 1.23x105 1.23e5
322.56 3.2256x102 3.2256e2
0.000056 5.6x10-5 5.6e-5
Balochistan University of IT & MS
A note on floats
• A suffix can be appended to a floating
constant to specify its type:
Suffix Type Example
F f or F float 3.7
l or L long double 3.7L
• The working floating type in C is double,
not float
Balochistan University of IT & MS
float
• The type float denotes normal precision
real numbers.
• Single precision floating-point numbers
are declared using:
float variable_name;
Balochistan University of IT & MS
double
• The type double denotes double
precision real-numbers.
• Double precision floating-point numbers
are declared using:
double variable_name;
• The qualifier long double denotes
extended precision.
long double variable_name;
Balochistan University of IT & MS
Examples of Floating-Point
Numbers
• The following are examples of valid
floating point numbers in C:
3.14159 /*of type double*/
314.159e-2F /*of type float*/
0e0 /*equivalent to 0.0*/
1. *equivalent to 1.0*/
Balochistan University of IT & MS
Examples of Floating-Point
Numbers
• The following are examples of invalid
floating point numbers in C:
3.14,159 /*comma not allowed*/
314159 /*decimal point or
exponent needed*/
.e0 /*integral or
fractional part needed*/
Balochistan University of IT & MS
Floating Point Types in C
• The possible values that a floating type can be
assigned are described in terms of attributes
called precision and range.
– Precision describes the number of significant
decimal places that a floating value carries.
– The range describes the limits of the largest and
smallest positive floating point values that can
be represented in a variable of that type.
– e.g. a float has precision of six significant
figures
Balochistan University of IT & MS
Floating Point Types in C
On a 16-bit System
• For example:
x=123.456789 /*9 significant digits*/
results in x being assigned the value:
0.123456x103
Type Approximate
Range
Significant
Digits
float 10-37..1038 6
double 10-307..10-308 15
long double 10-4931..10-4932 17
Balochistan University of IT & MS
Floating Point
• How small is 10-37?
• The mass of one electron is
approximately 10-27 grams, 10-37 is one
ten-billionth of 10-27
• How large is 1037?
• If you multiply the diameter of the Milky
Way galaxy in kilometers by a trillion
(1,000,000,000,000), the result is just
one ten-thousandth of 1037
Balochistan University of IT & MS
char
• The type char represents single
characters.
– A very short integer
• Characters are enclosed in single quotes
( ‘ ).
– ‘A’, ‘a’, and ‘!’ are character constants
• Characters are declared using the
statement:
char variable_name;
Balochistan University of IT & MS
char
• An example of a character value is the
letter A. An example of declaring a
character variable called letter is,
char letter;
letter = 'A';
• Note the assignment of the character A
to the variable letter is done by enclosing
the value in single quotes.
Balochistan University of IT & MS
char
• The backslash character (  ) is called
the escape character.
– It is used to signal that a special character
follows.
– For example ’ can be used to assign an
apostrophe to a character variable.
• Note:
– While characters are enclosed in single
quotes ( ‘ ), a different type, the string, is
enclosed in double quotes ( “ ).
Balochistan University of IT & MS
Character Types in C
On a 16-bit System
Type Approximate Range
char (signed) -128..127
unsigned char 0..255
Balochistan University of IT & MS
char – special characters
Character Name Meaning
b Backspace Move the cursor to the left
by one character
n New line Go to next line
r Return Go to beginning of current
Line
t Tab Advance to next tab stop
’ Apostrophe Character ‘
” Double quote Character “
 Backslash Character
Balochistan University of IT & MS
Couldn’t a double be used for all
numbers?
• Is having more than one numeric type
necessary?
– int versus double
• Operations involving integers are faster
• Less storage space is needed
• Operations with integers are always precise
• There is some loss of accuracy with floating-
point numbers
Balochistan University of IT & MS
Couldn’t a double be used for all
numbers?
• With doubles, a larger range of numbers can
be represented as compared to type int.
• For example:
– An 16-bit grayscale image of size 1024 x 2048
contains 2,097,152 pixels
• int @ 2 bytes per pixel = 4,096kb = 4mb
• double @ 8 bytes per pixel = 16mb
– If the image were color (Red, Green, Blue),
storage would be increased threefold.
• int → 12mb
• double → 48mb
Balochistan University of IT & MS
Type Qualifiers
• The keyword const is called a type
qualifier because it is used in
declarations to tell the compiler how
identifiers can be used
Balochistan University of IT & MS
const
• Sometimes you want to use a value that
does not change, such as π.
const float PI=3.1415927;
• The general form of a constant
declaration is:
const type variable = value;
• Constants must be initialized at
declaration time and can never be
changed.
Balochistan University of IT & MS
const
• Note:
– It is usual to initialize a const with a value as
it cannot get a value any other way.
• Examples of constants:
const float PI = 3.1415927;
const int TRUE = 1;
const char letter = 'a‘;
Balochistan University of IT & MS
Note
• The standard header file limits.h defines
constants for the various integer
numerical limits.
eg: CHAR_MIN, CHAR_MAX,INT_MAX etc.
• The standard header file float.h defines
constants for the various floating-point
numerical limits.
eg: FLT_MIN, FLT_MAX, DBL_MIN etc.
Balochistan University of IT & MS
Overflow Error
• The following will work on a Unix system, but
will fail on a PC:
int zip;
…
zip = 92126;
• Why does this fail?
– On Unix systems, integers are 32 bits, providing
a range of –231.. 231-1.
– On the PC, integers are 16 bits , so the range is
215.. 215-1
Balochistan University of IT & MS
The End

Mais conteúdo relacionado

Mais procurados

Mais procurados (18)

Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Mql4 manual
Mql4 manualMql4 manual
Mql4 manual
 
C Sharp Nagina (1)
C Sharp Nagina (1)C Sharp Nagina (1)
C Sharp Nagina (1)
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Data types in c language
Data types in c languageData types in c language
Data types in c language
 
Character set of c
Character set of cCharacter set of c
Character set of c
 
static dictionary technique
static dictionary techniquestatic dictionary technique
static dictionary technique
 
Data types in C
Data types in CData types in C
Data types in C
 
Numeric Data Types & Strings
Numeric Data Types & StringsNumeric Data Types & Strings
Numeric Data Types & Strings
 
Data type
Data typeData type
Data type
 
302 sargent word2007-ssp2008
302 sargent word2007-ssp2008302 sargent word2007-ssp2008
302 sargent word2007-ssp2008
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Data types
Data typesData types
Data types
 
Binary codes
Binary codesBinary codes
Binary codes
 

Semelhante a Data Types in C

Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_outputAnil Dutt
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckseidounsemel
 
C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxMurali M
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITASIT
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by FaixanٖFaiXy :)
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introductionnikshaikh786
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageDr.Florence Dayana
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.pptTanuGohel
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.pptMEHALAS3
 
Basics of C (1).ppt
Basics of C (1).pptBasics of C (1).ppt
Basics of C (1).pptSteveIrwin25
 
C programming basic pdf.ppt
C programming basic pdf.pptC programming basic pdf.ppt
C programming basic pdf.pptKauserJahan6
 

Semelhante a Data Types in C (20)

Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ck
 
FUNDAMENTAL OF C
FUNDAMENTAL OF CFUNDAMENTAL OF C
FUNDAMENTAL OF C
 
C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptx
 
C tokens.pptx
C tokens.pptxC tokens.pptx
C tokens.pptx
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by Faixan
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
M.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C LanguageM.Florence Dayana / Basics of C Language
M.Florence Dayana / Basics of C Language
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Funa-C.ppt
Funa-C.pptFuna-C.ppt
Funa-C.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
Basics of C (1).ppt
Basics of C (1).pptBasics of C (1).ppt
Basics of C (1).ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
C programming basic pdf.ppt
C programming basic pdf.pptC programming basic pdf.ppt
C programming basic pdf.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 

Mais de yarkhosh

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If conditionyarkhosh
 
Math Functions in C Scanf Printf
Math Functions in C Scanf PrintfMath Functions in C Scanf Printf
Math Functions in C Scanf Printfyarkhosh
 
Operators in C
Operators in COperators in C
Operators in Cyarkhosh
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variablesyarkhosh
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programmingyarkhosh
 

Mais de yarkhosh (6)

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Math Functions in C Scanf Printf
Math Functions in C Scanf PrintfMath Functions in C Scanf Printf
Math Functions in C Scanf Printf
 
Operators in C
Operators in COperators in C
Operators in C
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programming
 

Último

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 

Último (20)

SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 

Data Types in C

  • 1. Balochistan University of IT & MS Lecture 3 Data Types in C short, int, long, float, double, char CS106 Introduction to Programming Methodologies & Abstraction Fall 2005 Balochistan University of I.T & M.S Faculty of System Sciences Sadique Ahmed Bugti
  • 2. Balochistan University of IT & MS The Data Type int • int is short for integer • Integers are whole numbers, which have no fractional part, or decimal point. – They can be both positive and negative. – Unsigned integers (positive values only) are supported. – In addition, there are short and long integers. • Examples of integers include: 0 57 4567 128933
  • 3. Balochistan University of IT & MS The Data Type int • The keyword used to define integers is: int • Integer variables are declared using: int variable_name; • For example: int a; • declares that you want to create an int variable called a
  • 4. Balochistan University of IT & MS The Integral Types • int is considered the “natural” type for working with integers. – Other integral types such as char, short and long are intended for specialized use.
  • 5. Balochistan University of IT & MS long int • Sometimes extra digits are needed to store numbers larger than those allowed in a normal int. long int variable_name; • The long qualifier informs C that extra storage is required for the integer. • Long integers are normally in the range: -231… 231-1 or -2147483648...2147483647
  • 6. Balochistan University of IT & MS short int • Sometimes less digits are needed to store a number: short int variable_name; • The short qualifier informs C that less storage is required for the integer. • Short integers are normally in the range: -215… 215-1 or -32768..32767
  • 7. Balochistan University of IT & MS unsigned integers • Unsigned integers contain positive values only – An integer with n bits uses n bits for the number – An integer with n bits → 2n integers in the range [0,2n-1] e.g. if n=16 → 65536 integers, 0..65535 • Unsigned integers are declared: unsigned int variable_name; unsigned short variable_name; unsigned long variable_name;
  • 8. Balochistan University of IT & MS signed integers • Signed integers allow both positive and negative values: – An integer with n bits uses n-1 bits for the number and 1 bit for the sign • (+ve = 0, -ve = 1) – 2n integers in the range [-(2n-1),(2n-1-1)] e.g. n=16 → 65536 integers, -322768..32767
  • 9. Balochistan University of IT & MS signed integers • All int declarations default to signed. • Signed integers are declared: signed int variable_name; which is equivalent to: int variable_name;
  • 10. Balochistan University of IT & MS Integers in C On a 16-bit System Type Approximate Range short -32768..32767 unsigned short 0..65535 int -32768..32767 unsigned int 0..65535 long -2147483648..2147483647 unsigned long 0..4294967295
  • 11. Balochistan University of IT & MS A note on ints • Suffixes can be appended to an integer constant to specify its type. – Can be used with int, long, or unsigned long Example Type Suffix u or U unsigned 37U l or L long 37L ul or UL unsigned long 37UL
  • 12. Balochistan University of IT & MS Integer Overflow int b = 2000000000, c =2000000000; a = b + c; /* out of range */ • At run-time the variable a may be assigned an incorrect value. The logical value of the • expression b+c is 4 billion, which is greater • than the maximum value which can be stored in a variable of type int. • This is known as integer overflow.
  • 13. Balochistan University of IT & MS Integer Overflow • How do we fix this problem? • What if we declare: long a, b = 2000000000, c = 3000000000; – Does this work? • What about trying: unsigned long a, b = 2000000000, c = 2000000000;
  • 14. Balochistan University of IT & MS Floating Point Numbers • A real or floating-point number has an integral part and a fractional part which are separated by a decimal point. e.g. 0.01 70.456 3e+5 .5067e-16 3.14159 • Floating-point numbers can be either positive or negative – Floating-point numbers in C are always signed • ANSI C provides three floating types: 1. float 2. double 3. long double.
  • 15. Balochistan University of IT & MS Floating Point Numbers • Exponential (scientific) notation can be used to represent real numbers: e ± exponent 123000.0 = 1.23x105 = 1.23e5 = 1.23E5 • e or E is read as “times 10 to the power” e.g. 0.34e-4 = 0.000034 2500 = 25e2 e.g. 333.7777e-22 Integer Fraction Exponent 333 7777 e-22
  • 16. Balochistan University of IT & MS Floating Point Numbers Examples Number Scientific Notation Exponential Notation 1000000000 1.0x109 1.0e9 123000 1.23x105 1.23e5 322.56 3.2256x102 3.2256e2 0.000056 5.6x10-5 5.6e-5
  • 17. Balochistan University of IT & MS A note on floats • A suffix can be appended to a floating constant to specify its type: Suffix Type Example F f or F float 3.7 l or L long double 3.7L • The working floating type in C is double, not float
  • 18. Balochistan University of IT & MS float • The type float denotes normal precision real numbers. • Single precision floating-point numbers are declared using: float variable_name;
  • 19. Balochistan University of IT & MS double • The type double denotes double precision real-numbers. • Double precision floating-point numbers are declared using: double variable_name; • The qualifier long double denotes extended precision. long double variable_name;
  • 20. Balochistan University of IT & MS Examples of Floating-Point Numbers • The following are examples of valid floating point numbers in C: 3.14159 /*of type double*/ 314.159e-2F /*of type float*/ 0e0 /*equivalent to 0.0*/ 1. *equivalent to 1.0*/
  • 21. Balochistan University of IT & MS Examples of Floating-Point Numbers • The following are examples of invalid floating point numbers in C: 3.14,159 /*comma not allowed*/ 314159 /*decimal point or exponent needed*/ .e0 /*integral or fractional part needed*/
  • 22. Balochistan University of IT & MS Floating Point Types in C • The possible values that a floating type can be assigned are described in terms of attributes called precision and range. – Precision describes the number of significant decimal places that a floating value carries. – The range describes the limits of the largest and smallest positive floating point values that can be represented in a variable of that type. – e.g. a float has precision of six significant figures
  • 23. Balochistan University of IT & MS Floating Point Types in C On a 16-bit System • For example: x=123.456789 /*9 significant digits*/ results in x being assigned the value: 0.123456x103 Type Approximate Range Significant Digits float 10-37..1038 6 double 10-307..10-308 15 long double 10-4931..10-4932 17
  • 24. Balochistan University of IT & MS Floating Point • How small is 10-37? • The mass of one electron is approximately 10-27 grams, 10-37 is one ten-billionth of 10-27 • How large is 1037? • If you multiply the diameter of the Milky Way galaxy in kilometers by a trillion (1,000,000,000,000), the result is just one ten-thousandth of 1037
  • 25. Balochistan University of IT & MS char • The type char represents single characters. – A very short integer • Characters are enclosed in single quotes ( ‘ ). – ‘A’, ‘a’, and ‘!’ are character constants • Characters are declared using the statement: char variable_name;
  • 26. Balochistan University of IT & MS char • An example of a character value is the letter A. An example of declaring a character variable called letter is, char letter; letter = 'A'; • Note the assignment of the character A to the variable letter is done by enclosing the value in single quotes.
  • 27. Balochistan University of IT & MS char • The backslash character ( ) is called the escape character. – It is used to signal that a special character follows. – For example ’ can be used to assign an apostrophe to a character variable. • Note: – While characters are enclosed in single quotes ( ‘ ), a different type, the string, is enclosed in double quotes ( “ ).
  • 28. Balochistan University of IT & MS Character Types in C On a 16-bit System Type Approximate Range char (signed) -128..127 unsigned char 0..255
  • 29. Balochistan University of IT & MS char – special characters Character Name Meaning b Backspace Move the cursor to the left by one character n New line Go to next line r Return Go to beginning of current Line t Tab Advance to next tab stop ’ Apostrophe Character ‘ ” Double quote Character “ Backslash Character
  • 30. Balochistan University of IT & MS Couldn’t a double be used for all numbers? • Is having more than one numeric type necessary? – int versus double • Operations involving integers are faster • Less storage space is needed • Operations with integers are always precise • There is some loss of accuracy with floating- point numbers
  • 31. Balochistan University of IT & MS Couldn’t a double be used for all numbers? • With doubles, a larger range of numbers can be represented as compared to type int. • For example: – An 16-bit grayscale image of size 1024 x 2048 contains 2,097,152 pixels • int @ 2 bytes per pixel = 4,096kb = 4mb • double @ 8 bytes per pixel = 16mb – If the image were color (Red, Green, Blue), storage would be increased threefold. • int → 12mb • double → 48mb
  • 32. Balochistan University of IT & MS Type Qualifiers • The keyword const is called a type qualifier because it is used in declarations to tell the compiler how identifiers can be used
  • 33. Balochistan University of IT & MS const • Sometimes you want to use a value that does not change, such as π. const float PI=3.1415927; • The general form of a constant declaration is: const type variable = value; • Constants must be initialized at declaration time and can never be changed.
  • 34. Balochistan University of IT & MS const • Note: – It is usual to initialize a const with a value as it cannot get a value any other way. • Examples of constants: const float PI = 3.1415927; const int TRUE = 1; const char letter = 'a‘;
  • 35. Balochistan University of IT & MS Note • The standard header file limits.h defines constants for the various integer numerical limits. eg: CHAR_MIN, CHAR_MAX,INT_MAX etc. • The standard header file float.h defines constants for the various floating-point numerical limits. eg: FLT_MIN, FLT_MAX, DBL_MIN etc.
  • 36. Balochistan University of IT & MS Overflow Error • The following will work on a Unix system, but will fail on a PC: int zip; … zip = 92126; • Why does this fail? – On Unix systems, integers are 32 bits, providing a range of –231.. 231-1. – On the PC, integers are 16 bits , so the range is 215.. 215-1
  • 37. Balochistan University of IT & MS The End