C/prefix and postfix

From ATI public wiki
Jump to: navigation, search

C increment ++ and decrement -- operators give in the end same value change to variable whether used in prefix or postfix form

prefix form:

 --var;
 ++var;

postfix form:

 var--;
 var++;

however when used within functions as argument operators there is a big difference, the example below demonstrates it


downloadable file here: pre_post.zip , source example below

/*
 This example demonstrates the difference in using
 prefix or postfix form of increment and decrement operators
*/

#include <stdio.h>

int main(int argc, char **argv)
 {
  int i,j;

  i=15;
  j=15;

  printf("I= %d , J= %d \n\r", i, j);
  printf("-------------\n\r");
  printf("I= %d , J= %d \n\r", ++i, j++);
  printf("-------------\n\r");
  printf("I= %d , J= %d \n\r", i, j);

  printf("=============\n\r");

  printf("I= %d , J= %d \n\r", i, j);
  printf("-------------\n\r");
  printf("I= %d , J= %d \n\r", --i, j--);
  printf("-------------\n\r");
  printf("I= %d , J= %d \n\r", i, j);

  return 0;
 }