#include <stdio.h>
int main() {
int x = 3;
while (x > 0, x--) {
printf("positive");
}
printf("%d", & x);
return 0;
}
output - positivepositivepositive1472586508
how I got these numbers in output
CodePudding user response:
what you are trying to do is, printing the value of x
after some iteration, fixed code is:
#include <stdio.h>
int main() {
int x=3;
while (x>0)
{ printf("positive");
x--;
}
printf("%d",x);
return 0;
}
Note:
- formatting is important this would help future coders to understand easily.
&
-- denotes the address of any variable, while you printing the value ofx
with the ampersand operator, you actually denotes its address.
CodePudding user response:
You got that number because you were printing memory address of that variable.when &
used before any variable that represent address of that variable.In case of printf()
dont use it just use variable name.
#include <stdio.h>
int main() {
int x=3;
while (x>0,x--)
{
printf("positive");
}
printf("%d",x);
return 0;
}
CodePudding user response:
int main() {
int x = 3;
while (x > 0) {
printf("positive");
x--;
}
I little bit edit your code but output is same. up to this point output is "positivepositivepositive" after that you are trying to print memory address of the x variable. that's why output is undefined number. if you use printf statement to print variable value with & sign it always give the memory address of that variable.
printf("%d", & x);
using & sign this part give memory address of the x variable.
printf("%d", x);
if you use this without & sign it give the value of x variable. (x=0)
CodePudding user response:
The code as it is
#include <stdio.h>
int main() {
int x = 3;
while (x > 0, x--) {
printf("positive");
}
printf("%d", & x);
return 0;
}
is a bit strange:
the expression x>0, x--
is the same as x--
The comma here is an operator, the sequence operator. An here's what it means:
- the first expression is executed
x > 0
, resulting in a number, 0 or 1, and this result is discarded. Since the expression has no other effects it means nothing, but with a delay ;) . - then the second expression
x--
is evaluated, and its result is used to evaluate thewhile
condition. So thewhile
will break whenx
is0
a.k.a.false
inC
. - The expression has a post-decrement operator so it is always executed, leaving
x
as-1
at the lastprintf
A sequence e = a,b,c,d;
in C means that all expressions are evaluated, but only the result of the last is considered as the result of the expression
Sure, as noted by others, the &
is misplaced at the original code.
The code is the same as
#include <stdio.h>
int main(void)
{
int x = 3;
while (x--) printf("positive\n");
printf("%d", x);
return 0;
}
that shows
positive
positive
positive
-1
more about sequence operations
#include <stdio.h>
int main(void)
{
int x = 3;
while (x--) printf("positive\n");
printf(" [1] %d\n", x);
x = x>0, x<0, x 2, x--;
printf(" [2] %d\n", x);
x = x>0, x<0, 1;
printf(" [3] %d\n", x);
x = 0, x<0, --x;
printf(" [4] %d, ", x);
if ( x = 0, x<0, x-- )
printf("true\n");
else
printf("false\n");
printf(" [5] %d\n", x);
x = 11,2,3,4,5,6,0;
printf(" [6] %d\n", x);
return 0;
}
shows
positive
positive
positive
[1] -1
[2] -1
[3] 0
[4] -1, false
[5] -1
[6] 11
and shows a few cases of the comma operator --- not the same as the comma separator as in arguments or declarations