SlideShare a Scribd company logo
Data types in C programming
Objectives
• Be able to explain to others what a data type is
• Be able to use basic data types in C programs
• Be able to see the inaccuracies and limitations introduced
by machine representations of numbers
CoxSimple Data Types2
What do you see?
CoxSimple Data Types3
What do you see?
CoxSimple Data Types4
Last one
CoxSimple Data Types5
CoxSimple Data Types6
Everything is Just a Bunch of Bits
• Bits can represent many different things
• Depends on interpretation
• You and your program must keep track of what kind of data
is at each location in the computer’s memory
• E.g., program data types
CoxSimple Data Types7
Big Picture
• Processor works with finite-sized data
• All data implemented as a sequence of bits
• Bit = 0 or 1
• Represents the level of an electrical charge
• Byte = 8 bits
• Word = largest data size handled by processor
• 32 bits on most older computers
• 64 bits on most new computers
CoxSimple Data Types8
Data types in C
•Only really four basic types:
• char
• int (short, long, long long, unsigned)
• float
• double
•Size of these types on
•CLEAR machines:
•Sizes of these types
•vary from one machine
•to another!
Type Size (bytes)
char 1
int 4
short 2
long 8
long long 8
float 4
double 8
CoxSimple Data Types9
Characters (char)
• Roman alphabet, punctuation, digits, and other symbols:
• Encoded within one byte (256 possible symbols)
• ASCII encoding (man ascii for details)
• In C:
char a_char = ’a’;
char newline_char = ’n’;
char tab_char = ’t’;
char backslash_char = ’’;
CoxSimple Data Types10
ASCII
From “man ascii”:
| 0 NUL| 1 SOH| 2 STX| 3 ETX| 4 EOT| 5 ENQ| 6 ACK| 7 BEL|
| 8 BS | 9 HT | 10 NL | 11 VT | 12 NP | 13 CR | 14 SO | 15 SI |
| 16 DLE| 17 DC1| 18 DC2| 19 DC3| 20 DC4| 21 NAK| 22 SYN| 23 ETB|
| 24 CAN| 25 EM | 26 SUB| 27 ESC| 28 FS | 29 GS | 30 RS | 31 US |
| 32 SP | 33 ! | 34 " | 35 # | 36 $ | 37 % | 38 & | 39 ' |
| 40 ( | 41 ) | 42 * | 43 + | 44 , | 45 - | 46 . | 47 / |
| 48 0 | 49 1 | 50 2 | 51 3 | 52 4 | 53 5 | 54 6 | 55 7 |
| 56 8 | 57 9 | 58 : | 59 ; | 60 < | 61 = | 62 > | 63 ? |
| 64 @ | 65 A | 66 B | 67 C | 68 D | 69 E | 70 F | 71 G |
| 72 H | 73 I | 74 J | 75 K | 76 L | 77 M | 78 N | 79 O |
| 80 P | 81 Q | 82 R | 83 S | 84 T | 85 U | 86 V | 87 W |
| 88 X | 89 Y | 90 Z | 91 [ | 92  | 93 ] | 94 ^ | 95 _ |
| 96 ` | 97 a | 98 b | 99 c |100 d |101 e |102 f |103 g |
|104 h |105 i |106 j |107 k |108 l |109 m |110 n |111 o |
|112 p |113 q |114 r |115 s |116 t |117 u |118 v |119 w |
|120 x |121 y |122 z |123 { |124 | |125 } |126 ~ |127 DEL|
Special
control
characters
CoxSimple Data Types11
Characters are just numbers
•What does this function do?
Char
fun(char c)
{
char new_c;
if ((c >= ’A’) && (c <= ’Z’))
new_c = c - ’A’ + ’a’;
else
new_c = c;
return (new_c);
}
return type
procedure
name
argument type
and name
local variable
type and name
Math on
characters!
comparisons
with characters!
CoxSimple Data Types12
FP vs. Integer Results
int i = 20 / 3;
float f = 20.0 / 3.0;
True mathematical answer: 20  3 = 6 2/3
i = ?
f = ?
6
6.666667
Integer division ignores remainder
FP arithmetic rounds result
CoxSimple Data Types13
Integer Representations
Base 2 Base 16 Unsigned 2’s Comp.
0000 0 0 0
0001 1 1 1
0010 2 2 2
0011 3 3 3
0100 4 4 4
0101 5 5 5
0110 6 6 6
0111 7 7 7
1000 8 8 -8
1001 9 9 -7
1010 A 10 -6
1011 B 11 -5
1100 C 12 -4
1101 D 13 -3
1110 E 14 -2
1111 F 15 -1
0000 0001
0010
0011
0100
0101
0110
011110001001
1011
1100
1101
1010
1110
1111
0
1
2
3
4
5
6
789
A
D
C
B
E
F
0 1
2
3
4
5
6
789
10
11
12
13
14
15
0 1
-8
3
4
5
6
7
2
-7
-6
-5
-4
-3
-2
-1
Why one more negative than positive?
CoxSimple Data Types14
Integer Representations
Math for n bits:
Define




1
0
2)(2
n
i
i
i
xxUB







2
0
1
1
22)(2
n
i
i
i
n
n
xxxTB

01 xxx n 


sign bit
0=non-negative
1=negative
Base 2 Base 16 Unsigned 2’s Comp.
0000 0 0 0
0001 1 1 1
0010 2 2 2
0011 3 3 3
0100 4 4 4
0101 5 5 5
0110 6 6 6
0111 7 7 7
1000 8 8 -8
1001 9 9 -7
1010 A 10 -6
1011 B 11 -5
1100 C 12 -4
1101 D 13 -3
1110 E 14 -2
1111 F 15 -1
15
Memory Layout of Records
initials
(2 bytes)
married
(1 byte)
ss_number
(4 bytes)
• Fields are stored adjacently in memory.
• Memory is allocated for records based on
the order the fields are created.
• Variables are aligned for easy reference.
initials
(2 bytes)
ss_number
(4 bytes)
married
(1 byte)
Optimized for space Optimized for memory alignment
4 bytes / 32 bits 4 bytes / 32 bits
Data types in C programming

More Related Content

PDF
ĐỀ THI SEAMO - CẤP ĐỘ C (KHỐI 5 + 6) NĂM 2018
PDF
Construx Record Sheet
PDF
ĐỀ THI SEAMO - CẤP ĐỘ B (KHỐI 3 + 4) NĂM 2018
PDF
ĐỀ THI SEAMO - CẤP ĐỘ A (KHỐI 1 + 2) NĂM 2018
PPSX
INVERSE FUNCTION
DOCX
Review guide end of mod 5
PDF
Picturepuzzleb
PPT
Implementation
ĐỀ THI SEAMO - CẤP ĐỘ C (KHỐI 5 + 6) NĂM 2018
Construx Record Sheet
ĐỀ THI SEAMO - CẤP ĐỘ B (KHỐI 3 + 4) NĂM 2018
ĐỀ THI SEAMO - CẤP ĐỘ A (KHỐI 1 + 2) NĂM 2018
INVERSE FUNCTION
Review guide end of mod 5
Picturepuzzleb
Implementation

What's hot (12)

PDF
BC Math 10 Systems Practice Test
PPT
Jeopardy chapter 6
PPTX
PPT On Puzzle Patterns For class 9
PDF
BC Math 10 Exponents Practice Test
PDF
actel fpga problems
PDF
2 5 2-6 absolute value graphs and translations
DOCX
Gambar Grafik Suatu fungsi
DOC
Math review for 4th period exam
DOC
Math review for 4th period exam
DOCX
Solucion compendio 4
PDF
xilinx fpga problems
PPT
TechMathII - Chapter1 Review Jeopardy
BC Math 10 Systems Practice Test
Jeopardy chapter 6
PPT On Puzzle Patterns For class 9
BC Math 10 Exponents Practice Test
actel fpga problems
2 5 2-6 absolute value graphs and translations
Gambar Grafik Suatu fungsi
Math review for 4th period exam
Math review for 4th period exam
Solucion compendio 4
xilinx fpga problems
TechMathII - Chapter1 Review Jeopardy
Ad

Similar to Data types in C programming (20)

PPT
simple notes for ug students for college
DOC
C language
PPTX
COM1407: Variables and Data Types
PPTX
Introduction to C language programming.pptx
PPTX
Data Type in C Programming
PPTX
Data Types and Variables In C Programming
PDF
C Programming Assignment
PDF
Lec08-CS110 Computational Engineering
PDF
C programming_MSBTE_Diploma_Pranoti Doke
PPT
Mesics lecture 3 c – constants and variables
PDF
C Tutorial
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PPTX
Constants variables data_types
PPTX
Lecture 2
PPT
Basics of C.ppt C PROGRAMMING AND DATA STRUCT
PPS
T02 a firstcprogram
PPS
T02 a firstcprogram
PPT
Basics of C.ppt
PPT
Basics of C.ppt
PPT
Basiscs of c++ include variable, special characters
simple notes for ug students for college
C language
COM1407: Variables and Data Types
Introduction to C language programming.pptx
Data Type in C Programming
Data Types and Variables In C Programming
C Programming Assignment
Lec08-CS110 Computational Engineering
C programming_MSBTE_Diploma_Pranoti Doke
Mesics lecture 3 c – constants and variables
C Tutorial
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants variables data_types
Lecture 2
Basics of C.ppt C PROGRAMMING AND DATA STRUCT
T02 a firstcprogram
T02 a firstcprogram
Basics of C.ppt
Basics of C.ppt
Basiscs of c++ include variable, special characters
Ad

More from Osama Ghandour Geris (20)

PPTX
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
PPT
Python week 5 2019-2020 for G10 by Eng.Osama Ghandour.ppt
PPT
Python cs.1.12 week 10 2020 2021 covid 19 for g10 by eng.osama mansour
PPT
Python cs.1.12 week 9 10 2020-2021 covid 19 for g10 by eng.osama ghandour
PPT
Python week 7 8 2019-2020 for grade 10
PPT
Python week 6 2019 2020 for grade 10
PPT
Python week 5 2019 2020 for g10 by eng.osama ghandour
PPT
Python week 4 2019 2020 for g10 by eng.osama ghandour
PPTX
Programming intro variables constants - arithmetic and assignment operators
PPT
Mobile appliaction w android week 1 by osama
PPT
Python week 1 2020-2021
PPT
6 css week12 2020 2021 for g10
PPT
Css week11 2020 2021 for g10 by eng.osama ghandour
PPTX
Cooding history
PPT
Computer networks--network
PPTX
How to print a sketch up drawing in 3d
PPT
Google sketch up-tutorial
PPTX
7 types of presentation styles on line
PPT
Design pseudo codeweek 6 2019 -2020
PPT
Php introduction
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Python week 5 2019-2020 for G10 by Eng.Osama Ghandour.ppt
Python cs.1.12 week 10 2020 2021 covid 19 for g10 by eng.osama mansour
Python cs.1.12 week 9 10 2020-2021 covid 19 for g10 by eng.osama ghandour
Python week 7 8 2019-2020 for grade 10
Python week 6 2019 2020 for grade 10
Python week 5 2019 2020 for g10 by eng.osama ghandour
Python week 4 2019 2020 for g10 by eng.osama ghandour
Programming intro variables constants - arithmetic and assignment operators
Mobile appliaction w android week 1 by osama
Python week 1 2020-2021
6 css week12 2020 2021 for g10
Css week11 2020 2021 for g10 by eng.osama ghandour
Cooding history
Computer networks--network
How to print a sketch up drawing in 3d
Google sketch up-tutorial
7 types of presentation styles on line
Design pseudo codeweek 6 2019 -2020
Php introduction

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PDF
Digital Logic Computer Design lecture notes
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
web development for engineering and engineering
PDF
composite construction of structures.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
PPT on Performance Review to get promotions
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mechanical Engineering MATERIALS Selection
Digital Logic Computer Design lecture notes
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
573137875-Attendance-Management-System-original
Model Code of Practice - Construction Work - 21102022 .pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
R24 SURVEYING LAB MANUAL for civil enggi
bas. eng. economics group 4 presentation 1.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Embodied AI: Ushering in the Next Era of Intelligent Systems
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
web development for engineering and engineering
composite construction of structures.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT on Performance Review to get promotions
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf

Data types in C programming

  • 2. Objectives • Be able to explain to others what a data type is • Be able to use basic data types in C programs • Be able to see the inaccuracies and limitations introduced by machine representations of numbers CoxSimple Data Types2
  • 3. What do you see? CoxSimple Data Types3
  • 4. What do you see? CoxSimple Data Types4
  • 6. CoxSimple Data Types6 Everything is Just a Bunch of Bits • Bits can represent many different things • Depends on interpretation • You and your program must keep track of what kind of data is at each location in the computer’s memory • E.g., program data types
  • 7. CoxSimple Data Types7 Big Picture • Processor works with finite-sized data • All data implemented as a sequence of bits • Bit = 0 or 1 • Represents the level of an electrical charge • Byte = 8 bits • Word = largest data size handled by processor • 32 bits on most older computers • 64 bits on most new computers
  • 8. CoxSimple Data Types8 Data types in C •Only really four basic types: • char • int (short, long, long long, unsigned) • float • double •Size of these types on •CLEAR machines: •Sizes of these types •vary from one machine •to another! Type Size (bytes) char 1 int 4 short 2 long 8 long long 8 float 4 double 8
  • 9. CoxSimple Data Types9 Characters (char) • Roman alphabet, punctuation, digits, and other symbols: • Encoded within one byte (256 possible symbols) • ASCII encoding (man ascii for details) • In C: char a_char = ’a’; char newline_char = ’n’; char tab_char = ’t’; char backslash_char = ’’;
  • 10. CoxSimple Data Types10 ASCII From “man ascii”: | 0 NUL| 1 SOH| 2 STX| 3 ETX| 4 EOT| 5 ENQ| 6 ACK| 7 BEL| | 8 BS | 9 HT | 10 NL | 11 VT | 12 NP | 13 CR | 14 SO | 15 SI | | 16 DLE| 17 DC1| 18 DC2| 19 DC3| 20 DC4| 21 NAK| 22 SYN| 23 ETB| | 24 CAN| 25 EM | 26 SUB| 27 ESC| 28 FS | 29 GS | 30 RS | 31 US | | 32 SP | 33 ! | 34 " | 35 # | 36 $ | 37 % | 38 & | 39 ' | | 40 ( | 41 ) | 42 * | 43 + | 44 , | 45 - | 46 . | 47 / | | 48 0 | 49 1 | 50 2 | 51 3 | 52 4 | 53 5 | 54 6 | 55 7 | | 56 8 | 57 9 | 58 : | 59 ; | 60 < | 61 = | 62 > | 63 ? | | 64 @ | 65 A | 66 B | 67 C | 68 D | 69 E | 70 F | 71 G | | 72 H | 73 I | 74 J | 75 K | 76 L | 77 M | 78 N | 79 O | | 80 P | 81 Q | 82 R | 83 S | 84 T | 85 U | 86 V | 87 W | | 88 X | 89 Y | 90 Z | 91 [ | 92 | 93 ] | 94 ^ | 95 _ | | 96 ` | 97 a | 98 b | 99 c |100 d |101 e |102 f |103 g | |104 h |105 i |106 j |107 k |108 l |109 m |110 n |111 o | |112 p |113 q |114 r |115 s |116 t |117 u |118 v |119 w | |120 x |121 y |122 z |123 { |124 | |125 } |126 ~ |127 DEL| Special control characters
  • 11. CoxSimple Data Types11 Characters are just numbers •What does this function do? Char fun(char c) { char new_c; if ((c >= ’A’) && (c <= ’Z’)) new_c = c - ’A’ + ’a’; else new_c = c; return (new_c); } return type procedure name argument type and name local variable type and name Math on characters! comparisons with characters!
  • 12. CoxSimple Data Types12 FP vs. Integer Results int i = 20 / 3; float f = 20.0 / 3.0; True mathematical answer: 20  3 = 6 2/3 i = ? f = ? 6 6.666667 Integer division ignores remainder FP arithmetic rounds result
  • 13. CoxSimple Data Types13 Integer Representations Base 2 Base 16 Unsigned 2’s Comp. 0000 0 0 0 0001 1 1 1 0010 2 2 2 0011 3 3 3 0100 4 4 4 0101 5 5 5 0110 6 6 6 0111 7 7 7 1000 8 8 -8 1001 9 9 -7 1010 A 10 -6 1011 B 11 -5 1100 C 12 -4 1101 D 13 -3 1110 E 14 -2 1111 F 15 -1 0000 0001 0010 0011 0100 0101 0110 011110001001 1011 1100 1101 1010 1110 1111 0 1 2 3 4 5 6 789 A D C B E F 0 1 2 3 4 5 6 789 10 11 12 13 14 15 0 1 -8 3 4 5 6 7 2 -7 -6 -5 -4 -3 -2 -1 Why one more negative than positive?
  • 14. CoxSimple Data Types14 Integer Representations Math for n bits: Define     1 0 2)(2 n i i i xxUB        2 0 1 1 22)(2 n i i i n n xxxTB  01 xxx n    sign bit 0=non-negative 1=negative Base 2 Base 16 Unsigned 2’s Comp. 0000 0 0 0 0001 1 1 1 0010 2 2 2 0011 3 3 3 0100 4 4 4 0101 5 5 5 0110 6 6 6 0111 7 7 7 1000 8 8 -8 1001 9 9 -7 1010 A 10 -6 1011 B 11 -5 1100 C 12 -4 1101 D 13 -3 1110 E 14 -2 1111 F 15 -1
  • 15. 15 Memory Layout of Records initials (2 bytes) married (1 byte) ss_number (4 bytes) • Fields are stored adjacently in memory. • Memory is allocated for records based on the order the fields are created. • Variables are aligned for easy reference. initials (2 bytes) ss_number (4 bytes) married (1 byte) Optimized for space Optimized for memory alignment 4 bytes / 32 bits 4 bytes / 32 bits