473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Stupid compiler error

Hi,

Made me laugh so I though I'd share this.

short i = 32;
short j = 1;
short k = i + j;

gives an error
Cannot implicitly convert type 'int' to 'short'

That is a bit poo in my reckoning...

at least the following compiles.

short k = (short)(i + j);

Nov 15 '05 #1
8 2518
The same thing happens in Java, and there it seems that '+' will return
"at least int, because of normal promotions".
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
Hello, rollasoc!

r> short i = 32;
r> short j = 1;
r> short k = i + j;

int k = i + j; // Ok
Nov 15 '05 #3
You will get the same if you try

byte = byte + byte;

I wonder why

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #4
Indeed true

but that won't work when I the call a C++ function expecting a short.
"Malik Arykov" <ar****@r40.nalog.ru> wrote in message
news:##**************@TK2MSFTNGP11.phx.gbl...
Hello, rollasoc!

r> short i = 32;
r> short j = 1;
r> short k = i + j;

int k = i + j; // Ok

Nov 15 '05 #5
The same thing happens in Java, and there it seems that '+' will return
"at least int, because of normal promotions".
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #6
Hello

The CLR can perform arithmetic operations on 32 bit or 64 bit values only.
It can't perform 16 bit or 8 bit operations.
So the i and j are implicitly converted to Int32, then added, so the result
of the addition is an Int32, which have to be explicitly cast to short to
get a short result.

Best regards,
Sherif

"rollasoc" <ho*****@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
Hi,

Made me laugh so I though I'd share this.

short i = 32;
short j = 1;
short k = i + j;

gives an error
Cannot implicitly convert type 'int' to 'short'

That is a bit poo in my reckoning...

at least the following compiles.

short k = (short)(i + j);

Nov 15 '05 #7
Hi,

That's the normal behavior, see this scenario
short i = 32767 ; // the max positive value of a short aka int16
short j = 32;

the result of i + j overflow a short, so basically you have three choises:
1- ignore the overflow and do nothing, now you have an invalid result in k ,
tracking this error is a big pain!! ( I don;t really remember but I think
that this is what C/C++ does )

2- have the compiler do the check and throw an Overflow Exception, very
costly as now a lot of operations must be checked for this. C# provided a
controled way of doing this, see the checked keyword in C#

3- Automatically promote the type , this is the safer and faster way . I
think that Jave does this too ( is that right jon? )

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"rollasoc" <ho*****@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
Hi,

Made me laugh so I though I'd share this.

short i = 32;
short j = 1;
short k = i + j;

gives an error
Cannot implicitly convert type 'int' to 'short'

That is a bit poo in my reckoning...

at least the following compiles.

short k = (short)(i + j);

Nov 15 '05 #8
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
That's the normal behavior, see this scenario
short i = 32767 ; // the max positive value of a short aka int16
short j = 32;

the result of i + j overflow a short, so basically you have three choises:
1- ignore the overflow and do nothing, now you have an invalid result in k ,
tracking this error is a big pain!! ( I don;t really remember but I think
that this is what C/C++ does )

2- have the compiler do the check and throw an Overflow Exception, very
costly as now a lot of operations must be checked for this. C# provided a
controled way of doing this, see the checked keyword in C#

3- Automatically promote the type , this is the safer and faster way . I
think that Jave does this too ( is that right jon? )


Yup, Java does the same thing. Of course, the result of adding two ints
could overflow too - I believe it's more for efficiency reasons than to
avoid overflow, if you see what I mean. Modern processors tend to have
instructions to add 2 32-bit integers, but possibly not 16 or 8 bits...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matthew Del Buono | last post by:
Don't try to solve the problem. I've found a way -- around or fixing it. I'm just curious as to whether this is Microsoft's problem in their compiler or if there's a standard saying this is to be...
5
by: raz | last post by:
Greetings all. I apologize for what is almost certainly a stupid question, but I can't figure this out, and have no more time for head bashing... The short version: what is the appropriate...
2
by: Lampa Dario | last post by:
Hi, where is this stupid error in this program? When I execute it, i receive a segmentation fault error. #include <stdio.h> int main(int argc, char *argv, char *env) { int i=0; int l=0; int...
3
by: Bill | last post by:
I am confused why I must cast an enum to a type int if I defined the enum as follows: enum COLUMNS :int { IP=0, MSG =1 } IMHO the compiler should be able to make the conversion without...
1
by: Clocker | last post by:
Hello, I never practiced C++ development in VC++.net before. (only Unix) what does the error: d:\C++\UMath\UAlgebra.cpp(71): fatal error C1010: unexpected end of file while looking for...
6
by: Malvolio | last post by:
I know this is a *really* stupid question but what the hell is visual c++? I downloaded it and it doesn't seem to work. It isn't like any c++ compiler I ever saw before and the manual on the...
5
by: Puppet_Sock | last post by:
So, I'm madly coding away, and my fingers stutter, and I produce this. (mfirstToken is a std::string object.) if(m_firstToken.c_str() == 'M' || m_firstToken.c_str().c_str() == 'T') { // ......
7
by: ot_007_2001 | last post by:
Hi, all, For pricticing defination of structure in C, I wrote a small code as follow: #include<stdio.h> struct datastruct { int data;
10
by: Chris Becke | last post by:
In C and in older C++ compilers, you could write code like this void a(void** pp); // declare a function that takes a pointer to a pointer-to-void. struct I {}; // some struct. I *i1,**i2; //...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.