C - Use Unary Minus Operator

Introduction

Put negative sign - in front of the variable name.

To output the amount you had spent as a negative value, you could write the following:

int expenditure = 75;
printf("Your balance has changed by %d.", -expenditure);

The minus sign will remind you that you've spent this money rather than gained it.

-expenditure doesn't change the value stored in expenditure-it's still 75. The value of the expression is -75.

Demo

#include <stdio.h>

int main(void)
{
    int expenditure = 75;
    printf("Your balance has changed by %d.", -expenditure);
    return 0;//from   ww  w .  j  av  a 2  s . c om
}

Result

Related Topic