We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 396a2cd commit c8cbf8aCopy full SHA for c8cbf8a
chapter_2/exercise_2_02/loop.c
@@ -4,6 +4,8 @@
4
5
int main(void) {
6
char s[MAXLINE];
7
+ int c;
8
+ int i = 0;
9
10
// int i;
11
// int c;
@@ -13,21 +15,26 @@ int main(void) {
13
15
// s[i] = c;
14
16
// }
17
- int i = 0;
- int loop = 1;
18
- while (loop) {
19
- char c = getchar();
20
-
21
- if (i >= (MAXLINE - 1) || c == '\n' || c == EOF) {
22
- loop = 0;
+ while (1) {
+ if (i >= MAXLINE - 1) {
+ break;
23
}
24
25
- s[i++] = c;
+ c = getchar();
+
+ if (c == '\n') {
26
27
+ } else if (c == EOF) {
28
+ printf("\n");
29
30
+ } else {
31
+ s[i++] = c;
32
+ }
33
34
35
s[i] = '\0';
36
- printf("%s", s);
37
+ printf("%s\n", s);
38
39
return 0;
40
0 commit comments