SlideShare a Scribd company logo
1 of 22
A Closer Look at Data
Types, Variables and Expressions
BY
MD.INAN MASHRUR.
DATA TYPES
DataTypes
IntegerTypes
CharacterTypes
FloatingTypes
Unsigned int
long int
char
long double
doublefloatShort int
Integer Types
• Values of an integer type are whole numbers.
• The int type is usually 32 bits, but may be 16 bits on older CPUs.
• Long integers may have more bits than ordinary integers; short integers may have fewer bits.
• The specifiers long and short, as well as signed and unsigned, can be combined with int to form
integer types.
• Only six combinations produce different types:
short int int long int
unsigned int unsigned short int unsigned long int
• The order of the specifiers doesn’t matter. Also, the word int can be dropped (long int can be
abbreviated to just long).
Signed & Unsigned Integer
• The integer types, in turn, are divided into two categories: signed and unsigned.
• The leftmost bit of a signed integer (known as the sign bit) is 0 if the number is
positive or zero, 1 if it’s negative.
• An integer with no sign bit (the leftmost bit is considered part of the number’s
magnitude) is said to be unsigned.
• By default, integer variables are signed in C—the leftmost bit is reserved for the sign.
• To tell the compiler that a variable has no sign bit, declare it to be unsigned.
• Unsigned numbers are primarily useful for systems programming and low-level,
machine-dependent applications.
Integer Types Defined By ANSI C
Standard
Type
Int
Unsigned int
Signed int
Short int
Unsigned short int
Signed short int
Long int
Signed long int
Unsigned long int
Long long int
Unsigned long long -
int
Typical Size in Bits
16 or 32
16 or 32
16 or 32
16
16
16
32
32
32
64
64
Minimal Range
-32,767 to 32,767
0 to 65,535
Same as int
Same as int
0 to 65,535
Same as short int
-2,147,483,647 to 2,147,483,647
Same as long int
0 to 4,294,967,295
-263 to 263 – 1
0 to 264 – 1
Floating Types
• C provides three floating types, corresponding to different floating-point
formats:
• float Single-precision floating-point
• double Double-precision floating-point
• long double Extended-precision floating-point
• float is suitable when the amount of precision isn’t critical.
• double provides enough precision for most programs.
• long double is rarely used.
• The C standard doesn’t state how much precision the float, double,
and long double types provide, since that depends on how numbers
are stored.
Floating Types Defined By ANSI C
Standard
Type
Float
Double
Long double
Typical size in Bits
32
64
80
Minimal Range
Six digits of precision
Ten digits of precision
Ten digits of precision
Character Types
• The only remaining basic type is char, the character type.
• Today’s most popular character set is ASCII (American
StandardCode for Information Interchange), a 7-bit code
capable of representing 128 characters.
• ASCII is often extended to a 256-character code known as
Latin-1 that provides the characters necessary forWestern
European and many African languages.
Operations on Characters
• When a character appears in a computation, C uses its
integer value.
• Consider the following examples, which assume the ASCII
character set:
char ch;
int i;
i = 'a'; /* i is now 97 */
ch = 65; /* ch is now 'A' */
ch = ch + 1; /* ch is now 'B' */
ch++; /* ch is now 'C' */
• Characters can be compared, just as numbers can.
• An if statement that converts a lower-case letter to upper case:
if (ch >= ‘a’ && ch <= 'z')
ch = ch - 'a' + 'A';
• Comparisons such as ch>=‘a’ are done using the integer values of
the characters involved.The fact that characters have the same
properties as numbers has advantages.
• For example, it is easy to write a for statement whose control variable
steps through all the upper-case letters:
for (ch = 'A'; ch <= 'Z'; ch++)…
Character Types Defined By ANSI C
Standard
Type
Char
Unsigned char
Signed char
Typical Size in Bits
8
8
8
Minimal Range
-127 to 127
0 to 255
-127 to 127
Format Specifiers for Different Data Types
DataType
Unsigned
Short
Int
Long int
Long long int
Float
Double
Long double
Char
Format Specifiers
%u
%h
%d
%ld
%lld
%f
%lf
%Lf
%c
Declaring Variables
Variables
Local Global
LocalVariables: Local variables will be declared inside a function . Different functions
can have variables with the same name as local variables are not known
outside their own function.
GlobalVariables: Global variables will be declared outside of all functions. It is known
by all functions.
A variables is a named memory location that can hold various values.
Example:
#include<stdio.h>
void f1(void);
int max; /*This is GlobalVariable*/
int main()
{ max = 10; f1(); return 0;}
void f1(void)
{ int i ; /*This is LocalVariable*/
for ( i=0; i<max; i++) printf(“%d”,i ) ; }
Constants
Constants refer to fixed values that may not be altered by the
program.
Integer type constant : Integer type constants are specified as
numbers without fractional component . Ex: int i = 10 ;
Floating type constant : Floating type constants are specified as
numbers with fractional component . Ex: int i= 10.07 ;
Character type constant : Character type constants are enclosed
between single quotes . Ex: char ch = ‘A’ ;
String type constant : A string is a set of characters enclosed by
double quotes. Ex: char ch = “Bangladesh” ;
INITIALIZE VARIABLES
A variable may be given an initial value when it is
declared.This is called variable initialization.
Examples:
1. int i = 10 ;
2. int I = 10 .07;
3. char ch = ‘A’ ;
4. char ch = “Bangladesh” ;
Type Conversations In
Expressions
• C allows the mixing of types within an expressions
because it has a strict set of conversion rules that dictate
how type differences are resolved.
Integral promotion : In C, whenever a char or short
int is used in an expression , its value
is automatically elevated to int
during the evaluation of that
expression .
Type Promotion : After automatic integral promotions have been applied,
the C compiler will convert all operands “up” to the type of the largest
operand. It is done on an operation-by-operation basis, as described
below:
if an operand is a long double
then the second is converted to long double
else if an operand is a double
then the second is converted to double
else if an operand is a float
then the second is converted to float
else if an operand is a unsigned long
then the second is converted to unsigned long
else if an operand is a long
then the second is converted to long
else if an operand is a unsigned
then the second is converted to unsigned
Type Conversations In
Assignments
In an assignment statement, in C, when the type of the left side is
larger than the type of the right side, the process cause no
problem. In opposite case, data loss may occur.
For Example :
Going from long long to long : 32 bits data will be lost
Going from long to int : 16 bits data will be lost
Going from int to char : 8 bits data will be lost
Going from double to int : fractional part will be lost
Going from long double to double : precision will be lost
Going from double to float : precision will be lost
Type Casting
Sometimes we need to transform the type of the variable
temporarily. Type cast causes a temporary type change.
General form of type cast:
(type) value
For example:
float f ;
f = 100.2 ;
printf(“%d”,(int) f ) ; /*print f as an integer*/
Here, the type cast causes the value of f (float) to be
converted to an integer
• Reference:
TeachYourself C by Herbert Schildt.
The End
THANKS FOR YOUR ATTENTION

