Difference between revisions of "C/prefix and postfix"
From ATI public wiki
(C operator pre and postfix form difference) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | 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: [[:File:pre_post.zip | pre_post.zip ]] , source example below | ||
<source lang="c"> | <source lang="c"> | ||
Line 9: | Line 22: | ||
#include <stdio.h> | #include <stdio.h> | ||
− | int main(int argc, char * argv | + | int main(int argc, char **argv) |
{ | { | ||
int i,j; | int i,j; |
Latest revision as of 18:00, 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;
}