SlideShare a Scribd company logo
Datatypes in C Intro to Programming
MUHAMMAD HAMMAD WASEEM 1
Data Types in C
As we studied earlier, the primary data types could be of three varieties—char, int, and float. It
may seem odd to many, how C programmers manage with such a tiny set of data types. Fact is, the C
programmers aren’t really deprived. They can derive many data types from these three types. In fact,
the number of data types that can be derived in C, is in principle, unlimited. A C programmer can always
invent whatever data type he needs. Not only this, the primary data types themselves could be of several
types. For example, a char could be an unsigned char or a signed char. Or an int could be a short int or
a long int.
Integers, long and short
C offers a variation of the integer data type that provides what are called short and long integer
values. The intention of providing these variations is to provide integers with different ranges wherever
possible. Though not a rule, short and long integers would usually occupy two and four bytes
respectively. Each compiler can decide appropriate sizes depending on the operating system and
hardware for which it is being written, subject to the following rules:
 shorts are at least 2 bytes big
 longs are at least 4 bytes big
 shorts are never bigger than ints
 ints are never bigger than longs
long integers cause the program to run a bit slower, but the range of values that we can use is
expanded tremendously. The value of a long integer typically can vary from -2147483648 to
+2147483647. long variables which hold long integers are declared using the keyword long, as:
long int i ;
long int abc ;
If there are such things as longs, symmetry requires shorts as well—integers that need less space
in memory and thus help speed up program execution. short integer variables are declared as:
short int j ;
short int height ;
C allows the abbreviation of short int to short and of long int to long. So the declarations made
above can be written as,
long i ; long abc ;
short j ; short height ;
Integers, signed and unsigned
Sometimes, we know in advance that the value stored in a given integer variable will always be
positive—when it is being used to only count things, for example. In such a case we can declare the
variable to be unsigned, as in,
unsigned int num_students ;
With such a declaration, the range of permissible integer values (for a 16-bit OS) will shift from
the range -32768 to +32767 to the range 0 to 65535. Thus, declaring an integer as unsigned almost
Datatypes in C Intro to Programming
MUHAMMAD HAMMAD WASEEM 2
doubles the size of the largest possible value that it can otherwise take. This so happens because on
declaring the integer as unsigned, the left-most bit is now free and is not used to store the sign of the
number. Note that an unsigned integer still occupies two bytes. This is how an unsigned integer can be
declared:
unsigned int i ;
unsigned i ;
Like an unsigned int, there also exists a short unsigned int and a long unsigned int. By default a
short int is a signed short int and a long int is a signed long int.
Chars, signed and unsigned
Parallel to signed and unsigned ints (either short or long), similarly there also exist signed and
unsigned chars, both occupying one byte each, but having different ranges. To begin with it might appear
strange as to how a char can have a sign. Consider the statement
char ch = 'A' ;
Here what gets stored in ch is the binary equivalent of the ASCII value of ‘A’ (i.e. binary of 65).
And if 65’s binary can be stored, then -54’s binary can also be stored (in a signed char).
A signed char is same as an ordinary char and has a range from -128 to +127; whereas, an
unsigned char has a range from 0 to 255. Let us now see a program that illustrates this range:
main( )
{
char ch = 291 ;
printf ( "n%d %c", ch, ch ) ;
}
What output do you expect from this program? Possibly, 291 and the character corresponding to
it. Well, not really. Surprised? The reason is that ch has been defined as a char, and a char cannot take a
value bigger than +127. Hence when value of ch exceeds +127, an appropriate value from the other side
of the range is picked up and stored in ch. This value in our case happens to be 35, hence 35 and its
corresponding character #, gets printed out.
Floats and Doubles
A float occupies 4 bytes in memory and can range from -3.4e38 to +3.4e38. If this is insufficient
then C offers a double data type that occupies 8 bytes in memory and has a range from -1.7e308 to
+1.7e308. A variable of type double can be declared as,
double a, population ;
If the situation demands usage of real numbers that lie even beyond the range offered by double
data type, then there exists a long double that can range from -1.7e4932 to +1.7e4932. A long double
occupies 10 bytes in memory.
You would see that most of the times in C programming one is required to use either chars or
ints and cases where floats, doubles or long doubles would be used are indeed rare.
The essence of all the data types that we have learnt so far has been captured in:
Datatypes in C Intro to Programming
MUHAMMAD HAMMAD WASEEM 3

More Related Content

