This article needs additional citations for verification .(September 2014) |
Increment and decrement operators are unary operators that increase or decrease their operand by one.
They are commonly found in imperative programming languages. C-like languages feature two versions (pre- and post-) of each operator with slightly different semantics.
In languages syntactically derived from B (including C and its various derivatives), the increment operator is written as ++
and the decrement operator is written as --
. Several other languages use inc(x) and dec(x) functions.
The increment operator increases, and the decrement operator decreases, the value of its operand by 1. The operand must have an arithmetic or pointer data type, and must refer to a modifiable data object. Pointers values are increased (or decreased) by an amount that makes them point to the next (or previous) element adjacent in memory.
In languages that support both versions of the operators:
In languages where increment/decrement is not an expression (e.g., Go), only one version is needed (in the case of Go, post operators only).
Since the increment/decrement operator modifies its operand, use of such an operand more than once within the same expression can produce undefined results. For example, in expressions such as x - ++x
, it is not clear in what sequence the subtraction and increment operations should be performed. Such expressions generally invoke undefined behavior, and should be avoided.
In languages with typed pointers like C, the increment operator steps the pointer to the next item of that type -- increasing the value of the pointer by the size of that type. When a pointer (of the right type) points to any item in an array, incrementing (or decrementing) makes the pointer point to the "next" (or "previous") item of that array. Thus, incrementing a pointer to an integer makes it point to the next integer (typically increasing the pointer value by 4); [1] incrementing a pointer to a structure of size 106 bytes makes it point to the next structure by increasing the pointer value by 106. [2]
The following C code fragment illustrates the difference between the pre and post increment and decrement operators:
intx;inty;// Increment operators// Pre-increment: x is incremented by 1, then y is assigned the value of xx=1;y=++x;// x is now 2, y is also 2// Post-increment: y is assigned the value of x, then x is incremented by 1x=1;y=x++;// y is 1, x is now 2// Decrement operators// Pre-decrement: x is decremented by 1, then y is assigned the value of xx=1;y=--x;// x is now 0, y is also 0// Post-decrement: y is assigned the value of x, then x is decremented by 1x=1;y=x--;// y is 1, x is now 0
In languages lacking these operators, equivalent results require an extra line of code:
# Pre-increment: y = ++xx=1x=x+1# x is now 2 (can be written as "x += 1" in Python)y=x# y is also 2# Post-increment: y = x++x=1y=x# y is 1x=x+1# x is now 2
The post-increment operator is commonly used with array subscripts. For example:
// Sum the elements of an arrayfloatsum_elements(floatarr[],intn){floatsum=0.0;inti=0;while(i<n)sum+=arr[i++];// Post-increment of i, which steps// through n elements of the arrayreturnsum;}
The post-increment operator is also commonly used with pointers:
// Copy one array to anothervoidcopy_array(float*src,float*dst,intn){while(n-->0)// Loop that counts down from n to zero*dst++=*src++;// Copies element *(src) to *(dst),// then increments both pointers}
These examples also work in other C-like languages, such as C++, Java, and C#.
#include<stdio.h>intmain(){intc=2;printf("%d\n",c++);// this statement displays 2, then c is incremented by 1 to 3.printf("%d",++c);// this statement increments c by 1, then c is displayed.return0;}
24
The following list, though not complete or all-inclusive, lists some of the major programming languages that support the increment and decrement operators.
|
Apple's Swift once supported these operators, but they have been depreciated since version 2.2 [13] and removed as of version 3.0. [14] [15]
Pascal, Delphi, Modula-2, and Oberon uses functions (inc(x)
and dec(x)
) instead of operators.
The concept was introduced in the B programming language circa 1969 by Ken Thompson. [16]
Thompson went a step further by inventing the ++ and -- operators, which increment or decrement; their prefix or postfix position determines whether the alteration occurs before or after noting the value of the operand. They were not in the earliest versions of B, but appeared along the way. People often guess that they were created to use the auto-increment and auto-decrement address modes provided by the DEC PDP-11 on which C and Unix first became popular. This is historically impossible, since there was no PDP-11 when B was developed. The PDP-7, however, did have a few 'auto-increment' memory cells, with the property that an indirect memory reference through them incremented the cell. This feature probably suggested such operators to Thompson; the generalization to make them both prefix and postfix was his own. Indeed, the auto-increment cells were not used directly in implementation of the operators, and a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1.
+=
and -=
operatorsC is a general-purpose programming language. It was created in the 1970s by Dennis Ritchie and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems code, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.
In logic, mathematics, and computer science, arity is the number of arguments or operands taken by a function, operation or relation. In mathematics, arity may also be called rank, but this word can have many other meanings. In logic and philosophy, arity may also be called adicity and degree. In linguistics, it is usually named valency.
A one-instruction set computer (OISC), sometimes referred to as an ultimate reduced instruction set computer (URISC), is an abstract machine that uses only one instruction – obviating the need for a machine language opcode. With a judicious choice for the single instruction and given arbitrarily many resources, an OISC is capable of being a universal computer in the same manner as traditional computers that have multiple instructions. OISCs have been recommended as aids in teaching computer architecture and have been used as computational models in structural computing research. The first carbon nanotube computer is a 1-bit one-instruction set computer.
In mathematics, a unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is any function f : A → A, where A is a set. The function f is a unary operation on A.
In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor. Most bitwise operations are presented as two-operand instructions where the result replaces one of the input operands.
An index register in a computer's CPU is a processor register used for pointing to operand addresses during the run of a program. It is useful for stepping through strings and arrays. It can also be used for holding loop iterations and counters. In some architectures it is used for read/writing blocks of memory. Depending on the architecture it may be a dedicated index register or a general-purpose register. Some instruction sets allow more than one index register to be used; in that case additional instruction fields may specify which index registers to use.
In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type, or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which type is in use. It can be thought of as a type that has several "cases", each of which should be handled correctly when that type is manipulated. This is critical in defining recursive datatypes, in which some component of a value may have the same type as that value, for example in defining a type for representing trees, where it is necessary to distinguish multi-node subtrees and leaves. Like ordinary unions, tagged unions can save storage by overlapping storage areas for each type, since only one is in use at a time.
The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.
This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.
In computer programming, the ternary conditional operator is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, ternary if, or inline if. An expression a ? b : c
evaluates to b
if the value of a
is true, and otherwise to c
. One can read it aloud as "if a then b otherwise c". The form a ? b : c
is the most common, but alternative syntax do exist; for example, Raku uses the syntax a ?? b !! c
to avoid confusion with the infix operators ?
and !
, whereas in Visual Basic .NET, it instead takes the form If(a, b, c)
.
Addressing modes are an aspect of the instruction set architecture in most central processing unit (CPU) designs. The various addressing modes that are defined in a given instruction set architecture define how the machine language instructions in that architecture identify the operand(s) of each instruction. An addressing mode specifies how to calculate the effective memory address of an operand by using information held in registers and/or constants contained within a machine instruction or elsewhere.
In computer programming, operators are constructs defined within programming languages which behave generally like functions, but which differ syntactically or semantically.
The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.
The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.
sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.
The conditional operator is supported in many programming languages. This term usually refers to ?:
as in C, C++, C#, and JavaScript. However, in Java, this term can also refer to &&
and ||
.
In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.
In the C and C++ programming languages, the comma operator is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value ; there is a sequence point between these evaluations.
The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.
In the C programming language, operations can be performed on a bit level using bitwise operators.
{{cite book}}
: CS1 maint: multiple names: authors list (link)++
and --
operators". Swift evolution.