0
#include <stdio.h>
int main(){
static int a = 9;
if (a--){
   printf("after decrement a =%d\n", a);
   main();
   }
  return 0;
}

Output:

after decrement a =8
after decrement a =7
after decrement a =6
after decrement a =5
after decrement a =4
after decrement a =3
after decrement a =2
after decrement a =1
after decrement a =0

here prints up to a=0

when using if(--a)

output:

after decrement a =8
after decrement a =7
after decrement a =6
after decrement a =5
after decrement a =4
after decrement a =3
after decrement a =2
after decrement a =1

here prints up to a=1

So, the question is when using if(a--) it prints up to 0 and when using if(--a) it prints up to 1, how when using pre decrement , the output starts from 8 rather starting from 9.

mch
  • 9,424
  • 2
  • 28
  • 42
Syed
  • 1
  • While C doesn't forbid it, it's really bad behavior to call `main` recursively. If you want to do this experiment using recursion use a separate function that you call from `main`. – Some programmer dude Sep 02 '22 at 06:14
  • 3
    As for the difference between prefix and suffix `--` (and `++` of course) that should have been explained by any decent book, tutorial or class. – Some programmer dude Sep 02 '22 at 06:15
  • The difference is not in _what values get processed in a loop body_ but rathe in _when the loop terminates_. You may want to use a debugger and see step-by-step how the loop is executed. You may also try to initialize `a=1` and see what happens. And the results of setting `a=0` may be even more interesting for you... – CiaPan Sep 02 '22 at 07:29
  • BTW, are you sure you know why you make the `a` variable `static`...? – CiaPan Sep 02 '22 at 07:29

2 Answers2

1

This is about the post- and pre- in/decrement operators as documented at https://en.cppreference.com/w/cpp/language/operator_incdec.

With --a, the -- is executed PRIOR to returning the reference to the variable to the calculation, meaning it will basically start decremented.

With a--, on the other hand, the program caches the previous value of a, decrements it, and then returns the cached value.

So:

int a = 5;
printf("%d\n", --a); //Prints 4
printf("%d\n", a--); //Prints 4
printf("%d\n", a); //Prints 3
Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24
  • 1
    "This is about 'operator precedence'." No it isn't. It is about the behavior of post increment versus pre increment. – Lundin Sep 02 '22 at 06:26
  • @Lundin You are, of course, correct. I amended the introductory statement. – Refugnic Eternium Sep 02 '22 at 06:33
  • @Lundin Actually, there are two questions in the OP. One about why the output terminates at 0 v. 1, and another about why the both outputs starts with 8, not 9... Tricky... – Fe2O3 Sep 02 '22 at 07:49
0

OP: "how when using pre decrement , the output starts from 8 rather starting from 9"

In both cases, pre- and post- decrement, the variable is decremented in the if( ) conditional before the value of the variable is printed in the printf( ).

user438383
  • 5,716
  • 8
  • 28
  • 43
Fe2O3
  • 6,077
  • 2
  • 4
  • 20