PDF
[ITP - Lecture 04] Variables and Constants in C/C++
PPT
PPTX
FUNDAMENTAL OF C
PDF
Strings in c mrs.sowmya jyothi
PPTX
Unit 8. Pointers
PPTX
Data Type in C Programming
PPTX
Computer programming(CP)
PDF
Pointers In C
[ITP - Lecture 04] Variables and Constants in C/C++
FUNDAMENTAL OF C
Strings in c mrs.sowmya jyothi
Unit 8. Pointers
Data Type in C Programming
Computer programming(CP)
Pointers In C

What's hot (19)

PDF
Pointers
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Strings
PPT
Token and operators
PPTX
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
PPS
C programming session 04
PPT
Getting started with c++
PPTX
Pointers in c v5 12102017 1
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Arrays in c++
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
COM1407: Variables and Data Types
PDF
Programming with matlab session 4
PDF
Programming with matlab session 3 notes
PDF
C programming | Class 8 | III Term
PPT
Data type in c
DOCX
Data types and operators in vb
PPT
Introduction To Algorithm [2]
Pointers
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
Strings
Token and operators
Data types in java | What is Datatypes in Java | Learning with RD | Created b...
C programming session 04
Getting started with c++
Pointers in c v5 12102017 1
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Arrays in c++
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
COM1407: Variables and Data Types
Programming with matlab session 4
Programming with matlab session 3 notes
C programming | Class 8 | III Term
Data type in c
Data types and operators in vb
Introduction To Algorithm [2]
Ad

Similar to [ITP - Lecture 05] Datatypes (20)

PPTX
Data types slides by Faixan
DOC
Data type
DOCX
c++ best code.docxhsdsdcvcdcdvdvdvdvdcdv
PPTX
2. Variables and Data Types in C++ proramming.pptx
PPT
C++ data types
PPT
Structured Programming with C - Data Types.ppt
PPTX
A Closer Look at Data Types, Variables and Expressions
PDF
About size_t and ptrdiff_t
PPTX
Data types
PPT
FP 201 Unit 2 - Part 2
PPTX
Constant, variables, data types
PPT
Lec 08. C Data Types
PPT
Datatypes
PPTX
Structure of c_program_to_input_output
PDF
variablesfinal-170820055428 data type results
PPTX
Variables in C++, data types in c++
PPTX
Presentation 2nd
PDF
2 expressions (ppt-2) in C++
PPT
programming week 2.ppt
PPTX
PPS_unit_2_gtu_sem_2_year_2023_GTUU.pptx
Data types slides by Faixan
Data type
c++ best code.docxhsdsdcvcdcdvdvdvdvdcdv
2. Variables and Data Types in C++ proramming.pptx
C++ data types
Structured Programming with C - Data Types.ppt
A Closer Look at Data Types, Variables and Expressions
About size_t and ptrdiff_t
Data types
FP 201 Unit 2 - Part 2
Constant, variables, data types
Lec 08. C Data Types
Datatypes
Structure of c_program_to_input_output
variablesfinal-170820055428 data type results
Variables in C++, data types in c++
Presentation 2nd
2 expressions (ppt-2) in C++
programming week 2.ppt
PPS_unit_2_gtu_sem_2_year_2023_GTUU.pptx
Ad

More from Muhammad Hammad Waseem (20)

PDF
[ITP - Lecture 17] Strings in C/C++
PDF
[ITP - Lecture 16] Structures in C/C++
PDF
[ITP - Lecture 15] Arrays & its Types
PDF
[ITP - Lecture 14] Recursion
PDF
[ITP - Lecture 13] Introduction to Pointers
PDF
[ITP - Lecture 12] Functions in C/C++
PDF
[ITP - Lecture 11] Loops in C/C++
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
PDF
[ITP - Lecture 09] Conditional Operator in C/C++
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
PDF
[ITP - Lecture 07] Comments in C/C++
PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
PDF
[ITP - Lecture 03] Introduction to C/C++
PDF
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
PDF
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
PPTX
[OOP - Lec 20,21] Inheritance
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
[OOP - Lec 18] Static Data Member
PPTX
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 14] Recursion
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[OOP - Lec 20,21] Inheritance
[OOP - Lec 19] Static Member Functions
[OOP - Lec 18] Static Data Member
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 13,14,15] Constructors / Destructor and its Types

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Sports Quiz easy sports quiz sports quiz
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
GDM (1) (1).pptx small presentation for students
Complications of Minimal Access Surgery at WLH
Renaissance Architecture: A Journey from Faith to Humanism
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Final Presentation General Medicine 03-08-2024.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
TR - Agricultural Crops Production NC III.pdf
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

