How the answer of this for loop is 5?
I was practicing my C skills online. And I got a question like:
What is the output of this program?
void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}
The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
c
|
show 2 more comments
I was practicing my C skills online. And I got a question like:
What is the output of this program?
void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}
The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
c
Possible duplicate of Undefined behavior and sequence points
– Ken White
Nov 22 '18 at 4:47
The proper declarations formainareint main (void)andint main (int argc, char **argv)(which you will see written with the equivalentchar *argv). note:mainis a function oftype intand it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?
– David C. Rankin
Nov 22 '18 at 4:51
@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?
– Lundin
Nov 22 '18 at 7:44
Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.
– Gerhardh
Nov 22 '18 at 8:24
@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??
– David C. Rankin
Nov 22 '18 at 18:29
|
show 2 more comments
I was practicing my C skills online. And I got a question like:
What is the output of this program?
void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}
The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
c
I was practicing my C skills online. And I got a question like:
What is the output of this program?
void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}
The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
c
c
asked Nov 22 '18 at 4:45
MrAlphaMrAlpha
4619
4619
Possible duplicate of Undefined behavior and sequence points
– Ken White
Nov 22 '18 at 4:47
The proper declarations formainareint main (void)andint main (int argc, char **argv)(which you will see written with the equivalentchar *argv). note:mainis a function oftype intand it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?
– David C. Rankin
Nov 22 '18 at 4:51
@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?
– Lundin
Nov 22 '18 at 7:44
Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.
– Gerhardh
Nov 22 '18 at 8:24
@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??
– David C. Rankin
Nov 22 '18 at 18:29
|
show 2 more comments
Possible duplicate of Undefined behavior and sequence points
– Ken White
Nov 22 '18 at 4:47
The proper declarations formainareint main (void)andint main (int argc, char **argv)(which you will see written with the equivalentchar *argv). note:mainis a function oftype intand it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?
– David C. Rankin
Nov 22 '18 at 4:51
@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?
– Lundin
Nov 22 '18 at 7:44
Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.
– Gerhardh
Nov 22 '18 at 8:24
@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??
– David C. Rankin
Nov 22 '18 at 18:29
Possible duplicate of Undefined behavior and sequence points
– Ken White
Nov 22 '18 at 4:47
Possible duplicate of Undefined behavior and sequence points
– Ken White
Nov 22 '18 at 4:47
The proper declarations for
main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?– David C. Rankin
Nov 22 '18 at 4:51
The proper declarations for
main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?– David C. Rankin
Nov 22 '18 at 4:51
@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?
– Lundin
Nov 22 '18 at 7:44
@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?
– Lundin
Nov 22 '18 at 7:44
Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.
– Gerhardh
Nov 22 '18 at 8:24
Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.
– Gerhardh
Nov 22 '18 at 8:24
@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??
– David C. Rankin
Nov 22 '18 at 18:29
@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??
– David C. Rankin
Nov 22 '18 at 18:29
|
show 2 more comments
3 Answers
3
active
oldest
votes
This is explained by the sequence of events:
i = 1; // for init
i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
i++; // inside for body, makes i = 3
i++; // for increment, makes i = 4
i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
// condition is false, for loop ends, i = 5
Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.
add a comment |
i is incremented four times before the for loop exits.
- Twice in the
forcondition check (formallycond_expression). Since you are using postfix increment, the first timeiwill be1when the condition is checked. It is only on the second check,iwill be2. - Once in the increment (formally
iteration_expression) - Once more in the body (formally
loop_statement) of theforloop.
As the initial value of i is 1, this brings its value to 5.
Note:
- cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
- iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.
1
@MrAlpha: I update the answer. Please see if this clarifies.
– P.W
Nov 22 '18 at 5:04
add a comment |
As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
How can i never be less than or equal to 1 when it's initialised with a value of 1?
Did you intend to pre-increment i?
That would be:
for (i=1; ++1 <= 1; i++)
But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.
If you do
for (i=1; ++i <= 1; i++)
i will be > 1 on the first iteration and nothing will happen.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53424031%2fhow-the-answer-of-this-for-loop-is-5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is explained by the sequence of events:
i = 1; // for init
i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
i++; // inside for body, makes i = 3
i++; // for increment, makes i = 4
i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
// condition is false, for loop ends, i = 5
Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.
add a comment |
This is explained by the sequence of events:
i = 1; // for init
i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
i++; // inside for body, makes i = 3
i++; // for increment, makes i = 4
i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
// condition is false, for loop ends, i = 5
Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.
add a comment |
This is explained by the sequence of events:
i = 1; // for init
i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
i++; // inside for body, makes i = 3
i++; // for increment, makes i = 4
i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
// condition is false, for loop ends, i = 5
Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.
This is explained by the sequence of events:
i = 1; // for init
i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
i++; // inside for body, makes i = 3
i++; // for increment, makes i = 4
i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
// condition is false, for loop ends, i = 5
Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.
answered Nov 22 '18 at 4:56
HavenardHavenard
17.9k22646
17.9k22646
add a comment |
add a comment |
i is incremented four times before the for loop exits.
- Twice in the
forcondition check (formallycond_expression). Since you are using postfix increment, the first timeiwill be1when the condition is checked. It is only on the second check,iwill be2. - Once in the increment (formally
iteration_expression) - Once more in the body (formally
loop_statement) of theforloop.
As the initial value of i is 1, this brings its value to 5.
Note:
- cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
- iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.
1
@MrAlpha: I update the answer. Please see if this clarifies.
– P.W
Nov 22 '18 at 5:04
add a comment |
i is incremented four times before the for loop exits.
- Twice in the
forcondition check (formallycond_expression). Since you are using postfix increment, the first timeiwill be1when the condition is checked. It is only on the second check,iwill be2. - Once in the increment (formally
iteration_expression) - Once more in the body (formally
loop_statement) of theforloop.
As the initial value of i is 1, this brings its value to 5.
Note:
- cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
- iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.
1
@MrAlpha: I update the answer. Please see if this clarifies.
– P.W
Nov 22 '18 at 5:04
add a comment |
i is incremented four times before the for loop exits.
- Twice in the
forcondition check (formallycond_expression). Since you are using postfix increment, the first timeiwill be1when the condition is checked. It is only on the second check,iwill be2. - Once in the increment (formally
iteration_expression) - Once more in the body (formally
loop_statement) of theforloop.
As the initial value of i is 1, this brings its value to 5.
Note:
- cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
- iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.
i is incremented four times before the for loop exits.
- Twice in the
forcondition check (formallycond_expression). Since you are using postfix increment, the first timeiwill be1when the condition is checked. It is only on the second check,iwill be2. - Once in the increment (formally
iteration_expression) - Once more in the body (formally
loop_statement) of theforloop.
As the initial value of i is 1, this brings its value to 5.
Note:
- cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
- iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.
edited Nov 22 '18 at 5:11
answered Nov 22 '18 at 4:49
P.WP.W
12.7k3944
12.7k3944
1
@MrAlpha: I update the answer. Please see if this clarifies.
– P.W
Nov 22 '18 at 5:04
add a comment |
1
@MrAlpha: I update the answer. Please see if this clarifies.
– P.W
Nov 22 '18 at 5:04
1
1
@MrAlpha: I update the answer. Please see if this clarifies.
– P.W
Nov 22 '18 at 5:04
@MrAlpha: I update the answer. Please see if this clarifies.
– P.W
Nov 22 '18 at 5:04
add a comment |
As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
How can i never be less than or equal to 1 when it's initialised with a value of 1?
Did you intend to pre-increment i?
That would be:
for (i=1; ++1 <= 1; i++)
But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.
If you do
for (i=1; ++i <= 1; i++)
i will be > 1 on the first iteration and nothing will happen.
add a comment |
As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
How can i never be less than or equal to 1 when it's initialised with a value of 1?
Did you intend to pre-increment i?
That would be:
for (i=1; ++1 <= 1; i++)
But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.
If you do
for (i=1; ++i <= 1; i++)
i will be > 1 on the first iteration and nothing will happen.
add a comment |
As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
How can i never be less than or equal to 1 when it's initialised with a value of 1?
Did you intend to pre-increment i?
That would be:
for (i=1; ++1 <= 1; i++)
But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.
If you do
for (i=1; ++i <= 1; i++)
i will be > 1 on the first iteration and nothing will happen.
As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?
How can i never be less than or equal to 1 when it's initialised with a value of 1?
Did you intend to pre-increment i?
That would be:
for (i=1; ++1 <= 1; i++)
But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.
If you do
for (i=1; ++i <= 1; i++)
i will be > 1 on the first iteration and nothing will happen.
answered Nov 22 '18 at 4:58
NunchyNunchy
815411
815411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53424031%2fhow-the-answer-of-this-for-loop-is-5%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
Possible duplicate of Undefined behavior and sequence points
– Ken White
Nov 22 '18 at 4:47
The proper declarations for
mainareint main (void)andint main (int argc, char **argv)(which you will see written with the equivalentchar *argv). note:mainis a function oftype intand it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?– David C. Rankin
Nov 22 '18 at 4:51
@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?
– Lundin
Nov 22 '18 at 7:44
Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.
– Gerhardh
Nov 22 '18 at 8:24
@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??
– David C. Rankin
Nov 22 '18 at 18:29