[comp.lang.c] Help me find a hidden connection

mdf@tut.cis.ohio-state.edu (Mark D. Freeman) (05/05/88)

I have a routine that works fine in Microsoft C 4.0.  I am porting it
to 5.0 and starting to use function prototypes.  Now I am getting an
odd occurrance.  Using a variable in a printf() seems to change the
outcome of comparisons to that variable later in the code.  

The following code segment illustrates the problem:

************************************************************************

#define SG_H_LIN	196
#define BLK_H_LIN	220
#define BLK_H_LIN_B	223

int	drawbox(unsigned char h, unsigned short attr)
{
    unsigned short	hz, hzb;
    char		foo[2];			/* cheap hack to fix boxes */
    
    hz = (attr | (h ? h:(h = (unsigned char)SG_H_LIN)));

    sprintf(foo, "%c", h);			/* cheap hack to fix boxes */

    hzb = (h == (unsigned char)BLK_H_LIN ? (attr | (unsigned char)BLK_H_LIN_B) : hz);

************************************************************************

If 'h' as passed to this function is actually (unsigned char)BLK_H_LIN,
the comparison should result in hzb being assigned (attr | (unsigned
char)BLK_H_LIN_B).  However, if the sprintf() is taken out, the
comparison fails and hzb is assigned the value of hz.  I've changed the
%c in the sprintf() to %d and %u and it still 'fixes' the incorrect
comparison.

What am I doing that causes this comparison to act differently
depending on whether or not I try to sprintf() the variable 'h'?
-- 
Mark D. Freeman						  (614) 262-1418
					      mdf@tut.cis.ohio-state.edu
2440 Medary Avenue	   ...!cbosgd!osu-cis!tut.cis.ohio-state.edu!mdf
Columbus, OH  43202-3014      Guest account at The Ohio State University