[ITP - Lecture 05] Datatypes

  • 1. Datatypes in C Intro to Programming MUHAMMAD HAMMAD WASEEM 1 Data Types in C As we studied earlier, the primary data types could be of three varieties—char, int, and float. It may seem odd to many, how C programmers manage with such a tiny set of data types. Fact is, the C programmers aren’t really deprived. They can derive many data types from these three types. In fact, the number of data types that can be derived in C, is in principle, unlimited. A C programmer can always invent whatever data type he needs. Not only this, the primary data types themselves could be of several types. For example, a char could be an unsigned char or a signed char. Or an int could be a short int or a long int. Integers, long and short C offers a variation of the integer data type that provides what are called short and long integer values. The intention of providing these variations is to provide integers with different ranges wherever possible. Though not a rule, short and long integers would usually occupy two and four bytes respectively. Each compiler can decide appropriate sizes depending on the operating system and hardware for which it is being written, subject to the following rules:  shorts are at least 2 bytes big  longs are at least 4 bytes big  shorts are never bigger than ints  ints are never bigger than longs long integers cause the program to run a bit slower, but the range of values that we can use is expanded tremendously. The value of a long integer typically can vary from -2147483648 to +2147483647. long variables which hold long integers are declared using the keyword long, as: long int i ; long int abc ; If there are such things as longs, symmetry requires shorts as well—integers that need less space in memory and thus help speed up program execution. short integer variables are declared as: short int j ; short int height ; C allows the abbreviation of short int to short and of long int to long. So the declarations made above can be written as, long i ; long abc ; short j ; short height ; Integers, signed and unsigned Sometimes, we know in advance that the value stored in a given integer variable will always be positive—when it is being used to only count things, for example. In such a case we can declare the variable to be unsigned, as in, unsigned int num_students ; With such a declaration, the range of permissible integer values (for a 16-bit OS) will shift from the range -32768 to +32767 to the range 0 to 65535. Thus, declaring an integer as unsigned almost
  • 2. Datatypes in C Intro to Programming MUHAMMAD HAMMAD WASEEM 2 doubles the size of the largest possible value that it can otherwise take. This so happens because on declaring the integer as unsigned, the left-most bit is now free and is not used to store the sign of the number. Note that an unsigned integer still occupies two bytes. This is how an unsigned integer can be declared: unsigned int i ; unsigned i ; Like an unsigned int, there also exists a short unsigned int and a long unsigned int. By default a short int is a signed short int and a long int is a signed long int. Chars, signed and unsigned Parallel to signed and unsigned ints (either short or long), similarly there also exist signed and unsigned chars, both occupying one byte each, but having different ranges. To begin with it might appear strange as to how a char can have a sign. Consider the statement char ch = 'A' ; Here what gets stored in ch is the binary equivalent of the ASCII value of ‘A’ (i.e. binary of 65). And if 65’s binary can be stored, then -54’s binary can also be stored (in a signed char). A signed char is same as an ordinary char and has a range from -128 to +127; whereas, an unsigned char has a range from 0 to 255. Let us now see a program that illustrates this range: main( ) { char ch = 291 ; printf ( "n%d %c", ch, ch ) ; } What output do you expect from this program? Possibly, 291 and the character corresponding to it. Well, not really. Surprised? The reason is that ch has been defined as a char, and a char cannot take a value bigger than +127. Hence when value of ch exceeds +127, an appropriate value from the other side of the range is picked up and stored in ch. This value in our case happens to be 35, hence 35 and its corresponding character #, gets printed out. Floats and Doubles A float occupies 4 bytes in memory and can range from -3.4e38 to +3.4e38. If this is insufficient then C offers a double data type that occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308. A variable of type double can be declared as, double a, population ; If the situation demands usage of real numbers that lie even beyond the range offered by double data type, then there exists a long double that can range from -1.7e4932 to +1.7e4932. A long double occupies 10 bytes in memory. You would see that most of the times in C programming one is required to use either chars or ints and cases where floats, doubles or long doubles would be used are indeed rare. The essence of all the data types that we have learnt so far has been captured in:
  • 3. Datatypes in C Intro to Programming MUHAMMAD HAMMAD WASEEM 3