Okay, here’s the error:
s[i++] = '\0'; // always put a '\0' character to a string array ending, so that the compiler knows it's a string.
This will cause it to terminate the string even for no input (when it got EOF
directly), and since it increments i
before returning it, getLine()
will never return 0 and thus main()
will never stop. Removing the ++
fixed it, for my simple test.
Also, the comment is misleading, the compiler doesn’t know anything. The compiler is no longer around when the code runs; the in-memory format of strings is something that’s needed to keep the run-time libraries happy since that’s what they expect.
1
solved K&R, find longest line, same code but not working?