Knowledge (XXG)

Increment and decrement operators

Source 📝

1080:
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.
36: 246:
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); incrementing a pointer to a structure of size 106 bytes makes it point to the next structure by increasing the pointer value by 106.
245:
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
1079:
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
234:
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
53: 1232: 898:
The following list, though not complete or all-inclusive, lists some of the major programming languages that support the increment and decrement operators.
208:-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value. 1302: 100: 182:
The increment operator increases, and the decrement operator decreases, the value of its operand by 1. The operand must have an arithmetic or
72: 1372: 79: 193:. Pointers values are increased (or decreased) by an amount that makes them point to the next (or previous) element adjacent in memory. 1216: 1146: 219:-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's value 119: 238:, it is not clear in what sequence the subtraction and increment operations should be performed. Such expressions generally invoke 86: 1440: 68: 927: 764: 640: 183: 57: 1052: 1037: 1029: 1025: 1019: 1138: 1056: 995: 951: 760: 932: 46: 1445: 946: 228: 1022:
once supported these operators, but they have been depreciated since version 2.2 and removed as of version 3.0.
1068: 937: 917: 168: 161: 93: 1344: 1158: 164:-like languages feature two versions (pre- and post-) of each operator with slightly different semantics. 154: 143: 1171: 1288: 1090: 518: 157: 1226: 1114: 1001: 239: 1212: 1142: 912: 1208: 1201: 1414: 1330: 1316: 1246: 1006: 1260: 425:
In languages lacking these operators, equivalent results require an extra line of code:
1398: 140: 1434: 1072: 1402: 1274: 17: 1358: 1134: 961: 190: 171:(including C and its various derivatives), the increment operator is written as 35: 1185: 990: 966: 956: 186: 390:// Post-decrement: y is assigned the value of x, then x is decremented by 1 321:// Post-increment: y is assigned the value of x, then x is incremented by 1 1419: 357:// Pre-decrement: x is decremented by 1, then y is assigned the value of x 288:// Pre-increment: x is incremented by 1, then y is assigned the value of x 1033: 27:
Unary operators that add or subtract one from their operand, respectively
231:), only one version is needed (in the case of Go, post operators only). 971: 147: 1109: 254:
The following C code fragment illustrates the difference between the
1207:(2. ed.,  ed.). Englewood Cliffs, N.J.: Prentice Hall. p.  922: 756: 227:
In languages where increment/decrement is not an expression (e.g.,
1104: 1199:
Ritchie, Brian W. Kernighan; Dennis M.; Ritchie, Dennis (1988).
980: 985: 907: 835:// this statement displays 2, then c is incremented by 1 to 3. 457:# x is now 2 (can be written as "x += 1" in Python) 29: 901: 755:
These examples also work in other C-like languages, such as
179:. Several other languages use inc(x) and dec(x) functions. 196:
In languages that support both versions of the operators:
859:// this statement increments c by 1, then c is displayed. 639:
The post-increment operator is also commonly used with
771:
Increment operator can be demonstrated by an example:
60:. Unsourced material may be challenged and removed. 1200: 517:The post-increment operator is commonly used with 8: 1231:: CS1 maint: multiple names: authors list ( 223:to the increment (or decrement) operation. 1418: 175:and the decrement operator is written as 120:Learn how and when to remove this message 1333:. Wolfram Language Documentation Center. 1319:. Wolfram Language Documentation Center. 167:In languages syntactically derived from 1186:"8.3. The Double-Parentheses Construct" 1126: 717:// Loop that counts down from n to zero 1224: 7: 1135:"Understanding and Using C Pointers" 58:adding citations to reliable sources 1403:"The Development of the C Language" 1331:"Decrement Wolfram Language Symbol" 1317:"Increment Wolfram Language Symbol" 1289:"GP Operators and their Priorities" 744:// Copies element *(src) to *(dst), 622:// through n elements of the array 619:// Post-increment of i, which steps 262:increment and decrement operators: 69:"Increment and decrement operators" 1188:. The Linux Documentation Project. 1067:The concept was introduced in the 25: 747:// then increments both pointers 189:, and must refer to a modifiable 1139:"Chapter 4. Pointers and Arrays" 1059:do not support these operators. 146:that increase or decrease their 34: 1247:"Increment/decrement operators" 526:// Sum the elements of an array 45:needs additional citations for 1263:. Microsoft Developer Network. 1: 1141:. O'Reilly Media, Inc. 2013. 1000:Vex, a scripting language in 1303:"About Assignment Operators" 1261:"++ Operator (C# Reference)" 648:// Copy one array to another 1345:"New Features in Swift 2.2" 1174:. Free Software Foundation. 153:They are commonly found in 1462: 1203:The C programming language 387:// x is now 0, y is also 0 318:// x is now 2, y is also 2 1361:. Swift Official Website. 1347:. Swift Official Website. 1159:"Introductory C with C++" 521:subscripts. For example: 472:# Post-increment: y = x++ 242:, and should be avoided. 1048:) instead of operators. 878: 772: 645: 523: 430:# Pre-increment: y = ++x 427: 264: 1441:Operators (programming) 1275:"Operator Overloading" 1172:"GNU Awk's User Guide" 1082: 1069:B programming language 354:// Decrement operators 285:// Increment operators 1420:10.1145/155360.155580 1359:"Swift 3.0 Released!" 1161:. 2019. Figure 12-12. 1077: 420:// y is 1, x is now 0 351:// y is 1, x is now 2 158:programming languages 1091:Augmented assignment 894:Supporting languages 54:improve this article 1407:ACM SIGPLAN Notices 1249:. cppreference.com. 137:decrement operators 1399:Ritchie, Dennis M. 1157:Richard Petersen. 1115:Successor function 240:undefined behavior 18:Increment operator 1133:Richard M Reese. 1015: 1014: 130: 129: 122: 104: 16:(Redirected from 1453: 1446:Unary operations 1425: 1424: 1422: 1395: 1389: 1388: 1380: 1376: 1369: 1363: 1362: 1355: 1349: 1348: 1341: 1335: 1334: 1327: 1321: 1320: 1313: 1307: 1306: 1299: 1293: 1292: 1285: 1279: 1278: 1271: 1265: 1264: 1257: 1251: 1250: 1243: 1237: 1236: 1230: 1222: 1206: 1196: 1190: 1189: 1182: 1176: 1175: 1168: 1162: 1155: 1149: 1131: 1100: 1096: 1047: 1043: 1040:uses functions ( 1007:Wolfram Language 902: 885: 882: 872: 869: 866: 863: 860: 857: 854: 851: 848: 845: 842: 839: 836: 833: 830: 827: 824: 821: 818: 815: 812: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 751: 748: 745: 742: 739: 736: 733: 730: 727: 724: 721: 718: 715: 712: 709: 706: 703: 700: 697: 694: 691: 688: 685: 682: 679: 676: 673: 670: 667: 664: 661: 658: 655: 652: 649: 635: 632: 629: 626: 623: 620: 617: 614: 611: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 421: 418: 415: 412: 409: 406: 403: 400: 397: 394: 391: 388: 385: 382: 379: 376: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 337: 334: 331: 328: 325: 322: 319: 316: 313: 310: 307: 304: 301: 298: 295: 292: 289: 286: 283: 280: 277: 274: 271: 268: 237: 178: 174: 125: 118: 114: 111: 105: 103: 62: 38: 30: 21: 1461: 1460: 1456: 1455: 1454: 1452: 1451: 1450: 1431: 1430: 1429: 1428: 1397: 1396: 1392: 1385:Swift evolution 1378: 1374: 1371: 1370: 1366: 1357: 1356: 1352: 1343: 1342: 1338: 1329: 1328: 1324: 1315: 1314: 1310: 1301: 1300: 1296: 1287: 1286: 1282: 1273: 1272: 1268: 1259: 1258: 1254: 1245: 1244: 1240: 1223: 1219: 1198: 1197: 1193: 1184: 1183: 1179: 1170: 1169: 1165: 1156: 1152: 1132: 1128: 1123: 1098: 1094: 1087: 1065: 1045: 1041: 1016: 896: 887: 886: 883: 880: 874: 873: 870: 867: 864: 861: 858: 855: 852: 849: 846: 843: 840: 837: 834: 831: 828: 825: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 778:<stdio.h> 777: 774: 753: 752: 749: 746: 743: 740: 737: 734: 731: 728: 725: 722: 719: 716: 713: 710: 707: 704: 701: 698: 695: 692: 689: 686: 683: 680: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 647: 637: 636: 633: 630: 627: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 516: 514: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 423: 422: 419: 416: 413: 410: 407: 404: 401: 398: 395: 392: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 275: 272: 269: 266: 252: 235: 215:-increment and 204:-increment and 176: 172: 126: 115: 109: 106: 63: 61: 51: 39: 28: 23: 22: 15: 12: 11: 5: 1459: 1457: 1449: 1448: 1443: 1433: 1432: 1427: 1426: 1401:(March 1993). 1390: 1364: 1350: 1336: 1322: 1308: 1294: 1280: 1266: 1252: 1238: 1217: 1191: 1177: 1163: 1150: 1125: 1124: 1122: 1119: 1118: 1117: 1112: 1107: 1102: 1086: 1083: 1071:circa 1969 by 1064: 1061: 1013: 1012: 1011: 1010: 1004: 998: 993: 988: 983: 976: 975: 974: 969: 964: 959: 954: 949: 942: 941: 940: 935: 930: 925: 920: 915: 910: 900: 895: 892: 891: 890: 889: 888: 879: 844:"%d" 773: 646: 524: 428: 265: 251: 248: 225: 224: 209: 128: 127: 110:September 2014 42: 40: 33: 26: 24: 14: 13: 10: 9: 6: 4: 3: 2: 1458: 1447: 1444: 1442: 1439: 1438: 1436: 1421: 1416: 1412: 1408: 1404: 1400: 1394: 1391: 1386: 1382: 1368: 1365: 1360: 1354: 1351: 1346: 1340: 1337: 1332: 1326: 1323: 1318: 1312: 1309: 1304: 1298: 1295: 1290: 1284: 1281: 1276: 1270: 1267: 1262: 1256: 1253: 1248: 1242: 1239: 1234: 1228: 1220: 1218:0-13-110362-8 1214: 1210: 1205: 1204: 1195: 1192: 1187: 1181: 1178: 1173: 1167: 1164: 1160: 1154: 1151: 1148: 1147:9781449344184 1144: 1140: 1136: 1130: 1127: 1120: 1116: 1113: 1111: 1108: 1106: 1103: 1092: 1089: 1088: 1084: 1081: 1076: 1074: 1070: 1062: 1060: 1058: 1054: 1049: 1039: 1035: 1031: 1027: 1023: 1021: 1008: 1005: 1003: 999: 997: 994: 992: 989: 987: 984: 982: 979: 978: 977: 973: 970: 968: 965: 963: 960: 958: 955: 953: 950: 948: 945: 944: 943: 939: 936: 934: 931: 929: 926: 924: 921: 919: 916: 914: 911: 909: 906: 905: 904: 903: 899: 893: 876: 875: 770: 769: 768: 766: 762: 758: 644: 642: 522: 520: 469:# y is also 2 426: 263: 261: 257: 249: 247: 243: 241: 232: 230: 222: 218: 214: 210: 207: 203: 199: 198: 197: 194: 192: 188: 185: 180: 170: 165: 163: 159: 156: 151: 149: 145: 142: 138: 134: 124: 121: 113: 102: 99: 95: 92: 88: 85: 81: 78: 74: 71: –  70: 66: 65:Find sources: 59: 55: 49: 48: 43:This article 41: 37: 32: 31: 19: 1410: 1406: 1393: 1384: 1373:"Remove the 1367: 1353: 1339: 1325: 1311: 1297: 1283: 1277:. dlang.org. 1269: 1255: 1241: 1202: 1194: 1180: 1166: 1153: 1129: 1078: 1073:Ken Thompson 1066: 1050: 1024: 1017: 897: 754: 638: 532:sum_elements 515: 511:# x is now 2 424: 259: 255: 253: 244: 233: 226: 220: 216: 212: 205: 201: 195: 181: 166: 152: 136: 132: 131: 116: 107: 97: 90: 83: 76: 64: 52:Please help 47:verification 44: 962:Objective-C 191:data object 1435:Categories 1381:operators" 1121:References 991:PowerShell 967:GNU Octave 957:JavaScript 654:copy_array 155:imperative 80:newspapers 1227:cite book 1101:operators 187:data type 150:by one. 144:operators 133:Increment 1413:(3): 5. 1085:See also 1051:Notably 1034:Modula-2 1018:Apple's 814:"%d 775:#include 641:pointers 493:# y is 1 250:Examples 1063:History 1002:Houdini 972:PARI/GP 877:Output: 236:x - ++x 184:pointer 148:operand 94:scholar 1215:  1145:  1110:PDP-11 1093:– for 1053:Python 1046:dec(x) 1042:inc(x) 1038:Oberon 1036:, and 1030:Delphi 1026:Pascal 862:return 838:printf 820:" 808:printf 763:, and 625:return 96:  89:  82:  75:  67:  1105:PDP-7 1020:Swift 1009: 696:while 672:float 660:float 589:while 559:float 538:float 529:float 519:array 221:prior 141:unary 101:JSTOR 87:books 1377:and 1233:link 1213:ISBN 1143:ISBN 1097:and 1057:Rust 1055:and 1044:and 996:Vala 981:Perl 952:Java 933:CFML 913:Bash 784:main 761:Java 708:> 651:void 598:< 260:post 258:and 217:post 213:post 211:The 200:The 139:are 135:and 73:news 1415:doi 986:PHP 923:C++ 908:AWK 793:int 781:int 757:C++ 735:src 723:dst 684:int 678:dst 666:src 628:sum 613:arr 607:sum 574:int 568:0.0 562:sum 547:int 541:arr 276:int 267:int 256:pre 206:pre 202:pre 56:by 1437:: 1411:28 1409:. 1405:. 1383:. 1379:-- 1375:++ 1229:}} 1225:{{ 1211:. 1209:18 1137:. 1099:-= 1095:+= 1075:. 1032:, 1028:, 947:Go 928:C# 856:); 850:++ 832:); 829:++ 817:\n 787:() 767:. 765:C# 759:, 738:++ 726:++ 705:-- 643:: 610:+= 414:-- 378:-- 345:++ 309:++ 229:Go 177:-- 173:++ 160:. 1423:. 1417:: 1387:. 1305:. 1291:. 1235:) 1221:. 938:D 918:C 884:4 881:2 871:} 868:; 865:0 853:c 847:, 841:( 826:c 823:, 811:( 805:; 802:2 799:= 796:c 790:{ 750:} 741:; 732:* 729:= 720:* 714:) 711:0 702:n 699:( 693:{ 690:) 687:n 681:, 675:* 669:, 663:* 657:( 634:} 631:; 616:; 604:) 601:n 595:i 592:( 586:; 583:0 580:= 577:i 571:; 565:= 556:{ 553:) 550:n 544:, 535:( 508:1 505:+ 502:x 499:= 496:x 490:x 487:= 484:y 481:1 478:= 475:x 466:x 463:= 460:y 454:1 451:+ 448:x 445:= 442:x 439:1 436:= 433:x 417:; 411:x 408:= 405:y 402:; 399:1 396:= 393:x 384:; 381:x 375:= 372:y 369:; 366:1 363:= 360:x 348:; 342:x 339:= 336:y 333:; 330:1 327:= 324:x 315:; 312:x 306:= 303:y 300:; 297:1 294:= 291:x 282:; 279:y 273:; 270:x 169:B 162:C 123:) 117:( 112:) 108:( 98:· 91:· 84:· 77:· 50:. 20:)

Index

Increment operator

verification
improve this article
adding citations to reliable sources
"Increment and decrement operators"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
unary
operators
operand
imperative
programming languages
C
B
pointer
data type
data object
Go
undefined behavior
array
pointers
C++
Java
C#
AWK

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.