Changing an integer's type?

My topic isn't extremely accurate, nonetheless, here is my question.

My question is how to make an int value take a different type of value? This sounds very confusing, here is an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int add(int x, int y)
{
return x + y;
}

int main()
{
int number = 89;
long long int verylonginteger = 8438584392938578439;
int number34 = 34;
//Now i want int number to take the value of verylonginteger + number34
int number = add(verylonginteger + number34);
cout << int number;
}


How do i make int number be able to take the other two values added up without a warning coming up?

Last edited on
long 4 byte . int 4 byte. but if u add a 4 byte + 4 byte item.

u will get the error message
I know that if you add two different integer types it doesnt work, (if thats what you mean) but how do you make them add together without getting the warning.
you cant save a long long int in an int variable

that's like trying to fit an elefant in a doghouse
Oh, i see... Then the only way I have a chance of doing this by making the integer into a long long int?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

long long int add(long long int x, int y);

long long int add(long long int x, int y)
{
	return x + y;
}

int main()
{
	long long int number = 89;
	long long int verylonginteger = 8438584392938578439;
	int number34 = 34;
	//Now i want int number to take the value of verylonginteger + number34
	number = add(verylonginteger, number34);
	cout << number;
}
Thanks for clearing that!

Just one last question. How much memory would this take?
Im suspecting a lot.
Not really a lot. If you want to see the memory usage in bytes, use the sizeof() operator. ie:

cout << "Long long has a size of: " << sizeof(long long) << " bytes" << endl;


Sure. its (usually) double the size of an integer, so obviously you'd want to use an int or a short if you dont need the additional range.
There is a way of converting values to different types, a static cast:

 
static_cast<long long>(number);

The '<' and '>' brackets enclose a template. You will probably learn about templates later on, so you don't need to bother with it yet. Between the bigger/smaller than signs you put the goal type, and the name of the variable you want to convert between the round brackets.

I hope this helps!
If not, have a cookie!
(>^w^)>O
...that is also the wrong thing to do, because you are telling the compiler to ignore all the type information it has about 'number' and clobber any memory that happens to be nearby. Don't do that.

If you want to use space, allocate it first the proper way. See Darkmaster's example.
Topic archived. No new replies allowed.