Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function returning __int32 in place of __int64

Reply
Thread Tools

function returning __int32 in place of __int64

 
 
hurry
Guest
Posts: n/a
 
      12-28-2005
hi,

I am writing a c program in VC++ 6.

I have 2 files with 3 functions.

file-1 having two functions "a" and "c"
file-2 having a single function "b"

with function "a" as main() , "a" calls function "b" in file-2 which,
in turn calling function "c" in file-1.

function "c" and "b" are declared to return __int64. function "c"
returns it correctly but when it is assigned, it becomes a __int32 bit
number ( assigned to a __int64 var).
and interestingly, when i copy and paste the function "c" from file-2
to file 1, the program behaves as expected.

the code as is given below.

when "c" returns value to "b" it becomes __int32 format.
9007199237963776 becomes -16777216


file-1:

#include<stdio.h>
__int64 a();
__int64 b();

void main()
{
__int64 x;
x = b();
}

__int64 c()
{
__int64 y;
y = 9007199237963776;
return y;
}

file-2:

__int64 b()
{
__int64 z;
z = c();
return z;
}

 
Reply With Quote
 
 
 
 
tmp123
Guest
Posts: n/a
 
      12-28-2005
Hi,

Function "c" has no prototype in file-2.
Thus, the compiler (VC in this case) will asume "int".

Sugestion. Add "__int64 b(void);" at start of file-2.

Kind regards.

 
Reply With Quote
 
 
 
 
Anand
Guest
Posts: n/a
 
      12-28-2005
hurry wrote:
> hi,
>
> I am writing a c program in VC++ 6.

[...]
>
> when "c" returns value to "b" it becomes __int32 format.
> 9007199237963776 becomes -16777216
>

[...]
> __int64 c()
> {
> __int64 y;
> y = 9007199237963776;
> return y;
> }

[...]
__int64, and __int32 might be windows/VC specific data types and not std
C. So I am assuming all it is trying to represent is integer types of 64
bit and 32 bit long.

From what's provided in the post, the problem might be in the
statement: y = 9007199237963776;
The answer lies in sizeof(int) on your system(and compiler, of course.)

9007199237963776 is an integer constant of type 'int'.
1FFFFFFF000000 which is more than 32 bits and so this might be getting
truncated even before getting assigned to y.

You can try doing, instead,
y = 9007199237963776L; or
y = 9007199237963776LL;

--
(Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
(clc FAQ) http://c-faq.com
 
Reply With Quote
 
Anand
Guest
Posts: n/a
 
      12-28-2005
Anand wrote:
> hurry wrote:
>
>> hi,
>>
>> I am writing a c program in VC++ 6.

>
> [...]
>
>>
>> when "c" returns value to "b" it becomes __int32 format.
>> 9007199237963776 becomes -16777216
>>

> [...]
>
>> __int64 c()
>> {
>> __int64 y;
>> y = 9007199237963776;
>> return y;
>> }

>
> [...]
> __int64, and __int32 might be windows/VC specific data types and not std
> C. So I am assuming all it is trying to represent is integer types of 64
> bit and 32 bit long.
>
> From what's provided in the post, the problem might be in the
> statement: y = 9007199237963776;
> The answer lies in sizeof(int) on your system(and compiler, of course.)
>
> 9007199237963776 is an integer constant of type 'int'.
> 1FFFFFFF000000 which is more than 32 bits and so this might be getting
> truncated even before getting assigned to y.
>
> You can try doing, instead,
> y = 9007199237963776L; or
> y = 9007199237963776LL;
>

And, of course as tmp123 has rightly pointed out, the function c's
missing prototype might also be a strong (and main) candidate for the
problem.
--
(Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
(clc FAQ) http://c-faq.com
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-28-2005
Anand wrote:
> Anand wrote:
>> hurry wrote:
>>
>>> hi,
>>>
>>> I am writing a c program in VC++ 6.

>>
>> [...]
>>
>>>
>>> when "c" returns value to "b" it becomes __int32 format.
>>> 9007199237963776 becomes -16777216
>>>

>> [...]
>>
>>> __int64 c()
>>> {
>>> __int64 y;
>>> y = 9007199237963776;
>>> return y;
>>> }

>>
>> [...]
>> __int64, and __int32 might be windows/VC specific data types and not
>> std C. So I am assuming all it is trying to represent is integer types
>> of 64 bit and 32 bit long.
>>
>> From what's provided in the post, the problem might be in the
>> statement: y = 9007199237963776;
>> The answer lies in sizeof(int) on your system(and compiler, of course.)
>>
>> 9007199237963776 is an integer constant of type 'int'.
>> 1FFFFFFF000000 which is more than 32 bits and so this might be getting
>> truncated even before getting assigned to y.


I think you need to learn a bit more about C since you are wrong. From
the latest standard:
| The type of an integer constant is the first of the corresponding list
| in which its value can be represented.
| Suffix DecimalConstant Constant
| none int int
| long int unsigned int
| long long int long int
| unsigned long int
| long long int
| unsigned long long int
....
| If an integer constant cannot be represented by any type in its list,
| it may have an extended integer type, if the extended integer type can
| represent its value. If all of the types in the list for the constant
| are signed, the extended integer type shall be signed. If all of the
| types in the list for the constant are unsigned, the extended integer
| type shall be unsigned. If the list contains both signed and unsigned
| types, the extended integer type may be signed or unsigned.

So basically it will an int if it fits, otherwise it will be the
smallest type in which it fits.

>> You can try doing, instead,
>> y = 9007199237963776L; or
>> y = 9007199237963776LL;
>>

> And, of course as tmp123 has rightly pointed out, the function c's
> missing prototype might also be a strong (and main) candidate for the
> problem.


That will definitely be the problem, since without at least a
declaration in scope the compiler is required to assume the function
returns an int which of course can produce very strange results if that
is not true.

2 morals to this:
1) Don't give advice when you don't know the answer
2) Always use headers to provide the correct prototypes for functions.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      12-28-2005
On 2005-12-28, Anand <(E-Mail Removed)> wrote:
> y = 9007199237963776L; or


