educative.io

Implicit Type Casting

A reader asked us the following question:

How can char be inserted and extracted in the stack when the type is int in stack constructer?

Hi,

This is Maida Ijaz from Educative.

In the implementation, Stack can only store integer values. But here we are, implicitly converting the character into its equivalent ASCII value (It will be an integer) and then storing that value into the Stack. So actually, we are comparing the ASCII value of the character in this program, not the character itself.

In implicit casting, the compiler automatically converts one data type to another. For example, if you store a character into a variable of integer type, the compiler will convert the char value into int without any user intervention. See the code given below:

#include <iostream>
using namespace std;

int main() {
char character = ‘A’;
int ASCII;
// Converts char data type into int implicitly
ASCII = character;
cout << "ASCII value = " << ASCII;
}

Every character has a Decimal ASCII value. In the example above, because there is no integer A, character A will be converted to its ASCII value, i.e., 65 automatically.

Best Regards,
Maida | Developer Advocate
educative.io