Unsigned

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
closed account (G30GNwbp)
unsigned is unsigned int
http://en.cppreference.com/w/cpp/language/types

The standard assumes that when a type modifier keyword (i.e. unsigned) is used by itself, you're dealing with an integer type (or at least whole numbers).
Last edited on
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 :)
Topic archived. No new replies allowed.