what is the output of 'c++' code?????
what is the output of 'c++' code?????
void main()
{
int num=190*300/300;
cout<<"num:"<
}
explain me !!!!!!what is the logic behind it????
๐๐๐
void main()
{
int num=190*300/300;
cout<<"num:"<
explain me !!!!!!what is the logic behind it????
๐๐๐
Replies
-
MaRoWhat is the logic behind what?
-
shalini_goel14
@Maro I guess he/she is trying to ask what is the logic behind the output?MaRoWhat is the logic behind what?
By the way -what will be the output? C++ users answer Please? -
Prasad AjinkyaThe answer is logical, the int datatype has a 2byte storage. 2 bytes equals to 16 bits, which equals to a maximum value of 32768 for unsigned int, and 16384 for signed int. Any value higher than this, rolls over and goes to the extreme value (0 for signed and -16384 for unsigned). It keeps on doing this until the overflow does not occur.
190 * 300 creates an overflow of the int datatype. So the first iteration gets you 57000-16384, which is somewhere near 40000. Now it switches to -16384 and starts counting towards 16384. Once it reaches 16384, there are some 7800 odd more to go, so it switches back to -16384 and finishes off at -8536.
This is then divided by 300. To get -28.
I checked this against the code, and split it up and checked the logic as well. Works out. -
pradeep_agrawal
I compiled the above code for 32-bit machine and then executed the code and it gave me "190" as output which i feel is desired output.impecable_abhivoid main()
{
int num=190*300/300;
cout<<"num:"<}
explain me !!!!!!what is the logic behind it????
But as you have posted this query so i feel you are not getting the desired output. Could you post more details like what output you are getting? Which compiler you are using? Is it 16-bit or 32-bit compiler?
I am taking below guess for the issue you may be facing (i might be incorrect):
- You are using some 16-bit compiler.
- The size of data type 'int' on such compiler is 2 bytes, so the value of variables will range from -32768 to +32767.
- In the given statement
int num=190*300/300;
there are operators * and / having same precedence so based on the associativity for these the operations will be executed from left to right. Hence result for * will be computed first and the / will be computed.
- "190*300" results '57000' which exceeds the range (upper limit is +32767). Due to this overflow the variable now contain some -ve value and hence '/' is then not resulting desired output.
-Pradeep
-
MaRoPlease give more info about your machine & compiler you using.
-
Prasad AjinkyaPradeep, I ran this code on TurboC, the same compilers which our colleges used to have. It takes a pain to run it on Vista ๐, but it runs still the same. So, yes I am using a 16 bit compiler.
My bad, it is -32767 to +32767. Nonetheless, the calculations remain the same, except it overflows only once ๐
And TurboC first tackles the * operator since it needs to know the exact nature of the numerator to do the division. -
pradeep_agrawal
I am using a 32-bit gcc compiler and running it on 32-bit RHEL5 machine. For using gcc on Windows download cygwin or djgpp2. For compiling code use gpp compiler.MaRoPlease give more info about your machine & compiler you using.
Actually i was replying to query from impecable_abhi but by the time i could have posted you had already replied to the mail๐kidakakaI ran this code on TurboC, the same compilers which our colleges used to have. It takes a pain to run it on Vista ๐, but it runs still the same. So, yes I am using a 16 bit compiler.
Looks like impecable_abhimight be facing the issue with using the compiler that generates 16-bit code (might be turboc).
-Pradeep -
rama_krish627I run this code and O got 190 as the out put. why you are not getting this output. Let me tell you the details of the compiler you are using. So that we will come out with the solution.
-
silverscorpionI use turbo C 16 bit compiler and I got -28 as the answer.
How will you get 190 as the answer? What is the compiler you are using?? -
pradeep_agrawal
I am using 32-bit gcc compiler with which i am not facing the issue.silverscorpionI use turbo C 16 bit compiler and I got -28 as the answer.
How will you get 190 as the answer? What is the compiler you are using??
As i mentioned in the post
#-Link-Snipped-#
and
#-Link-Snipped-#
the issue will be coming with with compilers generating 16-bit code because in i16-bit the size of int is 2 byte and the value ranges from -32768 to 32767.
Here depending on the operator precedence and associativity "190*300" will be computed first resulting '57000' which is greater then the upper limit of int '32767' and causes overflow and hence resulting in some incorrect value.
The problem will not come with compilers generating 32-bit code (e.g., 32-bit gcc compiler) as that will have size of int as 4 byte and the value can range from รข2147483648 to 2147483647.
Let me know if it need more explanation of any item.
-Pradeep -
silverscorpionOk. Now I understand. It's clear.
Thanks!! -
impecable_abhi
thanx salini for understanding me well.....shalini_goel14@Maro I guess he/she is trying to ask what is the logic behind the output?
By the way -what will be the output? C++ users answer Please? -
slashfearHi abhi (Guess thats your name!!),
void main()
{
int num=190*300/300;
cout<<"num:"<}
ok so you wanna know how the code work here is the answer:
First your a creating as well as assigning a value to the variable (num) of type integer.
here you assign the value of num as num=190*300/300, so according to operator precedence * (multiply) as well as / (divide) has the same operator precedence value. so First 190*300 is calculated which results in 57000 and then the resulting value (57000)is divided with 300. Hence, 57000/300 results in 190, so the value of num is assigned to 190.
Now, in the next line of code :
cout<<"num:"<
cout followed by << is used to display the content in the display or console. so what we have given here is num: , the value given within the " " is a junk, so what ever we give with in the double coats will be displayed as it is in the console (Remember the values with in the double coats will be ignored by the compiler and hence it will be displayed as it is given in the console)
so <
so the output of the code is: num: 190
Hope It made scene to you !!! If you have further doubt please do reply buddy!!
;-) -
slashfearHi,
The logic behind the code??
There is no logic behind the code buddy dont complicate your self -
pradeep_agrawal
The output does not comes as 190 when user compiles the code to generate executable for 16-bit machine. See below post for more details:slashfearHi abhi (Guess thats your name!!),
so the output of the code is: num: 190
#-Link-Snipped-#
-Pradeep -
slashfearpradeep_agrawalThe output does not comes as 190 when user compiles the code to generate executable for 16-bit machine. See below post for more details:
#-Link-Snipped-#
-Pradeep
Hey Pradeep,
Ya what you say is right, in 16 bit machine when you try to compile the program it would cause overflow as the range of integer is -32768 to 32767. And you would get the answer as 190 when you use a 32 bit machine as integer has the range รข2147483648 to 2147483647. ๐ -
slashfearHi abhi,
When your using a 16 bit machine you have to change the data type as long int instead of int so that you would get the desired output. ๐
You are reading an archived discussion.
Related Posts
Hi everybody, I am a spanish student. I am not exactly studying civil engineering. I am studing "degree in Public works technical engineering, specialized in transport and urban services" that...
Is there anyone who has bought civil engineering instruments from AIMIL company or has procured civil engineering consultancy services?
hi,
i have used the micro controller 89C52 for sending data into PDA mobile.
i searched lot for RS-232 to USB converter so i can send my data thro' USB...
HI FRIENDS :
I WANT CIRCUIT THAT HAVE(4 RELAYS - 1 BUSH BUTTON - LAMP)
IF I PRESS THE BUSH PUTTON THE LAMP IS TURNED ON IF I PRESS AGAIN...
HI EVERYBODY,
IM JUST NOW JOIN THIS SITE. I NEED HELP FOR MY PROJECT WORK IN MY FINAL YEAR CIVIL BACHELOR OF ENGINEERING.THAT FLY ASH BASED GEOPOLYMER CONCRETE.FOR MY PROJECT...