Difference between i++ and (i)++ in CWhat is the difference between #include <filename> and #include...
Why zero tolerance on nudity in space?
What is the most fuel efficient way out of the Solar System?
Do authors have to be politically correct in article-writing?
Which communication protocol is used in AdLib sound card?
How can I get my players to come to the game session after agreeing to a date?
What is the purpose of easy combat scenarios that don't need resource expenditure?
Is Krishna the only avatar among dashavatara who had more than one wife?
Cookies - Should the toggles be on?
Is it possible to grant users sftp access without shell access? If yes, how is it implemented?
In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?
Difference between i++ and (i)++ in C
Absorbing damage with Planeswalker
Finding a logistic regression model which can achieve zero error on a training set training data for a binary classification problem with two features
How can my powered armor quickly replace its ceramic plates?
Why publish a research paper when a blog post or a lecture slide can have more citation count than a journal paper?
Consequences of lack of rigour
What sets the resolution of an analog resistive sensor?
Why did Democrats in the Senate oppose the Born-Alive Abortion Survivors Protection Act (2019 S.130)?
What is a good reason for every spaceship to carry a weapon on board?
Is there any risk in sharing info about technologies and products we use with a supplier?
Is there a Linux system call to create a “view” of a range of a file?
A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?
How do I draw the dashed lines as shown in this figure
Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?
Difference between i++ and (i)++ in C
What is the difference between #include <filename> and #include “filename”?What is the difference between ++i and i++?What is the difference between const int*, const int * const, and int const *?What is the difference between a definition and a declaration?Difference between malloc and calloc?Improve INSERT-per-second performance of SQLite?Difference between static and shared libraries?How do I achieve the theoretical maximum of 4 FLOPs per cycle?Does C make a difference between compiling and executing a program?Space as a token its impact on evaluation of binary or unary operator
int i = 3;
int j = (i)++;
vs
int i = 3;
int j = i ++;
Though both of the above examples store 3 in j
, is there a difference between how the above two cases are evaluated?
Since i
is an int
, do the ()
cause the first case to be evaluated as an expression, which would be equivalent to incrementing an rvalue? Or is it undefined behaviour and just happens to store 3 in j
?
Or am I overthinking it and its just a simple postfix?
c
add a comment |
int i = 3;
int j = (i)++;
vs
int i = 3;
int j = i ++;
Though both of the above examples store 3 in j
, is there a difference between how the above two cases are evaluated?
Since i
is an int
, do the ()
cause the first case to be evaluated as an expression, which would be equivalent to incrementing an rvalue? Or is it undefined behaviour and just happens to store 3 in j
?
Or am I overthinking it and its just a simple postfix?
c
2
Seemingly arbitrary usage of parentheses is common in macro definitions. Where they do make a big difference, you'd like the error message you get. Well, usually.
– Hans Passant
4 hours ago
add a comment |
int i = 3;
int j = (i)++;
vs
int i = 3;
int j = i ++;
Though both of the above examples store 3 in j
, is there a difference between how the above two cases are evaluated?
Since i
is an int
, do the ()
cause the first case to be evaluated as an expression, which would be equivalent to incrementing an rvalue? Or is it undefined behaviour and just happens to store 3 in j
?
Or am I overthinking it and its just a simple postfix?
c
int i = 3;
int j = (i)++;
vs
int i = 3;
int j = i ++;
Though both of the above examples store 3 in j
, is there a difference between how the above two cases are evaluated?
Since i
is an int
, do the ()
cause the first case to be evaluated as an expression, which would be equivalent to incrementing an rvalue? Or is it undefined behaviour and just happens to store 3 in j
?
Or am I overthinking it and its just a simple postfix?
c
c
edited 5 hours ago
Richard Chambers
10k24167
10k24167
asked 5 hours ago
Polaris000Polaris000
131412
131412
2
Seemingly arbitrary usage of parentheses is common in macro definitions. Where they do make a big difference, you'd like the error message you get. Well, usually.
– Hans Passant
4 hours ago
add a comment |
2
Seemingly arbitrary usage of parentheses is common in macro definitions. Where they do make a big difference, you'd like the error message you get. Well, usually.
– Hans Passant
4 hours ago
2
2
Seemingly arbitrary usage of parentheses is common in macro definitions. Where they do make a big difference, you'd like the error message you get. Well, usually.
– Hans Passant
4 hours ago
Seemingly arbitrary usage of parentheses is common in macro definitions. Where they do make a big difference, you'd like the error message you get. Well, usually.
– Hans Passant
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
i++
and (i)++
behave identically. C 2018 6.5.1 5 says:
A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.
add a comment |
In your simple example of i++
versus (i)++
, there is no difference, as noted in Eric Postpischil's answer.
However, this difference is actually meaningful if you are dereferencing a pointer variable with the *
operator and using the increment operator; there is a difference between *p++
and (*p)++
.
The former statement dereferences the pointer and then increments the pointer itself; the latter statement dereferences the pointer then increments the dereferenced value.
1
@downvoter: reason?
– Govind Parmar
5 hours ago
1
@SerG In my experience this particular nuance leads to a very common error by beginners, so I thought it was worth mentioning here.
– Govind Parmar
5 hours ago
An attempt to make information available through denormalization instead of structuration is a controversial way.
– SerG
5 hours ago
1
"increments the pointer and then dereferences it" may be confusing, since you're using postincrement -- I would suggest "dereferences the pointer and then increments the pointer".
– zwol
5 hours ago
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%2f54908480%2fdifference-between-i-and-i-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
i++
and (i)++
behave identically. C 2018 6.5.1 5 says:
A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.
add a comment |
i++
and (i)++
behave identically. C 2018 6.5.1 5 says:
A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.
add a comment |
i++
and (i)++
behave identically. C 2018 6.5.1 5 says:
A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.
i++
and (i)++
behave identically. C 2018 6.5.1 5 says:
A parenthesized expression is a primary expression. Its type and value are identical to those of the unparenthesized expression. It is an lvalue, a function designator, or a void expression if the unparenthesized expression is, respectively, an lvalue, a function designator, or a void expression.
answered 5 hours ago
Eric PostpischilEric Postpischil
77.3k881164
77.3k881164
add a comment |
add a comment |
In your simple example of i++
versus (i)++
, there is no difference, as noted in Eric Postpischil's answer.
However, this difference is actually meaningful if you are dereferencing a pointer variable with the *
operator and using the increment operator; there is a difference between *p++
and (*p)++
.
The former statement dereferences the pointer and then increments the pointer itself; the latter statement dereferences the pointer then increments the dereferenced value.
1
@downvoter: reason?
– Govind Parmar
5 hours ago
1
@SerG In my experience this particular nuance leads to a very common error by beginners, so I thought it was worth mentioning here.
– Govind Parmar
5 hours ago
An attempt to make information available through denormalization instead of structuration is a controversial way.
– SerG
5 hours ago
1
"increments the pointer and then dereferences it" may be confusing, since you're using postincrement -- I would suggest "dereferences the pointer and then increments the pointer".
– zwol
5 hours ago
add a comment |
In your simple example of i++
versus (i)++
, there is no difference, as noted in Eric Postpischil's answer.
However, this difference is actually meaningful if you are dereferencing a pointer variable with the *
operator and using the increment operator; there is a difference between *p++
and (*p)++
.
The former statement dereferences the pointer and then increments the pointer itself; the latter statement dereferences the pointer then increments the dereferenced value.
1
@downvoter: reason?
– Govind Parmar
5 hours ago
1
@SerG In my experience this particular nuance leads to a very common error by beginners, so I thought it was worth mentioning here.
– Govind Parmar
5 hours ago
An attempt to make information available through denormalization instead of structuration is a controversial way.
– SerG
5 hours ago
1
"increments the pointer and then dereferences it" may be confusing, since you're using postincrement -- I would suggest "dereferences the pointer and then increments the pointer".
– zwol
5 hours ago
add a comment |
In your simple example of i++
versus (i)++
, there is no difference, as noted in Eric Postpischil's answer.
However, this difference is actually meaningful if you are dereferencing a pointer variable with the *
operator and using the increment operator; there is a difference between *p++
and (*p)++
.
The former statement dereferences the pointer and then increments the pointer itself; the latter statement dereferences the pointer then increments the dereferenced value.
In your simple example of i++
versus (i)++
, there is no difference, as noted in Eric Postpischil's answer.
However, this difference is actually meaningful if you are dereferencing a pointer variable with the *
operator and using the increment operator; there is a difference between *p++
and (*p)++
.
The former statement dereferences the pointer and then increments the pointer itself; the latter statement dereferences the pointer then increments the dereferenced value.
edited 4 hours ago
answered 5 hours ago
Govind ParmarGovind Parmar
11.8k53361
11.8k53361
1
@downvoter: reason?
– Govind Parmar
5 hours ago
1
@SerG In my experience this particular nuance leads to a very common error by beginners, so I thought it was worth mentioning here.
– Govind Parmar
5 hours ago
An attempt to make information available through denormalization instead of structuration is a controversial way.
– SerG
5 hours ago
1
"increments the pointer and then dereferences it" may be confusing, since you're using postincrement -- I would suggest "dereferences the pointer and then increments the pointer".
– zwol
5 hours ago
add a comment |
1
@downvoter: reason?
– Govind Parmar
5 hours ago
1
@SerG In my experience this particular nuance leads to a very common error by beginners, so I thought it was worth mentioning here.
– Govind Parmar
5 hours ago
An attempt to make information available through denormalization instead of structuration is a controversial way.
– SerG
5 hours ago
1
"increments the pointer and then dereferences it" may be confusing, since you're using postincrement -- I would suggest "dereferences the pointer and then increments the pointer".
– zwol
5 hours ago
1
1
@downvoter: reason?
– Govind Parmar
5 hours ago
@downvoter: reason?
– Govind Parmar
5 hours ago
1
1
@SerG In my experience this particular nuance leads to a very common error by beginners, so I thought it was worth mentioning here.
– Govind Parmar
5 hours ago
@SerG In my experience this particular nuance leads to a very common error by beginners, so I thought it was worth mentioning here.
– Govind Parmar
5 hours ago
An attempt to make information available through denormalization instead of structuration is a controversial way.
– SerG
5 hours ago
An attempt to make information available through denormalization instead of structuration is a controversial way.
– SerG
5 hours ago
1
1
"increments the pointer and then dereferences it" may be confusing, since you're using postincrement -- I would suggest "dereferences the pointer and then increments the pointer".
– zwol
5 hours ago
"increments the pointer and then dereferences it" may be confusing, since you're using postincrement -- I would suggest "dereferences the pointer and then increments the pointer".
– zwol
5 hours ago
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%2f54908480%2fdifference-between-i-and-i-in-c%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
2
Seemingly arbitrary usage of parentheses is common in macro definitions. Where they do make a big difference, you'd like the error message you get. Well, usually.
– Hans Passant
4 hours ago