ebooksgratis.com

See also ebooksgratis.com: no banners, no cookies, totally FREE.

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
Privacy Policy Cookie Policy Terms and Conditions
Operator (programming) - Wikipedia, the free encyclopedia

Operator (programming)

From Wikipedia, the free encyclopedia

Programming languages generally have a set of operators that are similar to operators in mathematics: they are somehow special functions. In addition to arithmetic operations they often perform boolean operations on truth values and string operations on strings of text. Relational operators are used to test two values, such as equality if one value is greater than another. Unlike functions, operators often provide the primitive operations of the language, their name consists of punctuation rather than alphanumeric characters, and they have special infix syntax and irregular parameter passing conventions. The exact terminology, however, varies from language to language.

Conventionally, the computing usage of "operator" goes beyond the set of common arithmetic operators. The C programming language, for example, also supports operators like &, ++ and sizeof. Operators like sizeof, which are alphanumeric rather than a mathematical symbol or a punctuation character, are sometimes called named operators. See Operators in C and C++. Operators in C are primitive operations of the language that the compiler can fairly directly map into the machine instructions of microprocessors.

On the other hand, in languages such as Haskell and Prolog, operators are a purely syntactic concept. Any combination of symbols and punctuation can be used as an operator, and its precedence and associativity can be set. While Haskell only allows new binary operators, Prolog allows one to define operators that are either unary or binary and either prefix, infix or postfix. In Haskell, the operator can be defined and applied just like a function and vice versa by the appropriate use of parentheses or backquotes. Analogously in Prolog, terms use either operators or normal functors and quotes convert between the usages.

In certain programming languages, such as PostScript, the use of the word "operator" has more specific meaning, in that an operator is an executable element in the stack. Because operators here are always written postfix, the need for parentheses is redundant as the way objects are taken from the stack ensures correct evaluation. This is an example of Reverse Polish notation.

Contents

[edit] Operator syntax

Computers are mathematical devices, but compilers and interpreters require a full syntactic theory of all operations in order to parse formulae involving any combinations correctly. In particular they depend on operator precedence rules, on order of operations, that are tacitly assumed in mathematical writing.

Most operators are binary or unary, although operators with higher arity such as the ternary operator exist. Binary operators ("bi" as in "two") have two operands. In "A*B" the * operator has two operands: A and B. In "!B" the "!" operator (meaning boolean NOT) has only one operand, and is therefore a unary operator. The "-" and "+" operators can be both binary and unary, in "-4" or "+4" it denotes a negative or a positive number, in "0-4" it acts as the subtraction operator.

Operators have associativity. This defines the correct evaluation order if a sequence of the same operator is used one after another: whether the evaluator will evaluate the left operations first or the right. For example, since subtraction in most languages is left associative, the expression 8 - 4 - 2 evaluates left to right: 8 - 4 is evaluated first, and the result is 2 (rather than 6).

The exponentiation operator (typically written as ^ or **) is often right associative. So 4^3^2 equals 4^9, not 64^2.

See Infix notation for more information about infix operator syntax.

[edit] Operator overloading

Main article: 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, such as C++, operator overloading is often used to define customized uses for operators.

[edit] 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.

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.

See Type conversion for more information about coercion.

[edit] Operators in APL

In the Iverson Notation that later became APL, Kenneth E. Iverson defined several operators (reduction, inner product, outer product) acting on functions to produce functions. So, for example, +/ (plus-reduce) applies the reduction operator to the binary function + to create a function for adding vectors, rows of matrices, etc. thus:

   +/1 2 3 4
10

Many more operators (Scan, Each, Curry,...) have been defined since then in various dialects of APL, and user-defined operators have also been added in those dialects of APL that provide nested arrays.


aa - ab - af - ak - als - am - an - ang - ar - arc - as - ast - av - ay - az - ba - bar - bat_smg - bcl - be - be_x_old - bg - bh - bi - bm - bn - bo - bpy - br - bs - bug - bxr - ca - cbk_zam - cdo - ce - ceb - ch - cho - chr - chy - co - cr - crh - cs - csb - cu - cv - cy - da - de - diq - dsb - dv - dz - ee - el - eml - en - eo - es - et - eu - ext - fa - ff - fi - fiu_vro - fj - fo - fr - frp - fur - fy - ga - gan - gd - gl - glk - gn - got - gu - gv - ha - hak - haw - he - hi - hif - ho - hr - hsb - ht - hu - hy - hz - ia - id - ie - ig - ii - ik - ilo - io - is - it - iu - ja - jbo - jv - ka - kaa - kab - kg - ki - kj - kk - kl - km - kn - ko - kr - ks - ksh - ku - kv - kw - ky - la - lad - lb - lbe - lg - li - lij - lmo - ln - lo - lt - lv - map_bms - mdf - mg - mh - mi - mk - ml - mn - mo - mr - mt - mus - my - myv - mzn - na - nah - nap - nds - nds_nl - ne - new - ng - nl - nn - no - nov - nrm - nv - ny - oc - om - or - os - pa - pag - pam - pap - pdc - pi - pih - pl - pms - ps - pt - qu - quality - rm - rmy - rn - ro - roa_rup - roa_tara - ru - rw - sa - sah - sc - scn - sco - sd - se - sg - sh - si - simple - sk - sl - sm - sn - so - sr - srn - ss - st - stq - su - sv - sw - szl - ta - te - tet - tg - th - ti - tk - tl - tlh - tn - to - tpi - tr - ts - tt - tum - tw - ty - udm - ug - uk - ur - uz - ve - vec - vi - vls - vo - wa - war - wo - wuu - xal - xh - yi - yo - za - zea - zh - zh_classical - zh_min_nan - zh_yue - zu -