aboutsummaryrefslogtreecommitdiff
path: root/ch5
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2015-12-19 18:40:02 -0800
committerzlg <zlg@zlg.space>2015-12-19 18:40:02 -0800
commit0c7a59180de778a4e9019176a65eb02b5493b6f7 (patch)
tree967bf532cf9cc9ed2d0416257b8102dcd29aaded /ch5
parentChange license to GPLv3, add contributing guide (diff)
downloadknr-0c7a59180de778a4e9019176a65eb02b5493b6f7.tar.gz
knr-0c7a59180de778a4e9019176a65eb02b5493b6f7.tar.bz2
knr-0c7a59180de778a4e9019176a65eb02b5493b6f7.tar.xz
knr-0c7a59180de778a4e9019176a65eb02b5493b6f7.zip
1-16 solution code and comment style cleanup
Also of note is the length that the program reports includes the newline character at the end. This has been clarified in a comment. The exercise text does not specify whether to include newlines as part of the length, so it was left alone. Removed the contributor notice, since contributor acknowledgements are centralized.
Diffstat (limited to 'ch5')
-rw-r--r--ch5/5-01_getint-fixed.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/ch5/5-01_getint-fixed.c b/ch5/5-01_getint-fixed.c
index 8d53ba6..b013f20 100644
--- a/ch5/5-01_getint-fixed.c
+++ b/ch5/5-01_getint-fixed.c
@@ -23,6 +23,7 @@ int main() {
while (getint(bar) > 0) {
printf("%d\n", foo);
}
+ printf("Contents of buffer:%s\n", buf);
return 0;
}
@@ -38,8 +39,14 @@ int getint(int *pn) {
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-') {
c = getch();
+ if (!isdigit(c)) {
+ ungetch(c);
+ ungetch(sign == 1 ? '+' : '-');
+ return 0;
+ }
}
if (!isdigit(c)) {
+ ungetch(c);
return 0;
}
for (*pn = 0; isdigit(c); c = getch()) {