More Related Content

What's hot

Data types in c language
Data types in c languageData types in c language
Data types in c languageHarihamShiwani
 
Data Types in C
Data Types in CData Types in C
Data Types in Cyarkhosh
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1patcha535
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++Abdul Hafeez
 
Data types in C language
Data types in C languageData types in C language
Data types in C languagekashyap399
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answerVasuki Ramasamy
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programmingRumman Ansari
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by FaixanٖFaiXy :)
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingSaranyaK68
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
C programming language character set keywords constants variables data types
C programming language character set keywords constants variables data typesC programming language character set keywords constants variables data types
C programming language character set keywords constants variables data typesSourav Ganguly
 
Lec 08. C Data Types
Lec 08. C Data TypesLec 08. C Data Types
Lec 08. C Data TypesRushdi Shams
 

What's hot (20)

Data types in c language
Data types in c languageData types in c language
Data types in c language
 
Data Types in C
Data Types in CData Types in C
Data Types in C
 
Anti-Patterns
Anti-PatternsAnti-Patterns
Anti-Patterns
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1
 
JAVA Literals
JAVA LiteralsJAVA Literals
JAVA Literals
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
2 1 data
2 1  data2 1  data
2 1 data
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
Data types slides by Faixan
Data types slides by FaixanData types slides by Faixan
Data types slides by Faixan
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C++ data types
C++ data typesC++ data types
C++ data types
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
C programming language character set keywords constants variables data types
C programming language character set keywords constants variables data typesC programming language character set keywords constants variables data types
C programming language character set keywords constants variables data types
 
Lec 08. C Data Types
Lec 08. C Data TypesLec 08. C Data Types
Lec 08. C Data Types
 

Similar to A Closer Look at Data Types, Variables and Expressions

Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data typesPratik Devmurari
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introductionnikshaikh786
 
