Pass value of short type

B

Busin

In the code below, should i be casted to "unsigned int" type first before
passing it into f()? Thanks!

void f(unsigned int);

int main()
{
unsigned short i;
i = 23;
/* Should i be casted to "unsigned int" type first before passing it
into f()? */
f(static_cast<unsigned int>(i))
}
 
A

Alf P. Steinbach

* Busin:
void f(unsigned int);

int main()
{
unsigned short i;
i = 23;
/* Should i be casted to "unsigned int" type first before passing it
into f()? */
f(static_cast<unsigned int>(i))
}

No.
 
J

JKop

Alf P. Steinbach posted:
* Busin:

No.


Think about that there for a second, why did the OP ask that question? They
want to understand the concept of what's going on. "No." doesn't answer
their question.

-JKop
 
J

JKop

Busin posted:
In the code below, should i be casted to "unsigned int" type first before
passing it into f()? Thanks!

void f(unsigned int);

int main()
{
unsigned short i;
i = 23;
/* Should i be casted to "unsigned int" type first before passing it
into f()? */
f(static_cast<unsigned int>(i))
}

That's what I myself call an implicit conversion, ie. you don't have to
explicitly specify that it is to be converted. Consider:

unsigned char snake = 12;

unsigned long int lizard = 4000000000UL;

snake = lizard;


You may be thinking here, Oh No! There's not enough room in that unsigned
char for that number! You may be tempted to write:

snake = (unsigned char)lizard;

But it's unneccesary. It's done "automatically", "implicitly". The unsigned
long int number is truncated, most likely from 4000000000 to 255, for
storage in the unsigned char.

My suggestion to you is just try to compile your code without any casts. If
it compiles, then all is fine and dandy. If it doesn't, *THEN* you can go
playing with casts.


-JKop
 
I

Ioannis Vranos

Busin said:
In the code below, should i be casted to "unsigned int" type first before
passing it into f()? Thanks!

void f(unsigned int);

int main()
{
unsigned short i;
i = 23;
/* Should i be casted to "unsigned int" type first before passing it
into f()? */
f(static_cast<unsigned int>(i))
}



There is no need for a casting, since an implicit conversion of built in
types takes place here.






Regards,

Ioannis Vranos
 
I

Ioannis Vranos

JKop said:
unsigned long int lizard = 4000000000UL;


Also the UL thing is not required here (it determines that it is an
unsigned long constant but is not needed here).


It is needed in cases like:



// function overloading
void something(int i);
void something(long i);
void something(unsigned long i);


something(4UL);



That said, your use of it does not raise any concerns.



snake = lizard;


You may be thinking here, Oh No! There's not enough room in that unsigned
char for that number! You may be tempted to write:

snake = (unsigned char)lizard;

But it's unneccesary. It's done "automatically", "implicitly". The unsigned
long int number is truncated, most likely from 4000000000 to 255, for
storage in the unsigned char.

My suggestion to you is just try to compile your code without any casts. If
it compiles, then all is fine and dandy. If it doesn't, *THEN* you can go
playing with casts.



The general rule is, avoid casts as much as possible. Use of casts in
C++ usually indicates bad programming design and implementation (in C
things are different).

And if you have to use a cast, use the safest one (and *never* use the C
style one that you use above).






Regards,

Ioannis Vranos
 
H

Howard

JKop said:
Busin posted:


That's what I myself call an implicit conversion, ie. you don't have to
explicitly specify that it is to be converted. Consider:

unsigned char snake = 12;

unsigned long int lizard = 4000000000UL;

snake = lizard;


You may be thinking here, Oh No! There's not enough room in that unsigned
char for that number! You may be tempted to write:

snake = (unsigned char)lizard;

But it's unneccesary. It's done "automatically", "implicitly". The unsigned
long int number is truncated, most likely from 4000000000 to 255, for
storage in the unsigned char.

Well, in general, that's going to be a bad thing, isn't it? You just lost
some data. The compiler should warn you about doing that, until you add the
cast, which makes the warning go away and makes you think you're safe, which
you might not be. (You might have needed that data!)

In the OP's question, there was no loss of data. You can always put an
unsigned short into an unsigned int, with no need for casting. There will
be no warning, because the data will always fit.

In the OP's case, the implicit conversion is a promotion from unsigned short
to unsigned int, which is different from trying to stick a long into a char.
My suggestion to you is just try to compile your code without any casts. If
it compiles, then all is fine and dandy. If it doesn't, *THEN* you can go
playing with casts.

I agree you should avoid casts whenever possible (and especially avoid
C-style casts in C++). But just because it compiles, don't assume it works.
Heed compiler warnings, especially in cases where you might lose data!

-Howard
 
O

Old Wolf

JKop said:
unsigned char snake = 12;
unsigned long int lizard = 4000000000UL;

snake = lizard;

You may be thinking here, Oh No! There's not enough room in that unsigned
char for that number! You may be tempted to write:

snake = (unsigned char)lizard;

But it's unneccesary. It's done "automatically", "implicitly". The unsigned
long int number is truncated, most likely from 4000000000 to 255, for
storage in the unsigned char.

Actually, it will never be 255. As you have been told in another thread,
the rules for unsigned overflow are well-defined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,941
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top