연산자 (프로그래밍): 두 판 사이의 차이

위키백과, 우리 모두의 백과사전.
내용 삭제됨 내용 추가됨
편집 요약 없음
편집 요약 없음
1번째 줄: 1번째 줄:
[[프로그래밍 언어]]는 일반적으로 [[연산 (수학)|수학 연산]]과 유사한 '''연산자'''의 집합을 지원한다. 언어는 내장된 연산자의 정해진 숫자를 포함할 수 있다 (예: [[C와 C++에서 연산자|C와 C++]]에서 + - * =), 아니면 프로그래머가 정의한 연산자의 생성을 허용할 수 있다 (예: [[하스켈]]). 일부 프로그래밍 언어는 다른 <code>[[나눗셈#정수의 나눗셈|'''div''']]</code>와 같은 이름도 허용하는 동안 특수 문자 [[덧셈|'''+''']] 또는 [[대입 (컴퓨터 과학)|''':=''']] 과 같은 연산자 기호를 제한한다 (예: [[파스칼 (프로그래밍 언어)|파스칼]]).
[[프로그래밍 언어]]는 일반적으로 [[연산 (수학)|수학 연산]]과 유사한 '''연산자'''의 집합을 지원한다. 언어는 내장된 연산자의 정해진 숫자를 포함할 수 있다 (예: [[C와 C++에서 연산자|C와 C++]]에서 + - * =), 아니면 프로그래머가 정의한 연산자의 생성을 허용할 수 있다 (예: [[하스켈]]). 일부 프로그래밍 언어는 다른 <code>[[나눗셈#정수의 나눗셈|'''div''']]</code>와 같은 이름도 허용하는 동안 특수 문자 [[덧셈|'''+''']] 또는 [[대입 (컴퓨터 과학)|''':=''']] 과 같은 연산자 기호를 제한한다 (예: [[파스칼 (프로그래밍 언어)|파스칼]]).


언어에 의해 지원된 내장 연산자 일부는 일반적으로 [[중앙 처리 장치]]에 있는 [[기계어|명령]]의 작은 숫자에 직접 매핑을 하지만, 다른 것(''예'': '+'는 [[문자열 연결]]을 표현하기 위해 사용)은 복잡한 구현을 할 수 있다.
Some built-in operators supported by a language have a direct mapping to a small number of [[Machine code|instructions]] commonly found on [[central processing units]], though others (''e.g.'' '+' used to express [[string concatenation]]) may have complicated implementations.


언어의 사양은 [[연산의 우선순위|우선순위]]와 그것이 지원하는 연산자의 [[결합법칙]]을 지정한다. 프로그래머가 정의된 연산자를 지원하는 언어는 새로운 연산자의 기호 (예: [[프롤로그]])의 우선순위와 결합법칙의 사양이 필요하다.
The specification of a language will specify the [[Order of operations|precedence]] and [[associativity]] of the operators it supports. Languages which support programmer-defined operators require the specification of the precedence and associativity of new operator symbols (e.g. [[Prolog]]).


대부분의 프로그래밍 언어 연산자는 몇몇 많은 [[피연산자]] (예, C에서 [[?:]] 연산자) 지원과 함께, 하나 또는 두 개의 피연산자를 사용한다. 그것의 피연산자와 관련하여 연산자의 위치는 [[폴란드 표기법|접두사]], [[삽입사 표기법|삽입사]] 또는 [[역폴란드 표기법|접미사]]일 것이다.
Most programming language operators take one or two [[operands]], with a few supporting more operands (e.g., the [[?:]] operator in C). The position of the operator with respect to its operands may be [[Polish notation|prefix]], [[infix notation|infix]] or [[postfix notation|postfix]].


[[Syntax (programming languages)|Syntactically]] operators usually contrast to [[Function (computer science)|functions]]. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory [[Parens#Parentheses_.28_.29|parentheses]] e.g. <code>Func(a)</code> (or <code>(Func a)</code> in [[Lisp (programming language)|LISP]]). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).
[[Syntax (programming languages)|Syntactically]] operators usually contrast to [[Function (computer science)|functions]]. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory [[Parens#Parentheses_.28_.29|parentheses]] e.g. <code>Func(a)</code> (or <code>(Func a)</code> in [[Lisp (programming language)|LISP]]). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).

2011년 8월 27일 (토) 13:54 판

프로그래밍 언어는 일반적으로 수학 연산과 유사한 연산자의 집합을 지원한다. 언어는 내장된 연산자의 정해진 숫자를 포함할 수 있다 (예: C와 C++에서 + - * =), 아니면 프로그래머가 정의한 연산자의 생성을 허용할 수 있다 (예: 하스켈). 일부 프로그래밍 언어는 다른 div와 같은 이름도 허용하는 동안 특수 문자 + 또는 := 과 같은 연산자 기호를 제한한다 (예: 파스칼).

언어에 의해 지원된 내장 연산자 일부는 일반적으로 중앙 처리 장치에 있는 명령의 작은 숫자에 직접 매핑을 하지만, 다른 것(: '+'는 문자열 연결을 표현하기 위해 사용)은 복잡한 구현을 할 수 있다.

언어의 사양은 우선순위와 그것이 지원하는 연산자의 결합법칙을 지정한다. 프로그래머가 정의된 연산자를 지원하는 언어는 새로운 연산자의 기호 (예: 프롤로그)의 우선순위와 결합법칙의 사양이 필요하다.

대부분의 프로그래밍 언어 연산자는 몇몇 많은 피연산자 (예, C에서 ?: 연산자) 지원과 함께, 하나 또는 두 개의 피연산자를 사용한다. 그것의 피연산자와 관련하여 연산자의 위치는 접두사, 삽입사 또는 접미사일 것이다.

Syntactically operators usually contrast to functions. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory parentheses e.g. Func(a) (or (Func a) in LISP). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).

A compiler can implement operators and functions with subroutine calls or with inline code.

Operator overloading

In some programming languages an operator may be ad-hoc polymorphic, that is, have definitions for more than one kind of data, (such as in Java where the + operator is used both for the addition of numbers and for the concatenation of strings). Such an operator is said to be overloaded. In languages that support operator overloading by the programmer but have a limited set of operators, operator overloading is often used to define customized uses for operators.

Operand coercion

Some languages also allow for the operands of an operator to be implicitly converted, or coerced, to suitable data types for the operation to occur. For example, in Perl coercion rules lead into 12 + "3.14" producing the result of 15.14. The text "3.14" is converted to the number 3.14 before addition can take place. Further, 12 is an integer and 3.14 is either a floating or fixed-point number (a number that has a decimal place in it) so the integer is then converted to a floating point or fixed-point number respectively.

Javascript follows opposite rules—finding the same expression above, it will convert the integer 12 into a string "12", then concatenate the two operands to form "123.14".

In the presence of coercions in a language, the programmer must be aware of the specific rules regarding operand types and the operation result type to avoid subtle programming mistakes.

Operator features in programming languages

The following table shows the operator features in several programming languages:

Programming language Nonalphanumeric operator symbols Alphanumeric operator symbols Prefix Infix Postfix Precedence Associativity Overloading Programmer-defined overloading Programmer-defined operator symbols
ALGOL 68 +* ** * / % %* %× - + < <= >= > = /= & -:= +:= *:= /:= %:= %*:= +=: :=: :/=:

(All operators have bold Alphanumeric equivalents, c.f. next column. Some have non ASCII equivalents, c.f. below.) ¬ +× ⊥ ↑ ↓ ⌊ ⌈ × ÷ ÷× ÷* □ ≤ ≥ ≠ ∧ ∨ ×:= ÷:= ÷×:= ÷*:= %×:= :≠:

not abs arg bin entier leng level odd repr round shorten i shl shr up down lwb upb lt le ge gt eq ne and or over mod elem minusab plusab timesab divab overab modab plusto is isnt 아니요 (prefix operators always have priority 10) Infix operators are left associative, prefix operators are right associative
C () [] -> . ! ~ ++ -- + - * & / % << >> < <= > <= == != ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= sizeof 아니요 아니요
C++ (more) sizeof new delete throw 아니요
Java new instanceof 아니요 아니요
Pascal * / + - = < > <> <= >= := not div mod and or in 아니요 아니요 아니요
Seed7 {} [] -> ** ! + - * / << >> & >< | = <> > >= < <= <& := +:= -:= *:= /:= <<:= >>:= &:= @:= conv varConv parse conj div rem mdiv mod times mult in not and or digits lpad rpad lpad0
Prolog :- ?- ; , . =.. = \= < =< >= > == \== - + / * spy nospy not is mod 아니요 아니요
Lisp + - * / = /= < <= > >= mod rem floor truncate min max (operator and parameters must be surrounded by parentheses) 아니요 아니요 아니요 아니요 아니요
Smalltalk (yes - Up to two characters[1]) (yes - Needs a colon after the keyword) 아니요 아니요 아니요

See also

References