C Sharp Jn (1)
C Sharp Jn (1)C Sharp Jn (1)
C Sharp Jn (1)jahanullah
 
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
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiSowmyaJyothi3
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxjoachimbenedicttulau
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C ProgrammingQazi Shahzad Ali
 
Unit 1 Built in Data types in C language.ppt
Unit 1 Built in Data types in C language.pptUnit 1 Built in Data types in C language.ppt
Unit 1 Built in Data types in C language.pptpubgnewstate1620
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
CONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN CCONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN CSahithi Naraparaju
 
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 variableseShikshak
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfYRABHI
 
lecture 3 bca 1 year.pptx
lecture 3 bca 1 year.pptxlecture 3 bca 1 year.pptx
lecture 3 bca 1 year.pptxclassall
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageSURAJ KUMAR
 

Similar to A Closer Look at Data Types, Variables and Expressions (20)

Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Module 1:Introduction
Module 1:IntroductionModule 1:Introduction
Module 1:Introduction
 
Ch7 Basic Types
Ch7 Basic TypesCh7 Basic Types
Ch7 Basic Types
 
C Sharp Jn (1)
C Sharp Jn (1)C Sharp Jn (1)
C Sharp Jn (1)
 
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
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
LESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptxLESSON1-C_programming (1).GRADE 8 LESSONpptx
LESSON1-C_programming (1).GRADE 8 LESSONpptx
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Unit 1 Built in Data types in C language.ppt
Unit 1 Built in Data types in C language.pptUnit 1 Built in Data types in C language.ppt
Unit 1 Built in Data types in C language.ppt
 
Data Handling
Data HandlingData Handling
Data Handling
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
CONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN CCONSTANTS, VARIABLES & DATATYPES IN C
CONSTANTS, VARIABLES & DATATYPES IN C
 
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
 
