Hi ..
Can someone explain what is a "cast" and how can be used it?
Thank you in advance.
Hi ..
Can someone explain what is a "cast" and how can be used it?
Thank you in advance.
The following code should set the char, character, to the value 2.
Here's an example of what NOT to do:
char character;
int integer;
integer = 2;
character = integer;
This stores the value 2 to character, but printing the character will not display 2. The ASCII value that character needs to hold to display 2 is actually 50!
Try this instead:
integer = 2;
character = (char)integer;
(char) is called a type-cast and is used to automatically convert variables of one type to another. character will now print as 2.
Good programming always uses type-casting when using a line of code that uses different data types.