Monday, April 1, 2013

The (-) unary operator

A program showing simple uses of the unary minus operator.

/* The (-)unary operator */
#include <stdio.h> int main (void) { int a, b, c; a = 10; b = 20; /* b is 20 so -b is -20 */ b = -b + a; /* with multiple similar unary operators, use parentheses */ /* do you understand why c is 30? */ c = -b - (-a) + -b; printf ("The value of b is %d ", b); printf ("and the value of c is %d\n", c); return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
The value of b is -10 and the value of c is 30

No comments:

Post a Comment