0% found this document useful (0 votes)
34 views25 pages

Datatypes in C: ESC101 9 August

This document discusses various data types in C programming including char, int, float, and double. It explains how each data type represents data in memory with different bit sizes and formats. For example, it describes how floats use a sign bit, exponent bits, and mantissa bits to represent numbers in scientific notation in a fixed number of bits. The document also covers type conversions between data types, which can cause loss of precision or unexpected behavior if not done properly. Implicit and explicit type casting is explained along with examples.

Uploaded by

Arvind Samriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views25 pages

Datatypes in C: ESC101 9 August

This document discusses various data types in C programming including char, int, float, and double. It explains how each data type represents data in memory with different bit sizes and formats. For example, it describes how floats use a sign bit, exponent bits, and mantissa bits to represent numbers in scientific notation in a fixed number of bits. The document also covers type conversions between data types, which can cause loss of precision or unexpected behavior if not done properly. Implicit and explicit type casting is explained along with examples.

Uploaded by

Arvind Samriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Datatypes in C

ESC101
9th August
Last Class
• Basic alphabet of C
– Character set
– ASCII encoding
• Constructing words from alphabets
– Identifiers
– Keywords
This Class
• C data types
• Type conversions
– Implicit
– Explicit
• Consequences
• Related reading: Chapter 2.7, 2.8, 2.13 in
Gottfried
Data Types in C
• char
– Single character, e.g. a or C or 6 or $
• int
– Bounded integers, e.g. 732 or -5
• float
– Real numbers, e.g. 3.14 or 2.0
• double
– Real numbers with more precision

9/19/2019 4 Esc101, Programming


Type Modifiers in C
• Signed
– Range [-2N-1 , 2N-1-1]
• Unsigned
– Range [0,2N-1]
• Short
– Can use half the size of the normal data type
• Long
– Can use double the size of the normal data type
Composite data types
• signed short int = signed short = short (%hi)
• signed long int = signed long = long (%li)
• unsigned int (%u)
• float (%f)
• double (%f or %lf)
• long double (%Lf)
Floating Point Representation
• Have to represent three things
– sign
– Number
– Exponent
• Assign some bits of memory for each
– 1 bit for sign
– m for exponent
– n for mantissa
Conceptual Example
• Consider a 4 bit memory
– What can you assign with unsigned int?
• 0,1,.....15
– What can you assign with signed int?
• Use twos complement notation
• -8,-7,.... ,7
– What can you assign with float?
s e m m 1.0, 1.1, 1.2, 1.3
2.0, 2.2, 2.4, 2.6
-1.0, -1.1, -1.2, -1.3
(-1)s * 1.m* 2e-0 -2.0, -2.2, -2.4, -2.6
Edge Cases in float Representations
• Has upper bound
• Has lower bound
• Needs special handling for special numbers
– Zero (when e and m are all zeros)
– Infinity
• Exact matches can be problematic
– Is x = 0.902323?
temp_conversion.c • Microprocessors represent real
numbers using finite precision, i.e.,
Compile and Run using limited number of digits after
decimal point.
# include <stdio.h>
int main() { • Typically uses scientific notation:
float C; 12.3456789 represented as
float F; 1.23456789E+1.
C=50;
F = ((9*C)/5) + 32;
“%f” signifies that the corresponding
printf(“The temperature”);
variable is to be printed as a real number in
printf( “ %f ”, C);
decimal notation.
printf(“Celsius equals”);
printf(“ %f ”, F);
printf(“Fahrenheit”); C 50.000 F
return 0; ?? ??
122.0000
}

The temperature 50.000000 Celsius equals 122.000000 Fahrenheit


IEEE 754 Floating Point Representation
Single-precision Floating Point
Representation

This is what you’re using when you are invoking float


Double Precision
• Same logic as single precision, but with 64 bits
of memory
Type Conversion (Type Casting)
• Converting values of one type to other.
– Example: int to float and float to int
(also applies to other types)
• Can be implicit or explicit
int k =5;
float x = k; // good implicit conversion, x gets 5.0

float y = k/10; // poor implicit conversion

float z = ((float) k)/10; // Explicit conversion, z gets 0.5


Typecasting Application
int main() { int main() {
int total=100, number=50; int total=100, number=50;
float percentage=0.0; float percentage=0.0;
percentage=(number/total)*100; percentage= (float) number/total*100;
printf("%.2f",percentage); printf("%.2f",percentage);
return 0; return 0;
} }

Output: 0.00 Output: 50.00


Loss of Information!

• Type conversion may result in lost information

• Larger sized type (e.g. float ) converted to


smaller sized type (e.g. int) is undefined/
unpredictable

9/19/2019 16 Esc101, Programming


float to int: type conversion (result ok)
float x;
#include<stdio.h>

int main() {
(int) x;
float x; int y; /* define two variables */
x = 5.67;
converts the
y = (int) x; /* convert float to int */
real value
printf(“%d”, y);
stored in x into
return 0;
an integer. Can
}
be used
anywhere an
int can.
Output : 5
float to int Type Conversion (not ok!)
• float is a larger box, int is a The floating
smaller box. Assigning a float to point number
1E50 is too
an int may lead to loss of
large to fit in
information and unexpected an integer box.
values.
# include <stdio.h>
int main() { Output:
float x; int y; 2147483647 Careful when
x = 1.0E50; // 1050 converting from a
y = (int) x; ‘larger’ type to
printf(“%d”, y); ‘smaller’ type.
return 0; Undefined.
Rocket Science
• First launch of the Ariane
5 rocket on 4th June 1996
• Rocket lost its flight path
and disintegrated 40
seconds into launch
• Cost  $370 million
• Fundamental cause of
disaster
– Float to int data type
casting
Rocket science
• Rocket’s horizontal velocity
was greater than older rocket’s
(Ariane 4)
• Inertial navigation system
– A single calculation required
conversion from double to int
– Worked fine with the smaller
value in Ariane 4
– Gave a garbage result for the
large value in Ariane 5
• Commanded engine to make
an impossibly large course
correction
• Veered off course
– Auto-destruct sequence
initialized
char to int
• Range: 0 to 255
• You should NOT try to remember ASCII
values
– Encoding/programming languages provide
alternatives to use them
• C treats characters as integers corresponding
to their ASCII value
• While displaying with %c placeholder, the
ASCII value is converted to its corresponding
character
9/19/2019 21 Esc101, Programming
ASCII character set

Translates letters to numbers for the computer to


understand
char  int

• conversion between character and integer


datatypes can be exploited to write programs.

Output:
printf("%d\n", 'A'); 65
55
printf("%d\n", '7'); F
printf("%c\n", 70); 321 is outside range! What
printf("%c\n", 321); do you think will be the
output of
printf("%c\n",321);
Try it out
9/19/2019 23 Esc101, Programming
char-int operations

• Interconversion between character and


integer datatypes can be exploited to write
programs.
Output:
printf("%c\n", ‘C’+5); H
printf("%c\n", ‘D’ - ‘A’ + ‘a’ ); d
53
printf("%d\n", ‘3’ + 2);

• Placeholder determines the output.


• Use with caution.
• Avoid arithmetic operation such as * and / on characters.
•9/19/2019
Common Mistake: Incorrect data
24
type of placeholder.
Esc101, Programming
Next Class
• C program structure
– Variables
– Declarations
– Expressions
– Statements
• Related reading: Chapter 2.9-2.12 in Gottfried

You might also like