易百教程

一元减运算符

把负号放在变量名前面。要将花费的金额输出为负值,编写以下代码:

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

减号会提醒这是花费了这笔钱,而不是获得这笔钱。-符号不会改变存储在expenditure中的值 - 它仍然是75,表达式的值是-75

#include <stdio.h>

int main(void)
{
    int expenditure = 75;
    printf("Your balance has changed by %d.\\n", -expenditure);
    return 0;//
}

执行上面示例代码,得到以下结果:

hema@ubuntu:~/book$ gcc -o main *.c
hema@ubuntu:~/book$ ./main
Your balance has changed by -75.