Difference between revisions of "C/prefix and postfix"

From ATI public wiki
Jump to: navigation, search
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
+
C increment ++ and decrement -- operators give in the end same value change to variable whether used in prefix or postfix form
  
 
prefix form:
 
prefix form:
Line 12: Line 12:
  
  
downloadable file here: [[:File:pre_post.zip]] , source example below
+
downloadable file here: [[:File:pre_post.zip | pre_post.zip ]] , source example below
  
 
<source lang="c">
 
<source lang="c">

Revision as of 17:57, 7 October 2015

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