Fundamental Data Types − Character
Overview of Character Data Type
Introduction to Character Data Type
- The lecture begins with an introduction to the character data type, outlining key topics such as size, range, and differences between signed and unsigned characters.
Representation of Characters in Computers
- Characters are represented in computers using binary (0s and 1s), with each character encoded using 8 bits. The ASCII encoding scheme is highlighted as a common method for this representation.
Understanding ASCII Encoding
ASCII Table Overview
- The ASCII table includes both printable and non-printable characters; non-printable ones serve as control characters while printable ones can be displayed on screens.
Size and Capacity of ASCII
- ASCII uses 7 bits for encoding, allowing for a total of 128 characters (from 0 to 127). However, since one byte equals 8 bits, the most significant bit is set to zero in this context.
Declaring Character Variables
Variable Declaration
- A character variable can hold only one character at a time. It must be declared using single quotes (' ') rather than double quotes (" "), or unexpected results may occur.
Integer Assignment to Characters
- Character variables can also store integer values. For example, assigning the value 65 will print 'A' when formatted correctly due to its corresponding position in the ASCII table.
Size and Range of Characters
Size Specifications
- A char variable occupies 1 byte. Unsigned characters have a range from 0 to 255, while signed characters range from -128 to +127 based on two's complement representation.
Extended ASCII Encoding Scheme
- The Extended ASCII encoding scheme utilizes the eighth bit for additional character representation, expanding the range from traditional ASCII's limit of 127 up to 255. This accommodates various languages beyond English.
Signed vs Unsigned Characters
Understanding Signed Range
- While unsigned ranges are straightforward, signed ranges raise questions about their necessity for character data types since negative values seem irrelevant in this context.
Internal Representation Considerations
Understanding Extended ASCII and 2's Complement Representation
The Basics of Extended ASCII
- The Extended ASCII table ranges from 0 to 127 for both signed and unsigned values. The difference lies in the representation of negative numbers: -128 to -1 for signed and 128 to 255 for unsigned.
- To represent -128 in binary using 2's complement, the most significant bit (MSB) is set to 1, indicating a negative value. This MSB has a place value of -2^7.
Representing Negative and Positive Values
- For representing -127, two bits are set to 1. Conversely, +129 can be represented easily by adding the values of 2^7 (128) and 2^0 (1).
- Notably, certain negative and positive values share identical binary representations; for example, -128 equals +128 in binary form.
Exploring Binary Equivalence
- To represent +255, all bits must be set to 1. However, this results in different interpretations based on the sign due to the MSB's place value.
- When printing characters associated with these values (-1 vs +255), they yield identical outputs as they correspond to the same character in the Extended ASCII table.
Verification of Character Outputs
- Testing shows that both +128 and -128 print the same character, confirming their equivalence.
- It is concluded that negative values do not provide additional power when dealing with character variables; each negative value corresponds directly to a positive one within the Extended ASCII range.
Summary of Key Concepts