[comp.os.msdos.programmer] Unsigned long question

peter@csd4.csd.uwm.edu (Peter J Diaz de Leon) (08/09/90)

I am having problems with the following simple piece of code.
When reg1 is an unsigned int mode prints out correctly.
When reg1 is an unsigned long int mode prints out 
incorrect results. I have tried both Turbo C 2.0
and Turbo C++ 1.0 and get the strange results. 
Can somebody please explane to me what I am
over looking.

I am running on a Compaq Deskpro 286 using 
4dos ver:3.01a and DOS ver:3.31

	Thanks
	-Peter
	peter@csd4.csd.uwm.edu
	peter@cvax.cs.uwm.edu

=============================================================================

#include <stdio.h>
#define ME 0x12

test(reg1, mode)
unsigned long reg1;
int mode;
{
    printf("TEST: mode=0x%x \n", mode);
    return;
}


main()
{
    test(0x1, ME);
    return;
}

poffen@sj.ate.slb.com (Russ Poffenberger) (08/20/90)

In article <5633@uwm.edu> peter@csd4.csd.uwm.edu (Peter J Diaz de Leon) writes:
>I am having problems with the following simple piece of code.
>When reg1 is an unsigned int mode prints out correctly.
>When reg1 is an unsigned long int mode prints out 
>incorrect results. I have tried both Turbo C 2.0
>and Turbo C++ 1.0 and get the strange results. 
>Can somebody please explane to me what I am
>over looking.
>
>I am running on a Compaq Deskpro 286 using 
>4dos ver:3.01a and DOS ver:3.31
>
>	Thanks
>	-Peter
>	peter@csd4.csd.uwm.edu
>	peter@cvax.cs.uwm.edu
>
>=============================================================================
>
>#include <stdio.h>
>#define ME 0x12
>
>test(reg1, mode)
>unsigned long reg1;
>int mode;
>{
>    printf("TEST: mode=0x%x \n", mode);
>    return;
>}
>
>
>main()
>{
>    test(0x1, ME);
>    return;
>}


Ah yes, the infamous incompatible arguments problem. On most PC compilers,
long and ints are not the same size. (int 16 bits, longs 32 bits). By default,
constants are interpreted as ints. You are therefore attempting to pass an int
argument to a function that expects a long.

To fix it, you must do one of two things..

1.) Typecast to a long, for a constant, this is not the best practice.

2.) Specify the constant as being long by appending 'l' to it. ie


	test(0x1l,ME);


Russ Poffenberger               DOMAIN: poffen@sj.ate.slb.com
Schlumberger Technologies       UUCP:   {uunet,decwrl,amdahl}!sjsca4!poffen
1601 Technology Drive		CIS:	72401,276
San Jose, Ca. 95110             (408)437-5254

darcy@druid.uucp (D'Arcy J.M. Cain) (08/23/90)

In article <1990Aug20.163914.20063@sj.ate.slb.com> poffen@sj.ate.slb.com (Russ Poffenberger) writes:
>In article <5633@uwm.edu> peter@csd4.csd.uwm.edu (Peter J Diaz de Leon) writes:
>>I am having problems with the following simple piece of code.
>>When reg1 is an unsigned int mode prints out correctly.
>>When reg1 is an unsigned long int mode prints out 
>>incorrect results. I have tried both Turbo C 2.0
>>
>>#include <stdio.h>
>>#define ME 0x12
>>
>>test(reg1, mode)
>>unsigned long reg1;
>>int mode;
>>{
>>    printf("TEST: mode=0x%x \n", mode);
>>    return;
>>}
>>main()
>>{
>>    test(0x1, ME);
>>    return;
>>}
>Ah yes, the infamous incompatible arguments problem. On most PC compilers,
>long and ints are not the same size. (int 16 bits, longs 32 bits). By default,
>
>To fix it, you must do one of two things..
>
>1.) Typecast to a long, for a constant, this is not the best practice.
>
>2.) Specify the constant as being long by appending 'l' to it. ie

Or do the "right" (tm) thing and use ANSI prototypes:

test(unsigned long reg1, int mode)
{
    printf("TEST: mode=0x%x \n", mode);
    return;
}

Since the function precedes the call to it, it effectively becomes a
prototype for itself and the promotion is automatically made.  Also
a good idea if terms like "Data Abstraction" mean anything to you.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   MS-DOS:  The Andrew Dice Clay
West Hill, Ontario, Canada         |   of operating systems.
+ 416 281 6094                     |