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; }
karl@haddock.ima.isc.com (Karl Heuer) (08/09/90)
In article <5631@uwm.edu> peter@csd4.csd.uwm.edu (Peter J Diaz de Leon) writes: >When reg1 is an unsigned int mode prints out correctly. >When reg1 is an unsigned long int [it fails]. > test(reg1, mode) unsigned long reg1; int mode; { > printf("TEST: mode=0x%x \n", mode); > } > ... test(0x1, ME); The formal parameter has type `unsigned long int', but the actual argument `0x1' has type `int'. If you have lint, it should have flagged the mismatch. The portable fix is to use an explicit cast: ... test((unsigned long int)0x1, ME); If you're only interested in ANSI C compilers, then you could use a typed constant ... test(0x1UL, ME); or make sure there's a prototype in scope when test() is called: test(unsigned long reg1, int mode) { printf("TEST: mode=0x%x \n", mode); } ... test(0x1, ME); Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint