You must be logged in to post Login Register


Register? | Lost Your Password?

Search Forums:


 






Wildcard Usage:
*    matches any number of characters
%    matches exactly one character

need help in C/C++

UserPost

11:20 pm
July 24, 2009


d4rks3ct0r

Newbie

posts 7

Hi Guyz,

I have a question…

If I know C++ then is it necessary to learn C?? I think all the things that in C can be done also by C++.

And I have a question in below code of C++…

#include<iostream.h>

void main()

{

      int i=0;

      i=400*400/400;

      cout<<i;

}

its output is "72".

why? I dont know the reason.

4:19 pm
July 26, 2009


blackSwan_

Newbie

posts 3

That's weird.  What compiler are you using?

9:57 am
July 27, 2009


d4rks3ct0r

Newbie

posts 7

Ii m using Turbo C++ compiler.

But its not a compiler problem, it gives this unreal result.

But also in java & C# its answer is 400.

I didnt understand the reason behind this.

9:28 pm
July 29, 2009


blackSwan_

Newbie

posts 3

Looks like the TurboC compiler does some automatic conversions of integer types.  I would look into that…

8:11 am
July 30, 2009


d4rks3ct0r

Newbie

posts 7

thnx bro and then tell me what's the reason behind this.

6:28 am
August 19, 2009


iraqi-coder

Newbie

posts 1

try this

# include <iostream.h>

VOID main ( )

{

int i=400;

cout << i*i/i ;

}

5:51 am
September 15, 2009


d4rks3ct0r

Newbie

posts 7

I have solved it.

When the number exceeds the limit of integer data type i.e. -32768 to 32767 then it is divided by 65536, and the remainder is considered.

So, in my case, since multiplication has higher precedence it will be solved first, so we get,

400*400=160000  and offcourse this value is crossing the integer data type limit, so it will be divided by 65536(2^16, since integer is of 16 bit or 2 byte) and then its remainder is taken.

160000%65536 = 28928

so equation becomes from this 400*400/400 to 28928/400

and offcourse, now 28928 is in the limit of integer data type, it will be divided normally,

so will get the result as 72.32, but the integer data type does not support the floating value and hence the decimal part is truncated so the answer is 72.

In case of such a number which is not in the integer data type limit and is smaller than 65536,

then 65536 will get divided by that number and remainder is considered, and this remainder automatically becomes negative get printed.

for eg.  i=200*200/200;

i=25536/200;   // 25536 is the remainder of 65536/40000

i=-127.68   // automatically becomes negative and offcourse the floating value will get truncated, so answer is,

i=-127.

:)Laugh


About the Infinity Exists Forum

Most Users Ever Online: 164

Currently Online:
11 Guests

Currently Browsing this Topic:
1 Guest

Forum Stats:

Groups: 4
Forums: 22
Topics: 1967
Posts: 9706

Membership:

There are 8194 Members

There are 2 Admins
There are 3 Moderators

Top Posters:

GONZO – 569
slicer45 – 270
Teddy – 268
madf0x – 230
clarke – 223
gube – 214

Recent New Members: existant187, hiperflash, TMNH_Omega, Vishal, cavuera, register

Administrators: Patchy (1648 Posts), Nox (40 Posts)

Moderators: CrashOverron (377 Posts), Override (207 Posts), Copy (163 Posts)