Difference between revisions of "C/prefix and postfix"

From ATI public wiki
Jump to: navigation, search
(C operator pre and postfix form difference)
 
Line 1: Line 1:
 +
C increment ++ and decrement -- operators give in the end same value change to variable, either used in prefix or postfix forms
  
 +
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: [[:File:pre_post.zip]] , source example below
  
 
<source lang="c">
 
<source lang="c">

Revision as of 17:56, 7 October 2015

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

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: File: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;
 }