Behaviour of getchar() in a while loop
up vote
2
down vote
favorite
I am running this c program in the terminal
#include <stdio.h>
int main() {
int result = 0;
while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}
printf("outn");
}
After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.
Why doesn't this terminate after the first 'x'?
c getchar
add a comment |
up vote
2
down vote
favorite
I am running this c program in the terminal
#include <stdio.h>
int main() {
int result = 0;
while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}
printf("outn");
}
After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.
Why doesn't this terminate after the first 'x'?
c getchar
1
Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.
– chux
Nov 9 at 16:12
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am running this c program in the terminal
#include <stdio.h>
int main() {
int result = 0;
while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}
printf("outn");
}
After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.
Why doesn't this terminate after the first 'x'?
c getchar
I am running this c program in the terminal
#include <stdio.h>
int main() {
int result = 0;
while(result <= 0)
{
int result = (getchar() != EOF);
result = 2;
printf("x");
}
printf("outn");
}
After that I type in the word "hello" followed by a return. The result is that I get multiple 'x' characters.
Why doesn't this terminate after the first 'x'?
c getchar
c getchar
edited Nov 9 at 16:13
chux
78.8k869145
78.8k869145
asked Nov 9 at 15:21
theo_vvv
233
233
1
Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.
– chux
Nov 9 at 16:12
add a comment |
1
Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.
– chux
Nov 9 at 16:12
1
1
Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.
– chux
Nov 9 at 16:12
Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.
– chux
Nov 9 at 16:12
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
You're re-declaring (shadowing result
) inside the while loop. The result
that is used in while(result <= 0)
is the one that is declared outside the loop.
Thanks. A bit stupid, I was kind off blind for the obvious I guess.
– theo_vvv
Nov 9 at 15:29
1
so to address the wording of the question title directly - the mystery had nothing to do with getchar()
– Kevin Olree
Nov 9 at 17:30
add a comment |
up vote
0
down vote
Well,
#include <stdio.h>
int main() {
int result = 0; /* here *OUTER* result gets the value 0 */
while(result <= 0) /* THIS MAKES THE While to execute forever */
{
int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
result = 2; /* external block result is not visible here so this assign goes to the above inner result */
printf("x");
/* INNER result CEASES TO EXIST HERE */
}
printf("outn");
}
As you can deduct from the comments, the result
variable that is compared in the while
test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of x
s printed on stdout
.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
You're re-declaring (shadowing result
) inside the while loop. The result
that is used in while(result <= 0)
is the one that is declared outside the loop.
Thanks. A bit stupid, I was kind off blind for the obvious I guess.
– theo_vvv
Nov 9 at 15:29
1
so to address the wording of the question title directly - the mystery had nothing to do with getchar()
– Kevin Olree
Nov 9 at 17:30
add a comment |
up vote
5
down vote
accepted
You're re-declaring (shadowing result
) inside the while loop. The result
that is used in while(result <= 0)
is the one that is declared outside the loop.
Thanks. A bit stupid, I was kind off blind for the obvious I guess.
– theo_vvv
Nov 9 at 15:29
1
so to address the wording of the question title directly - the mystery had nothing to do with getchar()
– Kevin Olree
Nov 9 at 17:30
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
You're re-declaring (shadowing result
) inside the while loop. The result
that is used in while(result <= 0)
is the one that is declared outside the loop.
You're re-declaring (shadowing result
) inside the while loop. The result
that is used in while(result <= 0)
is the one that is declared outside the loop.
answered Nov 9 at 15:25
ODYN-Kon
2,0531425
2,0531425
Thanks. A bit stupid, I was kind off blind for the obvious I guess.
– theo_vvv
Nov 9 at 15:29
1
so to address the wording of the question title directly - the mystery had nothing to do with getchar()
– Kevin Olree
Nov 9 at 17:30
add a comment |
Thanks. A bit stupid, I was kind off blind for the obvious I guess.
– theo_vvv
Nov 9 at 15:29
1
so to address the wording of the question title directly - the mystery had nothing to do with getchar()
– Kevin Olree
Nov 9 at 17:30
Thanks. A bit stupid, I was kind off blind for the obvious I guess.
– theo_vvv
Nov 9 at 15:29
Thanks. A bit stupid, I was kind off blind for the obvious I guess.
– theo_vvv
Nov 9 at 15:29
1
1
so to address the wording of the question title directly - the mystery had nothing to do with getchar()
– Kevin Olree
Nov 9 at 17:30
so to address the wording of the question title directly - the mystery had nothing to do with getchar()
– Kevin Olree
Nov 9 at 17:30
add a comment |
up vote
0
down vote
Well,
#include <stdio.h>
int main() {
int result = 0; /* here *OUTER* result gets the value 0 */
while(result <= 0) /* THIS MAKES THE While to execute forever */
{
int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
result = 2; /* external block result is not visible here so this assign goes to the above inner result */
printf("x");
/* INNER result CEASES TO EXIST HERE */
}
printf("outn");
}
As you can deduct from the comments, the result
variable that is compared in the while
test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of x
s printed on stdout
.
add a comment |
up vote
0
down vote
Well,
#include <stdio.h>
int main() {
int result = 0; /* here *OUTER* result gets the value 0 */
while(result <= 0) /* THIS MAKES THE While to execute forever */
{
int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
result = 2; /* external block result is not visible here so this assign goes to the above inner result */
printf("x");
/* INNER result CEASES TO EXIST HERE */
}
printf("outn");
}
As you can deduct from the comments, the result
variable that is compared in the while
test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of x
s printed on stdout
.
add a comment |
up vote
0
down vote
up vote
0
down vote
Well,
#include <stdio.h>
int main() {
int result = 0; /* here *OUTER* result gets the value 0 */
while(result <= 0) /* THIS MAKES THE While to execute forever */
{
int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
result = 2; /* external block result is not visible here so this assign goes to the above inner result */
printf("x");
/* INNER result CEASES TO EXIST HERE */
}
printf("outn");
}
As you can deduct from the comments, the result
variable that is compared in the while
test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of x
s printed on stdout
.
Well,
#include <stdio.h>
int main() {
int result = 0; /* here *OUTER* result gets the value 0 */
while(result <= 0) /* THIS MAKES THE While to execute forever */
{
int result = (getchar() != EOF); /* THIS VARIABLE IS ***NOT*** THE outside result variable */
result = 2; /* external block result is not visible here so this assign goes to the above inner result */
printf("x");
/* INNER result CEASES TO EXIST HERE */
}
printf("outn");
}
As you can deduct from the comments, the result
variable that is compared in the while
test is the outer one, while the inner one hides the outer one, no assignations can be made to it in the body of the loop, so the loop runs forever. You get an infinite string of x
s printed on stdout
.
answered Nov 15 at 9:41
Luis Colorado
3,9991717
3,9991717
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228536%2fbehaviour-of-getchar-in-a-while-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Tip: a well enabled compiler may report "warning: variable 'result' set but not used [-Wunused-but-set-variable]". Save time and enable all compiler warnings.
– chux
Nov 9 at 16:12