The C postfix, prefix operators and the whitespaces effects program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: The increment and decrement by one using C postfix and prefix operators and to see the effect of whitespace

To show: The C postfix, prefix operators and whitespaces effects program example

 

 

 

/* The whitespace and unary operators prefix and postfix modes */#include <stdio>

int main(void){int a, b; a = b = 5;printf("postfix mode and prefix mode example\n"); printf("initial value, a = b = 5\n"); printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);

/* Some comment here */printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);printf("\n");return 0;}

 

Output example:

 

postfix mode and prefix mode example

initial value, a = b = 5

postfix mode, a-- = 5 prefix mode, --b = 4

postfix mode, a-- = 4 prefix mode, --b = 3

postfix mode, a-- = 3 prefix mode, --b = 2

postfix mode, a-- = 2 prefix mode, --b = 1

postfix mode, a-- = 1 prefix mode, --b = 0

Press any key to continue . . .

 

 

Another program example

 

 

/* The C unary operators prefix and postfix modes program example */

#include <stdio.h>

 

int main(void)

{

int a, b;

/* set a and b both equal to 5 */

a = b = 5;

 

/* print them, decrementing each time */

/* use prefix mode for variable b, postfix mode for variable a */

printf("postfix mode and prefix mode example\n");

printf("initial value, a = b = 5\n");

printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);

printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);

printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);

printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);

printf("\npostfix mode, a-- = %d prefix mode, --b = %d", a--, --b);

printf("\n");

 

return 0;

}

 

Output example:

 

postfix mode and prefix mode example

initial value, a = b = 5

postfix mode, a-- = 5 prefix mode, --b = 4

postfix mode, a-- = 4 prefix mode, --b = 3

postfix mode, a-- = 3 prefix mode, --b = 2

postfix mode, a-- = 2 prefix mode, --b = 1

postfix mode, a-- = 1 prefix mode, --b = 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index