Explanation

Started by
10 comments, last by Drakkcon 19 years, 4 months ago
#include <iostream>using namespace std;int main(){    int x = 0, y = 4;    int thisIsAlsoAnIntVariable = 10;    y = y * 8;        cout << "The number stored in x is " << x << ".\n";    cout << "On a 32 bit system, an int is " << y << " bits.\n";    cout << "An int can be any name you want (following some rules of course).\n"    	 << "Such as thisIsAlsoAnIntVariable = " << thisIsAlsoAnIntVariable << endl;        return 0;}    


Without using a function, you can see that an int is just a place to store a number. You can use it for math (y = y * 8), and ask the computer to show you what it is. The name is your choice, but usually long names are not used because they are slow to type and remember, easier to screw up typing, and make the code look messier.

Note: the 'int' before main just tells what type of value my function will return. This corresponds with 'return 0', which is saying the int that it is returning is now 0.
--Eric BurnettI know I have mnemophobia, but I can't remember what it is.
Advertisement
I think what's confusing you is that you're trying to think of it from a math perspective. X and Y represent something hidden, and I'm trying to figure it out. Here's really what's going on:
int x, y; x and y are blank spaces in memory. If you're using a 32-bit CPU, then they're probably 32 bits each. You've just allocated 64 bits of memory. Now, you store data in that memory; you can only store integers. So X = 4; is storing the value 4 in those 32-bits. It's just a way to represent a number.

This topic is closed to new replies.

Advertisement