c - Increment and Decrement Operators -
#include <stdio.h> int main() { int x = 4, y, z; y = --x; z = x--; printf("%d %d %d", x, y, z); }
output: 2 3 3
can explain this?
, i =+ j
mean (suppose i = 1
, j = 2
)?
simple explanation:
--x or ++x : value modified after.
x-- or x++ : value modified before
detailed explanation:
--x or ++x: pre-decrement/increment: first operation of decrementing or incrementing first, assign x.
x-- or x++: post:decrement/increment: first assign value of x , operation of decrementing or incrementing after.
lets write code in nicer format , go through code step step , annotate show visually happens:
main() { //we declare variables x, y , z, x given value of 4. int x=4,y,z; //--x decrement var x 1 first assign value of x y. //so: x = 3, y = 3 , z = nothing yet. y = --x; //x-- assign value of x z first, var x decremented 1 after. //so: x = 2, y=3 , z = 3 z = x--; printf ("\n %d %d %d", x,y,z); }
Comments
Post a Comment