What is happening here while post/pre decrementing a char variable in C [duplicate]
This question already has an answer here:
What is the difference between ++i and i++?
20 answers
I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.
Code 1
char i=0;
do
{
printf("%d ",i);
}while(i--);
return 0;
The output for this snippet is 0.
Code 2
char i=0;
do
{
printf("%d ",i);
}while(--i);
return 0;
The output is for this one is
0,-1,-2,.....-128,127,126,......1 .
Can anyone explain why this is happening?
c char
marked as duplicate by Mitch Wheat, n.m., Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 7:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is the difference between ++i and i++?
20 answers
I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.
Code 1
char i=0;
do
{
printf("%d ",i);
}while(i--);
return 0;
The output for this snippet is 0.
Code 2
char i=0;
do
{
printf("%d ",i);
}while(--i);
return 0;
The output is for this one is
0,-1,-2,.....-128,127,126,......1 .
Can anyone explain why this is happening?
c char
marked as duplicate by Mitch Wheat, n.m., Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 7:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What is the difference between ++i and i++?
20 answers
I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.
Code 1
char i=0;
do
{
printf("%d ",i);
}while(i--);
return 0;
The output for this snippet is 0.
Code 2
char i=0;
do
{
printf("%d ",i);
}while(--i);
return 0;
The output is for this one is
0,-1,-2,.....-128,127,126,......1 .
Can anyone explain why this is happening?
c char
This question already has an answer here:
What is the difference between ++i and i++?
20 answers
I was solving some multiple choice C codes from a book. Two of the questions involve pre decrementing, post decrementing a char variable initialised at 0. The output for both these is very different. I dont understand whats going on there.
Code 1
char i=0;
do
{
printf("%d ",i);
}while(i--);
return 0;
The output for this snippet is 0.
Code 2
char i=0;
do
{
printf("%d ",i);
}while(--i);
return 0;
The output is for this one is
0,-1,-2,.....-128,127,126,......1 .
Can anyone explain why this is happening?
This question already has an answer here:
What is the difference between ++i and i++?
20 answers
c char
c char
asked Nov 22 '18 at 6:47
Kanchana GoreKanchana Gore
11
11
marked as duplicate by Mitch Wheat, n.m., Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 7:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mitch Wheat, n.m., Lundin
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 7:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
At both code while
loop checking i==0
or not. If i!=0
it will keep going on.
At first code value of i
initially 0
. So after printing 0
it checks i==0
or not. if i==0
it will break the loop or keep going on by decrementing i
. So in code 1 post decrementing used. check value first then decrements the value.
At second code value of i
initially 0
. So after printing 0
it decrements i
then it checks if i==0
or not. it is pre decrement. decrement value first then check.
Here, i
is char
which size is 1 byte
and range -128 to 127
. So after decrementing value 0
to -1
it keep decrementing until it goes to 0
and exit the loop by printing 0,-1,...,-128,127...1 .
add a comment |
Code 1
char i=0;
do
{
printf("%d ",i); // print o
}while(i--); //check i = 0, means false, loop ends, then increment i
return 0;
Code 2
char i=0;
do
{
printf("%d ",i); //print 0
}while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
return 0;
add a comment |
Both i--
and --i
are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.
This is completely independent of whether the expression is used in a while
statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.
add a comment |
- Initial value of
i
is0
. - In Code 1, first
while
check happens in which the value ofi
(= 0
) is used and theni
is decremented because it is postfix decrement. So it exitswhile
after printing0
. - In Code 2, because it is prefix decrement,
i
is decremented first and its value (= -1
) is used when thewhile
check is performed. Here it exits after printing the entire range of values asigned char
can hold because it becomes0
at the end only.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
At both code while
loop checking i==0
or not. If i!=0
it will keep going on.
At first code value of i
initially 0
. So after printing 0
it checks i==0
or not. if i==0
it will break the loop or keep going on by decrementing i
. So in code 1 post decrementing used. check value first then decrements the value.
At second code value of i
initially 0
. So after printing 0
it decrements i
then it checks if i==0
or not. it is pre decrement. decrement value first then check.
Here, i
is char
which size is 1 byte
and range -128 to 127
. So after decrementing value 0
to -1
it keep decrementing until it goes to 0
and exit the loop by printing 0,-1,...,-128,127...1 .
add a comment |
At both code while
loop checking i==0
or not. If i!=0
it will keep going on.
At first code value of i
initially 0
. So after printing 0
it checks i==0
or not. if i==0
it will break the loop or keep going on by decrementing i
. So in code 1 post decrementing used. check value first then decrements the value.
At second code value of i
initially 0
. So after printing 0
it decrements i
then it checks if i==0
or not. it is pre decrement. decrement value first then check.
Here, i
is char
which size is 1 byte
and range -128 to 127
. So after decrementing value 0
to -1
it keep decrementing until it goes to 0
and exit the loop by printing 0,-1,...,-128,127...1 .
add a comment |
At both code while
loop checking i==0
or not. If i!=0
it will keep going on.
At first code value of i
initially 0
. So after printing 0
it checks i==0
or not. if i==0
it will break the loop or keep going on by decrementing i
. So in code 1 post decrementing used. check value first then decrements the value.
At second code value of i
initially 0
. So after printing 0
it decrements i
then it checks if i==0
or not. it is pre decrement. decrement value first then check.
Here, i
is char
which size is 1 byte
and range -128 to 127
. So after decrementing value 0
to -1
it keep decrementing until it goes to 0
and exit the loop by printing 0,-1,...,-128,127...1 .
At both code while
loop checking i==0
or not. If i!=0
it will keep going on.
At first code value of i
initially 0
. So after printing 0
it checks i==0
or not. if i==0
it will break the loop or keep going on by decrementing i
. So in code 1 post decrementing used. check value first then decrements the value.
At second code value of i
initially 0
. So after printing 0
it decrements i
then it checks if i==0
or not. it is pre decrement. decrement value first then check.
Here, i
is char
which size is 1 byte
and range -128 to 127
. So after decrementing value 0
to -1
it keep decrementing until it goes to 0
and exit the loop by printing 0,-1,...,-128,127...1 .
edited Nov 22 '18 at 7:35
answered Nov 22 '18 at 7:10
Sayed SohanSayed Sohan
659
659
add a comment |
add a comment |
Code 1
char i=0;
do
{
printf("%d ",i); // print o
}while(i--); //check i = 0, means false, loop ends, then increment i
return 0;
Code 2
char i=0;
do
{
printf("%d ",i); //print 0
}while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
return 0;
add a comment |
Code 1
char i=0;
do
{
printf("%d ",i); // print o
}while(i--); //check i = 0, means false, loop ends, then increment i
return 0;
Code 2
char i=0;
do
{
printf("%d ",i); //print 0
}while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
return 0;
add a comment |
Code 1
char i=0;
do
{
printf("%d ",i); // print o
}while(i--); //check i = 0, means false, loop ends, then increment i
return 0;
Code 2
char i=0;
do
{
printf("%d ",i); //print 0
}while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
return 0;
Code 1
char i=0;
do
{
printf("%d ",i); // print o
}while(i--); //check i = 0, means false, loop ends, then increment i
return 0;
Code 2
char i=0;
do
{
printf("%d ",i); //print 0
}while(--i); //decrement i, check i=-1, means true, next cycle, loop until i = 0 which means false
return 0;
answered Nov 22 '18 at 7:21
MikeMike
2,0051722
2,0051722
add a comment |
add a comment |
Both i--
and --i
are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.
This is completely independent of whether the expression is used in a while
statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.
add a comment |
Both i--
and --i
are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.
This is completely independent of whether the expression is used in a while
statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.
add a comment |
Both i--
and --i
are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.
This is completely independent of whether the expression is used in a while
statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.
Both i--
and --i
are expressions. An expression is (part of) a statement that can yield a value. As per definition, the pre-increment version increments first, then yields the value. For the post-increment version it is the other way round.
This is completely independent of whether the expression is used in a while
statement or elsewhere. However, when using such expressions, you need to be aware of operator precendence.
answered Nov 22 '18 at 7:23
GermanNerdGermanNerd
492111
492111
add a comment |
add a comment |
- Initial value of
i
is0
. - In Code 1, first
while
check happens in which the value ofi
(= 0
) is used and theni
is decremented because it is postfix decrement. So it exitswhile
after printing0
. - In Code 2, because it is prefix decrement,
i
is decremented first and its value (= -1
) is used when thewhile
check is performed. Here it exits after printing the entire range of values asigned char
can hold because it becomes0
at the end only.
add a comment |
- Initial value of
i
is0
. - In Code 1, first
while
check happens in which the value ofi
(= 0
) is used and theni
is decremented because it is postfix decrement. So it exitswhile
after printing0
. - In Code 2, because it is prefix decrement,
i
is decremented first and its value (= -1
) is used when thewhile
check is performed. Here it exits after printing the entire range of values asigned char
can hold because it becomes0
at the end only.
add a comment |
- Initial value of
i
is0
. - In Code 1, first
while
check happens in which the value ofi
(= 0
) is used and theni
is decremented because it is postfix decrement. So it exitswhile
after printing0
. - In Code 2, because it is prefix decrement,
i
is decremented first and its value (= -1
) is used when thewhile
check is performed. Here it exits after printing the entire range of values asigned char
can hold because it becomes0
at the end only.
- Initial value of
i
is0
. - In Code 1, first
while
check happens in which the value ofi
(= 0
) is used and theni
is decremented because it is postfix decrement. So it exitswhile
after printing0
. - In Code 2, because it is prefix decrement,
i
is decremented first and its value (= -1
) is used when thewhile
check is performed. Here it exits after printing the entire range of values asigned char
can hold because it becomes0
at the end only.
edited Nov 22 '18 at 7:35
answered Nov 22 '18 at 7:03
P.WP.W
12.8k3945
12.8k3945
add a comment |
add a comment |