Not likely to fix it if long is 32 bits [AIUI likely on windows]

> y = 9007199237963776LL;


Not likely if the compiler doesn't support "long long"

besides - IIRC integer literals have to be of a type large enough to
contain them if such a type exists.

 
Reply With Quote
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-29-2005
"hurry" <(E-Mail Removed)> writes:

> hi,
>
> I am writing a c program in VC++ 6.
>
> I have 2 files with 3 functions.
>
> file-1 having two functions "a" and "c"
> file-2 having a single function "b"
>
> with function "a" as main() , "a" calls function "b" in file-2 which,
> in turn calling function "c" in file-1.
>
> function "c" and "b" are declared to return __int64. function "c"
> returns it correctly but when it is assigned, it becomes a __int32 bit
> number ( assigned to a __int64 var).
> and interestingly, when i copy and paste the function "c" from file-2
> to file 1, the program behaves as expected.
>
> the code as is given below.
>
> when "c" returns value to "b" it becomes __int32 format.
> 9007199237963776 becomes -16777216
>
>
> file-1:
>
> #include<stdio.h>
> __int64 a();
> __int64 b();


No such types in C.

typedef long long int64;
/* or if you really insist :
typedef __some_implementation_specific_type int64;
*/

> void main()


int main(void)

The return type of main must be int! And the argument list
should be either (void), or (int, char**).

> {
> __int64 x;
> x = b();
> }


Ever heard of indentation?

{
int64 x;
x = b(); /* or int64 x = b(); */
return 0; /* or EXIT_SUCCESS (from <stdlib.h> */
}

> __int64 c()
> {
> __int64 y;
> y = 9007199237963776;
> return y;
> }


More indentation, and a little bit of simplification:

int64 c()
{
return 9007199237963776;
}

> file-2:


Function below calls c() without prototype in scope.

int64 c();

In the current standard a diagnostic is required. In the previous standard
return type of int is assumed, but I'd be surprised if you don't get a
warning if you use proper flags to the compiler (if I remember correctly it
is /W4 on microsoft's compilers).

> __int64 b()
> {
> __int64 z;
> z = c();
> return z;
> }


int64 b()
{
return c();
}

/Niklas Norrthon
 
Reply With Quote
 
haroon
Guest
Posts: n/a
 
      12-29-2005

hurry wrote:
> hi,
>
> I am writing a c program in VC++ 6.
>
> I have 2 files with 3 functions.
>
> file-1 having two functions "a" and "c"
> file-2 having a single function "b"
>

[...]

> when "c" returns value to "b" it becomes __int32 format.
> 9007199237963776 becomes -16777216

if you compiled the program, you might have noticed an error saying

"c undefined assuming extern returning int"

now read 6.8.6.4 bullet number 3 of C99 draft.

this explains alot, doesn't it?

 
Reply With Quote
 
haroon
Guest
Posts: n/a
 
      12-29-2005

haroon wrote:

[...]

> if you compiled the program, you might have noticed an error saying

pardon me, it's a warning not an error.

P.S. hurry, you if u put some thing like `extern __int64 c();'
on top of file-2 (where you defined c) it seems to work fine.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Limiting function input only to __int64 but not int... A C++ 14 10-26-2011 08:02 AM
Why different sizes (with unions) when __int64 used??? JR C++ 3 02-05-2004 03:31 PM
__int64 vs. long long Tim Clacy C++ 8 11-10-2003 03:29 PM
unsigned __int64 overflow problem Vivi C++ 2 08-25-2003 06:09 AM
Re: Different results if compared as double or __int64? John Tsiombikas (Nuclear / the Lab) C++ 1 06-30-2003 02:31 PM



Advertisments