Jump to content

Typecasting

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hey guys! I know it's been a while since my last tutorial. So, I thought I would make one on typecasting. This tutorial is gonna be ANSII chart basically.

Explaination of a typecast: It basically makes one variable type into another. So, basically I can take an integer variable and make it a character. The ANSII chart we are going to make is gonna be centered around the one on this site. If you are looking at this, I am assuming you know the basics of C++, so I wont explain the main function. For an explanation of a for loop, look at my tutorial. I am only going to explain the typecast.

The code:

#include <iostream>


using namespace std;


int main(void) {

for(int x=0;x <=127;x++){

cout << x << " = " << (char)x << endl;

}

}


Explanation:
cout << x << " = " << (char)x << endl;
Basically, I declared variable x as an integer. By placing (char) in front of it, I typecasted it into a character, meaning the value of x was changed into its ANSII value.

I know this wasn't that great, but it has been a while since I've written a tutorial, so meh... Any questions you may have, feel free to ask. I am always willing to help.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Just an observation: technically, that's a C-style cast, which is required for built-in types. Classes usually use dynamic_cast, static_cast, const_cast, and reinterpret_cast.

+rep :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
heh...true...i havent coded in a while either. Last week was my last week of school.
Check out my site: www.khaoticirc.net