Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What is variable typecasting in Pascal?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Typecasting a variable means converting a variable from one data type to another. There are two types of typecasting in Pascal:

  • Implicit typecasting: Typecasting in which the range of values of the source data type is less than the range of values of the destination data type is called implicit typecasting.
  • Explicit typecasting: Typecasting in which the range of values of the source data type is greater than the range of values of the destination data type is called explicit typecasting.

Variable typecasting

Variable typecasting temporarily converts the data type of a variable. It does not change the contents of the variable. The variable typecasting of a variable can be done as follows:

_newDatatype_(_variable_)
  • _newDatatype_: The datatype in which _variable_ will be converted.
  • _variable_: The variable to be converted into the datatype _newDatatype_.

Note: Variable typecasting works on the variables whose addresses we can take.

Variable typecasting vs value typecasting

We can use variable typecasting on both sides of the assignment operator. We cannot use value typecasting on the left side of an assignment.

Code

Example 1

Consider the code snippet below, which demonstrates the use of variable typecasting.

program Example1;
var
ch1: Char = 'a';
num1: Integer = 10;
begin
writeln('');
num1 := Integer(ch1);
writeln('a typecased as integer: ', num1);
ch1 := Char(num1);
writeln('97 typecased as character: ', ch1);
end.

Explanation

  • Line 7: A character ch1 is typecased to an integer num1.
  • Line 10: An integer num1 is typecased to a character ch1.

Example 2

Consider the code snippet below, which demonstrates variable typecasting on the left side of an assignment.

program Example2;
var
ch1: Char = 'a';
ch2: Byte;
begin
writeln('');
Char(ch2) := ch1;
writeln('byte typecased as char: ',ch2);
Byte(ch1) := ch2;
writeln('char typecased as byte: ',ch1);
end.

Explanation

  • Line 7: A byte ch2 is typecased to a character ch1.
  • Line 10: A character ch1 is typecased to a byte ch2.

RELATED TAGS

pascal

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring