null
运算符是任何编程语言的基础。因此,如果不使用运算符,C语言的功能是不完整的。运算符允许我们对操作数执行不同类型的操作。在C中,中的运算符可以分为以下类别:
- 算术运算符 s(+、-、*、/、%,增量后、增量前、减量后、减量前)
- 关系运算符 (==,!=,>,=&<=)逻辑运算符(&&,| |和!)
- 位运算符 (&,|,^,~,>>和<
- 赋值运算符 s(,+=,-=,*=,等等)
- 其他运营商 (条件、逗号、sizeof、地址、重定向)
算术运算符: 它们用于对操作数执行算术/数学运算。这类二进制运算符包括:
- 补充: 这个 ‘+’ 运算符将两个操作数相加。例如 x+y .
- 减法: 这个 ‘-‘ 运算符减去两个操作数。例如 x-y .
- 乘法: 这个 ‘*’ 运算符将两个操作数相乘。例如 x*y .
- 部门: 这个 ‘/’ 运算符将第一个操作数除以第二个操作数。例如 x/y .
- 模数: 这个 ‘%’ 当第一个操作数除以第二个操作数时,运算符返回余数。例如 x%y .
C
// C program to demonstrate // working of binary arithmetic // operators #include <stdio.h> int main() { int a = 10, b = 4, res; // printing a and b printf ( "a is %d and b is %d" , a, b); res = a + b; // addition printf ( "a+b is %d" , res); res = a - b; // subtraction printf ( "a-b is %d" , res); res = a * b; // multiplication printf ( "a*b is %d" , res); res = a / b; // division printf ( "a/b is %d" , res); res = a % b; // modulus printf ( "a%b is %d" , res); return 0; } |
C++
#include <iostream> using namespace std; int main() { int a = 10, b = 4, res; // printing a and b cout<< "a is " <<a<< " and b is " <<b<< "" ; // addition res = a + b; cout << "a+b is: " << res << "" ; // subtraction res = a - b; cout << "a-b is: " << res << "" ; // multiplication res = a * b; cout << "a*b is: " << res << "" ; // division res = a / b; cout << "a/b is: " << res << "" ; // modulus res = a % b; cout << "a%b is: " << res << "" ; return 0; } |
输出:
a is 10 and b is: 4a+b is: 14a-b is: 6a*b is: 40a/b is: 2a%b is: 2
属于一元算术运算符的有:
- 增量: 这个 ‘++’ 运算符用于增加整数的值。当放置在变量名(也称为预增量运算符)之前时,其值立即递增。例如 ++x . 当它放在变量名(也称为增量后运算符)之后时,它的值会暂时保留,直到执行此语句,并在执行下一个语句之前更新。例如 x++ .
- 减量: 这个 ‘ – – ‘ 运算符用于减少整数的值。当放置在变量名(也称为预减量运算符)之前时,其值会立即减量。例如 ––x . 当它被放置在变量名(也称为后减量运算符)之后时,它的值会暂时保留,直到执行此语句,并且在执行下一个语句之前会得到更新。例如 x– .
C
// C program to demonstrate working // of Unary arithmetic // operators #include <stdio.h> int main() { int a = 10, b = 4, res; // post-increment example: // res is assigned 10 only, a is not updated yet res = a++; printf ( "a is %d and res is %d" , a, res); // a becomes 11 now // post-decrement example: // res is assigned 11 only, a is not updated yet res = a--; printf ( "a is %d and res is %d" , a, res); // a becomes 10 now // pre-increment example: // res is assigned 11 now since // a is updated here itself res = ++a; // a and res have same values = 11 printf ( "a is %d and res is %d" , a, res); // pre-decrement example: // res is assigned 10 only since a is updated here // itself res = --a; // a and res have same values = 10 printf ( "a is %d and res is %d" , a, res); return 0; } |
C++
#include <iostream> using namespace std; int main() { int a = 10, b = 4, res; // post-increment example: // res is assigned 10 only, // a is not updated yet res = a++; // a becomes 11 now cout << "a is " << a << " and res is " << res << "" ; // post-decrement example: // res is assigned 11 only, // a is not updated yet res = a--; // a becomes 10 now cout << "a is " << a << " and res is " << res << "" ; // pre-increment example: // res is assigned 11 now // since a is updated here itself res = ++a; // a and res have same values = 11 cout << "a is " << a << " and res is " << res << "" ; // pre-decrement example: // res is assigned 10 only // since a is updated here // itself res = --a; // a and res have same values = 10 cout << "a is " << a << " and res is " << res << "" ; return 0; } |
输出:
a is 11 and res is 10a is 10 and res is 11a is 11 and res is 11a is 10 and res is 10
我们将很快讨论不同岗位的其他类别的运营商。 了解 运算符优先级与结合性 参考 这 链接: C语言中的运算符测试 本文由Ayush Jaggi撰稿。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END