C 语言增量和减量运算符2025年3月17日 | 阅读 7 分钟 运算符是C/C++库中的预定义符号,用于对操作数执行逻辑和数学运算。C编程语言中有多种运算符,例如算术运算符、逻辑运算符、位运算符、自增或自减运算符等。 ![]() 自增运算符自增运算符是用于将操作数的值增加1的一元运算符。自增运算符用双加号(++)表示。它有两种类型:前缀自增运算符和后缀自增运算符。 前缀自增运算符前缀自增运算符用于在将操作数的值赋给表达式之前,将其增加1。 语法 在上面的语法中,操作数'A'的值增加了1,然后将新值赋给变量'B'。 示例1:在C中使用前缀自增运算符的程序 输出 Input the value of X: 10 Input the value of Y: 15 Input the value of Z: 20 The updated value of the X: 11 The updated value of the Y: 16 The updated value of the Z: 21 后缀自增运算符后缀自增运算符用于在将操作数的值赋给表达式之后,将其增加1。 语法 在上面的语法中,操作数'A'的值赋给了变量'X'。之后,变量'A'的值增加了1。 示例2:在C中使用后缀自增运算符的程序 输出 Input the value of X: 10 Input the value of Y: 15 Input the value of Z: 20 The original value of a: 10 The original value of b: 15 The original value of c: 20 The updated value of the X: 11 The updated value of the Y: 16 The updated value of the Z: 21 自减运算符自减运算符是一元运算符,用于将操作数的值减少1。自减运算符用双减号(--)表示。它有两种类型:前缀自减运算符和后缀自减运算符。 前缀自减运算符前缀自减运算符在将操作数的值赋给数学表达式之前,将其减少1。换句话说,操作数的值首先减1,然后将新值赋给另一个变量。 语法 在上面的语法中,操作数'A'的值减少了1,然后将新值赋给变量'B'。 示例3:演示C中前缀自减运算符的程序 输出 Input the value of X: 5 Input the value of Y: 6 Input the value of Z: 7 The updated value of the X: 6 The updated value of the Y: 7 The updated value of the Z: 8 后缀自减运算符后缀自减运算符用于在将操作数的值赋给表达式之后,将其减少1。 语法 在上面的语法中,操作数'A'的值赋给了变量'B',然后'A'的值减少了1。 示例4:在C中使用后缀自减运算符的程序 输出 Input the value of X: 6 Input the value of Y: 12 Input the value of Z: 18 The original value of a: 6 The original value of b: 12 The original value of c: 18 The updated value of the X: 5 The updated value of the Y: 11 The updated value of the Z: 17 示例5:执行前缀自增和前缀自减运算符的程序 输出 Enter the value of i 5 Enter the value of j 10 After using the pre-incrementing, the value of i is 6 The value of x is 6 After using the pre-decrementing, the value of j is 9 The value of y is 9 示例6:打印后缀自增和后缀自减运算符的程序 输出 Enter the value of i 10 Enter the value of j 20 After using the post-incrementing, the value of i is 11 The value of x is 10 After using the post-decrementing, the value of j is 19 The value of y is 20 C中自增运算符和自减运算符的区别
下一主题C中的逻辑AND运算符 |
我们请求您订阅我们的新闻通讯以获取最新更新。