Jump to content


c++ types,


5 replies to this topic

#1 kruseborn

    Member

  • Members
  • PipPip
  • 33 posts

Posted 02 December 2009 - 01:10 PM

What is the different when writing for example unsigned i = 0; and unsigned i = 0u; Does the compiler compile this two examples different?

The same question regarding big numbers, when for example long long i = 1LL << 35;

Should you always tell the compiler what kind of type you are using?

#2 Wernaeh

    Senior Member

  • Members
  • PipPipPipPip
  • 368 posts

Posted 02 December 2009 - 01:30 PM

See this discussion:
http://stackoverflow...e-or-at-runtime

As a short summary, your compiler is free to evaluate these constant expressions either at compile time or at runtime.

However, your compiler needs to at least evaluate the types of a given constant expression to provide compile-time typing errors.

Thus most compilers automatically perform the appropriate calculations as well for optimization purposes.

Thus,
long long i = 1LL << 35;
commonly is precalculated, but may also be explicitly calculated on weird compilers.

Just have a look at your compiler output for the appropriate line for the actual behaviour.

I generally don't bother with providing constant type specifiers, since I think code is more readable without them. However, I add them whereever required to solve typing problems.

I.e.
float myValue = 1;
But:
float myValueToo = MyInteger / 7.f;

Hope this helps,
Cheers,
- Wernaeh
Some call me mathematician, some just call me computer guy. Yet, I prefer the term professional weirdo :)

#3 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 02 December 2009 - 01:35 PM

There is no difference. The compiler automatically converts them to the type you need.

The difference comes in when calling templated/overloaded functions.

e.g.

void foo(int i) {cout << "int" << endl;}
void foo(unsigned int i) {cout << "unsigned int" << endl;}

int main()
{
  foo(0); // prints "int"
  foo(0U); // prints "unsigned int"
}


#4 kruseborn

    Member

  • Members
  • PipPip
  • 33 posts

Posted 02 December 2009 - 01:36 PM

Thank you

#5 kruseborn

    Member

  • Members
  • PipPip
  • 33 posts

Posted 02 December 2009 - 01:53 PM

Well it is a different,
this code print different outputs:

#include <iostream>


using namespace std;


int main()

{

    long long temp = 1LL<<35;

    long long temp2 = 1<<35;

    cout << temp << endl;

    cout << temp2 << endl;

    return 0;

}


poita said:

There is no difference. The compiler automatically converts them to the type you need.

The difference comes in when calling templated/overloaded functions.

e.g.

void foo(int i) {cout << "int" << endl;}

void foo(unsigned int i) {cout << "unsigned int" << endl;}


int main()

{

  foo(0); // prints "int"

  foo(0U); // prints "unsigned int"

}


#6 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 02 December 2009 - 02:28 PM

Yes, it will matter in that case because you have an expression that will be evaluated as an int. 1<<35 cannot be represented as an int, so you get garbage. You need to promote the 1 to a long long so that it will be evaluated correctly.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users