Expert C Programming

(Jeff_L) #1

So if you add a prototype for a K&R C definition including a short, the prototype will cause a
short to be passed, but the definition will expect an int, so it will retrieve junk from whatever
happens to be adjacent to the parameter. You can force cases 3 and 4 to work by writing the prototype
to use the widened type. This will detract from portability and confuse maintenance programmers. The
examples below show the two cases that fail.


file 1


/ old style definition, but has prototype /


olddef (d,i)


float d;


char i;


{


printf("olddef: float= %f, char =%x \n", d, i);


}


/ new style definition, but no prototype /


newdef (float d, char i)


{


printf("newdef: float= %f, char =%x \n", d, i);


}


file 2:


/ old style definition, but has prototype /


int olddef (float d, char i);


main() {


float d=10.0;


char j=3;


olddef(d, j);


/ new style definition, but no prototype /


newdef (d, j);


}


Expected output:


olddef: float= 10.0, char =3


newdef: float= 10.0, char =3


Actual output:


olddef: float= 524288.000000, char =4


newdef: float= 2.562500, char =0

Free download pdf