Dec 15, 2013 at 10:53pm UTC
If i use
Unsigned
without explicitly declaring a data type does it always default to integer?
I made a short program to show what I mean. Are there any glaring pitfalls I'm not seeing by setting a program up this way?
This prints out 10
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main ()
{
unsigned pos = 0;
for (int i = 0; i < 10; i++){
pos = pos + 1;
}
std::cout << pos;
return 0;
}
This prints out zero
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main ()
{
unsigned pos = 0;
for (int i = 0; i < 10; i++){
pos = pos + 0.2;
}
std::cout << pos;
return 0;
}
Last edited on Dec 15, 2013 at 10:53pm UTC
Dec 15, 2013 at 11:23pm UTC
Last edited on Dec 15, 2013 at 11:23pm UTC
Dec 15, 2013 at 11:26pm UTC
Thanks for the response.
It appeared that way to me, but sometimes something only works because of a my compiler or IDE as others have pointed out to me before.
Long
also appears to default to an int data type as well.
That is good to know. I've seen people doing that in the past and I was confused by how it worked so that's why I made those tiny programs :)