|
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...
|