Thread: [Dev-C++] short integer problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: rchavez <rc...@so...> - 2002-06-06 22:08:01
|
Hi,
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 !!
I've played with it a lot but can't find the problem. Any 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.
Rob
/** Math on Short Integers **/
main()
{
short x,y, entries=2; /* 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=scanf("%d",&x);
entries +=scanf("%d",&y);
printf("\n x= %d y= %d",x,y);
while (entries == 2) {
printf("\n x= %d y= %d",x,y);
printf("\nX+Y = %d",x+y);
printf("\nX-Y = %d",x-y);
printf("\nX*Y = %d",x*y);
printf("\nX/Y = %d",x/y);
printf("\nx %c y = %d\n",'\045',x%y);
ss = 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=scanf("%d%d",&x,&y);
}
printf("\n\n");
system("PAUSE");
return 0;
}
|
|
From: rchavez <rc...@so...> - 2002-06-09 02:22:55
|
Thanks for all the useful replies on this. They helped me clear up some other minor mysteries regarding double variables. My text did not mention format specifiers hd, le or lg at the appropriate times ( only in the appendix once I knew what I was looking for ). Two related questions if I may: double should be equivalent to long float but long float gives compiler error. Was the ANSI standard changed? long double compiles but I get strange behavior as I did for short when using %d instead of %hd. I looked for long double in Kernighan & Ritchie but saw only the modifiers h & l. Is there a format specifier for long double? If not, why do I not get an error in compiling as I did for short char? Rob |
|
From: Ioannis V. <no...@ho...> - 2002-06-09 03:17:31
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of rchavez > Sent: Sunday, June 09, 2002 5:23 AM > To: dev...@li... > Subject: [Dev-C++] short integer problem > > > Thanks for all the useful replies on this. They helped me > clear up some > other minor mysteries regarding double variables. My text > did not mention > format specifiers hd, le or lg at the appropriate times ( only in the > appendix once I knew what I was looking for ). > > Two related questions if I may: > > double should be equivalent to long float but long float > gives compiler > error. Was the ANSI standard changed? The latest ANSI C standard was ratified in 1999. However a long float type never existed. The built in non-pointer types of ANSI C 1989 (not mentioning signed and unsigned integer types explicitly) are: char, short, int, long, float, double and long double. > long double compiles but I get strange behavior as I did > for short when > using %d instead of %hd. I looked for long double in > Kernighan & Ritchie > but saw only the modifiers h & l. Is there a format > specifier for long > double? Yes. %lf. However long double has extremelly high precision and that means some time cost. Use long double when you want very very high floating point precision. In almost all cases float and double (when you want high precision) are sufficient. > If not, why do I not get an error in compiling as I > did for short > char? C has not a strong type safe system. Also it is very hard to check argument validity in variadic functions. Also there is not a short char type and i do not understand completely what warnings you mean. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys |
|
From: <jp...@bo...> - 2002-06-07 01:23:31
|
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.
Juliano
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of rchavez
Sent: quinta-feira, 6 de junho de 2002 19:08
To: dev...@li...
Subject: [Dev-C++] short integer problem
Hi,
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 !!
I've played with it a lot but can't find the problem. Any 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.
Rob
/** Math on Short Integers **/
main()
{
short x,y, entries=2; /* 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=scanf("%d",&x);
entries +=scanf("%d",&y);
printf("\n x= %d y= %d",x,y);
while (entries == 2) {
printf("\n x= %d y= %d",x,y);
printf("\nX+Y = %d",x+y);
printf("\nX-Y = %d",x-y);
printf("\nX*Y = %d",x*y);
printf("\nX/Y = %d",x/y);
printf("\nx %c y = %d\n",'\045',x%y);
ss = 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=scanf("%d%d",&x,&y);
}
printf("\n\n");
system("PAUSE");
return 0;
}
_______________________________________________________________
Don't miss the 2002 Sprint PCS Application Developer's Conference August
25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Ioannis V. <no...@ho...> - 2002-06-07 12:59:40
|
Yes it has the bug, because he used wrong format specifiers. For short
he should use %hd. Also in a variadic function, when the passed type is
different from the type specifier a casting should be used (e.g.
printf("\n sizeof(short): %d",(int)sizeof(short)); ).
#include <stdio.h>
#include <stdlib.h>
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);
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);
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);
}
printf("\n\n");
system("PAUSE");
return 0;
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
> -----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 -- 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
|
|
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 |
|
From: Luigi S. <gi...@li...> - 2002-06-07 15:01:23
|
At 22:23 06/06/02 -0300, Juliano P=E1vel Brasil Cust=F3dio wrote: > 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. Probably you had warnings turned off. BTW: did the program exhibit the=20 strange behavior reported by Rob? -- Luigi gi...@li... |
|
From: <jar...@ig...> - 2002-06-07 11:59:20
|
On 6 Jun, rchavez wrote:
> Hi,
>
> 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 !!
>
> I've played with it a lot but can't find the problem. Any 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.
>
> Rob
>
> /** Math on Short Integers **/
>
> main()
> {
> short x,y, entries=2; /* 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=scanf("%d",&x);
====================^^ ^
Since x is of type short int you MUST you the input format
%hd . If you write %d scanf inputs 4 bytes and thus overwrites the
2 bytes in storage following x .
Since scanf use a variable parameter list type checking is disabled.
One more reason to avoid the C input/output system.
In C++ the input operator '>>' has no such problems.
The same in the scanf calls below.
> entries +=scanf("%d",&y);
> printf("\n x= %d y= %d",x,y);
>
> while (entries == 2) {
> printf("\n x= %d y= %d",x,y);
> printf("\nX+Y = %d",x+y);
> printf("\nX-Y = %d",x-y);
> printf("\nX*Y = %d",x*y);
> printf("\nX/Y = %d",x/y);
> printf("\nx %c y = %d\n",'\045',x%y);
>
> ss = 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=scanf("%d%d",&x,&y);
> }
>
> printf("\n\n");
> system("PAUSE");
> return 0;
> }
>
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
Institute of Technology, RWTH Aachen
D 52056 Aachen, Germany
|
|
From: Jonathan R. <jon...@tr...> - 2002-06-07 13:51:46
|
how do i link to dll's in dev-c++. i cant find infoz thanks - jonathan |
|
From: Luigi S. <gi...@li...> - 2002-06-07 15:01:00
|
At 15:07 06/06/02 -0700, rchavez wrote:
>Hi,
>
>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 !!
>...
The reason, I guess, is that you are reading integers using the wrong
format, in fact, you read a short using "%d", instead of "%hd". I'm not an
expert, hopefully someone else can fill the details, so I'll say it simple:
you are "trying to put a round peg in a square hole" :).
I modified your code (hope you don't mind :), to isolate what seems the
"strange" behavior. Below is the result.
file: test.c
-------------
#include <stdio.h>
/*********************************************************************
Parametrize the type of integer used by the program
possible values are:
short
int
long
*********************************************************************/
#define INTEGER short
/*********************************************************************
How should an INTEGER be scanned or printed
The format should be chosen according to what is used for INTEGER,
thus values are:
short "%hd"
int "%d"
long "%ld"
If you mix, that is, setting INTEGER to short and using "%d" as
FORMAT; when compiling, expect to see, at least, a warning.
*********************************************************************/
#define FORMAT "%d"
/*********************************************************************
test - try to scan / print different types of integers
The program expects input from the console.
*********************************************************************/
int
main()
{
INTEGER i;
INTEGER j;
/* tell what is currently used for INTEGERS */
printf("sizeof(INTEGER):\t%d bytes\n",sizeof(INTEGER));
printf("format to scan/print:\t%s\n\n", FORMAT);
printf("Enter an INTEGER:\t");
scanf(FORMAT, &i); /* this is the offending line (it is #50) */
printf("you just entered:\t" FORMAT "\n\n", i);
printf("Enter another INTEGER:\t");
scanf(FORMAT, &j); /* the other offending line (it is #54) */
printf("you just entered:\t" FORMAT "\n\n", j);
printf("Simple operation using INTEGERS:\n");
printf("i = " FORMAT "\tj = " FORMAT "\n", i, j); /* surprise! */
printf("i + j = %d\n", i + j);
return 0;
}
-------------
I've generated a binary file using different compilers, here are the results:
*) Borland Turbo C++ version 1.01 (very old compiler, I use it for C only)
command: tcc -w test.c
results: no warning was emitted, executable works! No "strange" behavior
*) Borland C++ 5.5.1 (free command line tools):
command: bcc32 -w test.c
results: no warning was emitted, but executable exhibits the "strange"
behavior
*) GNU gcc 2.95.2 (Win98SE - MinGW)
command: gcc -Wall test.c
results: warnings emitted and executable exhibits the "strange" behavior
warning messages:
-----------------
test.c: In function `main':
test.c:50: warning: int format, different type arg (arg 2)
test.c:54: warning: int format, different type arg (arg 2)
One last thing: a C++ version of the program that uses stream extraction
operator, would have no problem. If you are allowed, switch to C++.
Regards
--
Luigi
gi...@li...
|