|
From: Ioannis V. <no...@ho...> - 2002-06-07 13:02:55
|
Also in my system, short ss was too small to store 64000 and the results got truncated. That's why i made it int. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys > -----Original Message----- > From: Ioannis Vranos [mailto:no...@ho...]=20 > Sent: Friday, June 07, 2002 3:59 PM > To: Dev-C++ Mailing List > Subject: RE: [Dev-C++] short integer problem >=20 >=20 > Yes it has the bug, because he used wrong format specifiers. For short > he should use %hd. Also in a variadic function, when the=20 > passed type is > different from the type specifier a casting should be used (e.g. > printf("\n sizeof(short): %d",(int)sizeof(short)); ). >=20 >=20 > #include <stdio.h> > #include <stdlib.h> >=20 >=20 > int main(void) > { > short x,y, entries; /* declared as int program works as > expected */ > int ss; > printf("\n sizeof(short): %d",(int)sizeof(short)); > printf("\n2 to the 15: %d\n\n",128*128*2); > printf("\n Enter 2 int values: \n"); > entries=3Dscanf("%hd",&x); > entries +=3Dscanf("%hd",&y); > printf("\n x=3D %hd y=3D %hd",x,y); >=20 > while (entries =3D=3D 2) { > printf("\n x=3D %hd y=3D %hd",x,y); > printf("\nX+Y =3D %hd",x+y); > printf("\nX-Y =3D %hd",x-y); > printf("\nX*Y =3D %hd",x*y); > printf("\nX/Y =3D %hd",x/y); > printf("\nx %c y =3D %hd\n",'\045',x%y); >=20 > ss =3D 64000; > printf("\nEnter a short int"); > scanf("%d",&ss); > printf("\n Another try on a short int gives: %d",ss); > printf("\n Enter 2 int values: "); > entries=3Dscanf("%hd%hd",&x,&y); > } >=20 > printf("\n\n"); > system("PAUSE"); > return 0; > } >=20 >=20 >=20 >=20 >=20 >=20 > Ioannis >=20 > * Ioannis Vranos > * Programming pages: http://www.noicys.cjb.net > * Alternative URL: http://run.to/noicys >=20 >=20 > > -----Original Message----- > > From: dev...@li...=20 > > [mailto:dev...@li...] On Behalf=20 > > Of Juliano P=E1vel Brasil Cust=F3dio > > Sent: Friday, June 07, 2002 4:23 AM > > To: dev...@li... > > Subject: RE: [Dev-C++] short integer problem > >=20 > >=20 > > Rob, I didn't got any error message when I tried to compile and > > run your program. By the way, I'm working with an Athlon > > 800MHz/WinXP/DevCpp 4.9.3.0. > >=20 > > Juliano > >=20 > > -----Original Message----- > > From: dev...@li... > > [mailto:dev...@li...] On Behalf=20 > > Of rchavez > > Sent: quinta-feira, 6 de junho de 2002 19:08 > > To: dev...@li... > > Subject: [Dev-C++] short integer problem > >=20 > >=20 > > Hi, > >=20 > > While trying a problem from my text 'A Book on C' I ran into some > > strange behavior for short integers. If I use integers, the program > > below reads them in & performs the arithmetic correctly. But if I > > change line 3 so the variables are of type short, the value of x is > > always 0 !! > >=20 > > I've played with it a lot but can't find the problem. Any=20 > > help would be > > appreciated. Also, notice that the scanf for var ss at the bottom > > always works. It's not a gcc bug since I get the same behavior on a > > unix compiler. > >=20 > > Rob > >=20 > > /** Math on Short Integers **/ > >=20 > > main() > > { > > short x,y, entries=3D2; /* declared as int program works as > > expected */ > > short ss; > > printf("\n sizeof(short): %d",sizeof(short)); > > printf("\n2 to the 15: %d\n\n",128*128*2); > > printf("\n Enter 2 int values: \n"); > > entries=3Dscanf("%d",&x); > > entries +=3Dscanf("%d",&y); > > printf("\n x=3D %d y=3D %d",x,y); > >=20 > > while (entries =3D=3D 2) { > > printf("\n x=3D %d y=3D %d",x,y); > > printf("\nX+Y =3D %d",x+y); > > printf("\nX-Y =3D %d",x-y); > > printf("\nX*Y =3D %d",x*y); > > printf("\nX/Y =3D %d",x/y); > > printf("\nx %c y =3D %d\n",'\045',x%y); > >=20 > > ss =3D 64000; > > printf("\nEnter a short int"); > > scanf("%d",&ss); > > printf("\n Another try on a short int gives: %d",ss); > > printf("\n Enter 2 int values: "); > > entries=3Dscanf("%d%d",&x,&y); > > } > >=20 > > printf("\n\n"); > > system("PAUSE"); > > return 0; > > } > >=20 > > _______________________________________________________________ > >=20 > > Don't miss the 2002 Sprint PCS Application Developer's=20 > > Conference August > > 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm > >=20 > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > >=20 > >=20 > > _______________________________________________________________ > >=20 > > Don't miss the 2002 Sprint PCS Application Developer's Conference > > August 25-28 in Las Vegas --=20 > http://devcon.sprintpcs.com/adp/index.cfm > >=20 > >=20 > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > >=20 >=20 |