将1加到给定的数字上?
将1加到给定数字的程序会将变量的值加1。这通常在计数器中使用。
有2种增加方法,可用于将给定数增加1-
将数字简单加一,然后将其重新分配给变量。
在程序中使用增量运算符。
方法1-使用重新分配方法
此方法采用变量,将其加1,然后重新分配其值。
范例程式码
#include <stdio.h> int main(void) { int n = 12; printf("The initial value of number n is %d \n", n); n = n+ 1; printf("The value after adding 1 to the number n is %d", n); return 0; }
输出结果
The initial value of number n is 12 The value after adding 1 to the number n is 13
方法2-使用增量运算符
此方法使用增量运算符将1加到给定的数字上。这是将数字加1的简便方法。
范例程式码
#include <stdio.h> int main(void) { int n = 12; printf("The initial value of number n is %d \n", n); n++; printf("The value after adding 1 to the number n is %d", n); return 0; }
输出结果
The initial value of number n is 12 The value after adding 1 to the number n is 13