[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
All C ppt.ppt
All C ppt.pptAll C ppt.ppt
All C ppt.ppt
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 
FUNDAMENTAL OF C
FUNDAMENTAL OF CFUNDAMENTAL OF C
FUNDAMENTAL OF C
 
lecture 3 bca 1 year.pptx
lecture 3 bca 1 year.pptxlecture 3 bca 1 year.pptx
lecture 3 bca 1 year.pptx
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming Language
 

Recently uploaded

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
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.
 
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
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
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
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
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
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
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
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
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
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
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...
 
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
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
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...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
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
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
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
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
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
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

A Closer Look at Data Types, Variables and Expressions

  • 1. A Closer Look at Data Types, Variables and Expressions BY MD.INAN MASHRUR.
  • 3. Integer Types • Values of an integer type are whole numbers. • The int type is usually 32 bits, but may be 16 bits on older CPUs. • Long integers may have more bits than ordinary integers; short integers may have fewer bits. • The specifiers long and short, as well as signed and unsigned, can be combined with int to form integer types. • Only six combinations produce different types: short int int long int unsigned int unsigned short int unsigned long int • The order of the specifiers doesn’t matter. Also, the word int can be dropped (long int can be abbreviated to just long).
  • 4. Signed & Unsigned Integer • The integer types, in turn, are divided into two categories: signed and unsigned. • The leftmost bit of a signed integer (known as the sign bit) is 0 if the number is positive or zero, 1 if it’s negative. • An integer with no sign bit (the leftmost bit is considered part of the number’s magnitude) is said to be unsigned. • By default, integer variables are signed in C—the leftmost bit is reserved for the sign. • To tell the compiler that a variable has no sign bit, declare it to be unsigned. • Unsigned numbers are primarily useful for systems programming and low-level, machine-dependent applications.
  • 5. Integer Types Defined By ANSI C Standard Type Int Unsigned int Signed int Short int Unsigned short int Signed short int Long int Signed long int Unsigned long int Long long int Unsigned long long - int Typical Size in Bits 16 or 32 16 or 32 16 or 32 16 16 16 32 32 32 64 64 Minimal Range -32,767 to 32,767 0 to 65,535 Same as int Same as int 0 to 65,535 Same as short int -2,147,483,647 to 2,147,483,647 Same as long int 0 to 4,294,967,295 -263 to 263 – 1 0 to 264 – 1
  • 6. Floating Types • C provides three floating types, corresponding to different floating-point formats: • float Single-precision floating-point • double Double-precision floating-point • long double Extended-precision floating-point • float is suitable when the amount of precision isn’t critical. • double provides enough precision for most programs. • long double is rarely used. • The C standard doesn’t state how much precision the float, double, and long double types provide, since that depends on how numbers are stored.
  • 7. Floating Types Defined By ANSI C Standard Type Float Double Long double Typical size in Bits 32 64 80 Minimal Range Six digits of precision Ten digits of precision Ten digits of precision
  • 8. Character Types • The only remaining basic type is char, the character type. • Today’s most popular character set is ASCII (American StandardCode for Information Interchange), a 7-bit code capable of representing 128 characters. • ASCII is often extended to a 256-character code known as Latin-1 that provides the characters necessary forWestern European and many African languages.
  • 9. Operations on Characters • When a character appears in a computation, C uses its integer value. • Consider the following examples, which assume the ASCII character set: char ch; int i; i = 'a'; /* i is now 97 */ ch = 65; /* ch is now 'A' */ ch = ch + 1; /* ch is now 'B' */ ch++; /* ch is now 'C' */
  • 10. • Characters can be compared, just as numbers can. • An if statement that converts a lower-case letter to upper case: if (ch >= ‘a’ && ch <= 'z') ch = ch - 'a' + 'A'; • Comparisons such as ch>=‘a’ are done using the integer values of the characters involved.The fact that characters have the same properties as numbers has advantages. • For example, it is easy to write a for statement whose control variable steps through all the upper-case letters: for (ch = 'A'; ch <= 'Z'; ch++)…
  • 11. Character Types Defined By ANSI C Standard Type Char Unsigned char Signed char Typical Size in Bits 8 8 8 Minimal Range -127 to 127 0 to 255 -127 to 127
  • 12. Format Specifiers for Different Data Types DataType Unsigned Short Int Long int Long long int Float Double Long double Char Format Specifiers %u %h %d %ld %lld %f %lf %Lf %c
  • 13. Declaring Variables Variables Local Global LocalVariables: Local variables will be declared inside a function . Different functions can have variables with the same name as local variables are not known outside their own function. GlobalVariables: Global variables will be declared outside of all functions. It is known by all functions. A variables is a named memory location that can hold various values.
  • 14. Example: #include<stdio.h> void f1(void); int max; /*This is GlobalVariable*/ int main() { max = 10; f1(); return 0;} void f1(void) { int i ; /*This is LocalVariable*/ for ( i=0; i<max; i++) printf(“%d”,i ) ; }
  • 15. Constants Constants refer to fixed values that may not be altered by the program. Integer type constant : Integer type constants are specified as numbers without fractional component . Ex: int i = 10 ; Floating type constant : Floating type constants are specified as numbers with fractional component . Ex: int i= 10.07 ; Character type constant : Character type constants are enclosed between single quotes . Ex: char ch = ‘A’ ; String type constant : A string is a set of characters enclosed by double quotes. Ex: char ch = “Bangladesh” ;
  • 16. INITIALIZE VARIABLES A variable may be given an initial value when it is declared.This is called variable initialization. Examples: 1. int i = 10 ; 2. int I = 10 .07; 3. char ch = ‘A’ ; 4. char ch = “Bangladesh” ;
  • 17. Type Conversations In Expressions • C allows the mixing of types within an expressions because it has a strict set of conversion rules that dictate how type differences are resolved. Integral promotion : In C, whenever a char or short int is used in an expression , its value is automatically elevated to int during the evaluation of that expression .
  • 18. Type Promotion : After automatic integral promotions have been applied, the C compiler will convert all operands “up” to the type of the largest operand. It is done on an operation-by-operation basis, as described below: if an operand is a long double then the second is converted to long double else if an operand is a double then the second is converted to double else if an operand is a float then the second is converted to float else if an operand is a unsigned long then the second is converted to unsigned long else if an operand is a long then the second is converted to long else if an operand is a unsigned then the second is converted to unsigned
  • 19. Type Conversations In Assignments In an assignment statement, in C, when the type of the left side is larger than the type of the right side, the process cause no problem. In opposite case, data loss may occur. For Example : Going from long long to long : 32 bits data will be lost Going from long to int : 16 bits data will be lost Going from int to char : 8 bits data will be lost Going from double to int : fractional part will be lost Going from long double to double : precision will be lost Going from double to float : precision will be lost
  • 20. Type Casting Sometimes we need to transform the type of the variable temporarily. Type cast causes a temporary type change. General form of type cast: (type) value For example: float f ; f = 100.2 ; printf(“%d”,(int) f ) ; /*print f as an integer*/ Here, the type cast causes the value of f (float) to be converted to an integer
  • 21. • Reference: TeachYourself C by Herbert Schildt.
  • 22. The End THANKS FOR YOUR ATTENTION