연산자는 특정 수학적 또는 논리적 조작을 수행하도록 컴파일러에 지시하는 기호입니다.
| 연산자 | 설명 | 예 |
|---|---|---|
| + | 두 개의 피연산자를 추가합니다. | A + B =30 |
| - | 첫 번째 피연산자에서 두 번째 피연산자를 뺍니다. | A - B =-10 |
| * | 두 피연산자를 곱합니다. | A * B =200 |
| / | 분자를 분자로 나누기 | B / A =2 |
| % | 정수 나누기 후 모듈러스 연산자 및 나머지 | B % A =0 |
| ++ | 증가 연산자는 정수 값을 1 증가시킵니다. | A++ =11 |
| -- | 감소 연산자는 정수 값을 1 감소시킵니다. | A-- =9 |
C#에서 산술 연산자를 사용하는 예를 살펴보겠습니다.
예시
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int a = 99;
int b = 33;
int c;
c = a + b;
Console.WriteLine("Value of c is {0}", c);
c = a - b;
Console.WriteLine("Value of c is {0}", c);
c = a * b;
Console.WriteLine("Value of c is {0}", c);
c = a / b;
Console.WriteLine("Value of c is {0}", c);
c = a % b;
Console.WriteLine("Value of c is {0}", c);
c = a++;
Console.WriteLine("Value of c is {0}", c);
c = a--;
Console.WriteLine("Value of c is {0}", c);
Console.ReadLine();
}
}
} 출력
Value of c is 132 Value of c is 66 Value of c is 3267 Value of c is 3 Value of c is 0 Value of c is 99 Value of c is 100