Path: utzoo!attcan!uunet!lll-winken!ames!mailrus!wasatch!cmos.utah.edu!jacobs From: jacobs%cmos.utah.edu@wasatch.UUCP (Steven R. Jacobs) Newsgroups: comp.sys.ibm.pc Subject: Re: Turboc floating scanf Message-ID: <889@wasatch.UUCP> Date: 11 Jan 89 04:52:07 GMT References: <370@atlas.tegra.UUCP> Sender: news@wasatch.UUCP Reply-To: jacobs%cmos.utah.edu.UUCP@wasatch.UUCP (Steven R. Jacobs) Organization: University of Utah, Computer Science Dept. Lines: 88 In article <370@atlas.tegra.UUCP> vail@tegra.UUCP (Johnathan Vail) writes: >I am using the "latest" version of TurboC and am trying to use scanf >to read floats. I give tcc a '-f' or '-f87' switch but I still get a >run-time error from scanf when it tries to read in a float. I've seen a problem in Turbo C that sounds like the one you describe, but it is more subtle than merely failing to handle floats in scanf(). For example, the program shown below compiles and executes correctly. (compiled with "tcc foo1.c", the '-f' or '-f87' are not needed) ----------- foo1.c ------------ #include main() { float y; printf("Input for foo1: "); scanf("%f", &y); printf("... %f\n", y); } ---------- end of foo1.c ------- The bug I have seen fails to handle floats in scanf(), but only when all references to floats are through pointers to structures which contain floats. For example, the following program will compile, but fails to run to completion. ----------- foo2.c ------------ #include typedef struct { float y; } FL_STRUCT; FL_STRUCT fls; main() { FL_STRUCT *ptr; ptr = &fls; printf("Input for foo2: "); fscanf(stdin, "%f", &(ptr->y)); printf("... %f\n", ptr->y); } ---------- end of foo2.c ------- When this program is run, the following two lines are produced as output: Input for foo2: scanf : floating point formats not linked Abnormal program termination The messages show that the program fails inside of scanf(), because the linker wasn't smart enough to include support for floating point. This same bug has appeared in all versions of Turbo C, and is the only Turbo C bug that has given me problems. The "fix" is to add a dummy variable of type float and use that variable in at least one scanf() call so that the floating point formats will be linked in. In versions 1.5 and 1.0 of Turbo C, the mere existence of a float that was assigned a value was enough to trigger the linking of floating point formats, but in 2.0 there must be a scanf() call which reads into a float that is not referenced through a structure pointer. A patched version of foo2.c is shown below, with changes which get around the bug. This is ugly, but it works. The patch only needs to be applied to one scanf() call in the program. ----------- foo2x.c ------------ #include typedef struct { float y; } FL_STRUCT; FL_STRUCT fls; main() { FL_STRUCT *ptr; float tc_kludge; ptr = &fls; printf("Input for foo2: "); fscanf(stdin, "%f", &tc_kludge); ptr->y = tc_kludge; printf("... %f\n", ptr->y); } ---------- end of foo2x.c ------- I hope that helps. ----- Steve Jacobs ({ihnp4,decvax}!utah-cs!jacobs, jacobs@cs.utah.edu)