Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What is value 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 datatype to another. There are two types of typecasting in Pascal:

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

There are two types of explicit typecasting:

Value typecasting

Value typecasting converts the value of a data type to another data type. The value typecasting of a variable can be done as follows:

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

Note: The _value_ can be a variable or an expression.

Value typecasting vs variable typecasting

We cannot use value typecasting on the left side of assignment. On the other hand, we can use variable typecasting on both sides of assignment.

Code

Example 1

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

program Example1;
var
ch1: Char = 'a';
begin
writeln('');
writeln('a typecased as integer: ', Integer(ch1));
writeln('a typecased as byte: ', Byte(ch1));
end.

Explanation

A character ch1 is typecasted to an integer and a byte.

Example 2

Consider the code snippet below, which demonstrates the type casting of integer to other data types.

program Example2;
var
num1: Integer = 99;
begin
writeln('99 typecased as char: ', Char(num1));
writeln('99 typecased as bool: ', Boolean(num1));
end.

Explanation

An integer num1 is typecasted to a character and a boolean value.

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