Knowledge

Integer literal

Source 📝

413:). There can be some restrictions on placement; for example, in Java they cannot appear at the start or end of the literal, nor next to a decimal point. While the period, comma, and (thin) spaces are used in normal writing for digit separation, these conflict with their existing use in programming languages as 162:
As with other literals, integer literals are generally evaluated at compile time, as part of the semantic analysis phase. In some cases this semantic analysis is done in the lexer, immediately on recognition of an integer literal, while in other cases this is deferred until the parsing stage, or
730: 488:, the apostrophe character may be used to separate digits arbitrarily in numeric literals. The underscore was initially proposed, with an initial proposal in 1993, and again for 337:
Typically decimal numbers (base-10) are grouped in three digit groups (representing one of 1000 possible values), binary numbers (base-2) in four digit groups (one
341:, representing one of 16 possible values), and hexadecimal numbers (base-16) in two digit groups (each digit is one nibble, so two digits are one 345:, representing one of 256 possible values). Numbers from other systems (such as id numbers) are grouped following whatever convention is in use. 190:
is possible, meaning that literal expressions involving literal values can be evaluated at the compile phase. For example, in the statement
318:
into more legible forms. If this is available, it can usually be done for floating point literals as well. This is particularly useful for
656: 595: 676: 206:
Integer literals frequently have prefixes indicating base, and less frequently suffixes indicating type. For example, in
139:). Once a string has been lexed (tokenized) as an integer literal, its value cannot be determined syntactically (it is 107:, which happens to be an integer string, but this is semantic analysis of the string literal – at the syntactic level 135:, with the lowercase indicating a lexical-level token class, as opposed to phrase-level production rule (such as 374: 358: 25: 390: 366: 151: 128: 782:"N0259: A Proposal to allow Binary Literals, and some other small changes to Chapter 2: Lexical Conventions" 402: 382: 303: 398: 394: 378: 198:
has been parsed, it can then be evaluated to 4, though the value 4 does not itself appear as a literal.
29: 123:
Recognizing a string (sequence of characters in the source code) as an integer literal is part of the
832: 799: 710: 485: 370: 814: 781: 745: 622: 608: 481: 362: 354: 33: 696: 331: 326:
rather than counting digits. It is also useful for numbers that are typically grouped, such as
857: 315: 8: 763: 744:
Crowl, Lawrence; Smith, Richard; Snyder, Jeff; Vandevoorde, Daveed (25 September 2013).
322:
and makes it easier to see the size of large numbers (such as a million) at a glance by
493: 327: 147: 852: 571:
Typically sensitive numbers such as these would not be included as literals, however.
623:""Rationale for the Design of the Ada® Programming Language": 2.1 Lexical Structure" 187: 124: 17: 171:
the lexer could immediately evaluate this to 16 and store that (a token of type
418: 213:
indicates the value 16 (because hexadecimal) as an unsigned long long integer.
100: 103:, not an integer literal, because it is in quotes. The value of the string is 846: 260: 131:
phase. Within the lexer and phrase grammar, the token class is often denoted
186:
Once literals have been evaluated, further semantic analysis in the form of
314:
In some languages, integer literals may contain digit separators to allow
636: 497: 414: 228: 127:(lexing) phase, while evaluating the literal to its value is part of the 37: 167:
has been completely constructed. For example, on recognizing the string
406: 323: 164: 91:
evaluates to 4, but the value 4 is not literally included. Further, in
56:
is an integer literal indicating the value 16, which is represented by
334:. Very long numbers can be further grouped by doubling up separators. 175:
and value 16), or defer evaluation and instead record a token of type
143:
an integer), and evaluation of its value becomes a semantic question.
319: 48:
is an integer literal indicating the value 1, while in the statement
657:"Glasgow Haskell Compiler User's Guide: 11.3.7. Numeric underscores" 489: 477: 492:, following other languages. However, this caused conflict with 405:, integer literals and float literals can be separated with an 338: 76: 306:, though sigils attach to identifiers (names), not literals. 246: 207: 743: 386: 342: 609:"Ada '83 Language Reference Manual: 2.4. Numeric Literals" 194:
after the literals have been evaluated and the expression
731:"The Swift Programming Language: Lexical Structure" 746:"N3781 Single-Quotation-Mark as a Digit Separator" 637:"Go 1.13 Release Notes - Changes to the language" 496:, so the apostrophe was proposed instead, as an " 844: 779: 761: 83:included in the source code. More simply, in 812: 590: 588: 40:. For example, in the assignment statement 500:" (which is used in some other contexts). 146:Integer literals are generally lexed with 596:2.4.4. Integer and long integer literals 585: 845: 302:These affixes are somewhat similar to 111:is simply a string, no different from 830: 797: 417:, list separator (and in C/C++, the 780:John Max Skaller (March 26, 1993). 309: 13: 815:"N3448: Painless Digit Separation" 813:Vandevoorde, Daveed (2012-09-21). 14: 869: 677:"Underscores in Numeric Literals" 544:0b0000'0001'0010'0011 60:in hexadecimal (indicated by the 824: 806: 791: 773: 755: 737: 723: 550:0100'0101'0110'0111 480:(2014) and the next version of 831:Crowl, Lawrence (2012-12-19). 798:Crowl, Lawrence (2007-05-02). 703: 689: 669: 649: 629: 615: 601: 565: 79:of 0), but the value 1 is not 1: 578: 157: 762:Aaron Ballman (2020-12-15). 7: 348: 36:is directly represented in 10: 874: 697:"What's New In Python 3.6" 377:(from GHC version 8.6.1), 201: 118: 833:"N3499: Digit Separators" 800:"N2281: Digit Separators" 764:"N2626: Digit separators" 267:Common suffixes include: 216:Common prefixes include: 711:"Literals and operators" 558: 538:very_long_binary_literal 529:0b0100'1100'0110 502: 426: 421:), and token separator. 332:social security numbers 75:evaluates to 1 (as the 288:for long long integer; 494:user-defined literals 373:(from version 1.13), 298:for unsigned integer. 462:socialSecurityNumber 393:(from version 3.6), 361:(from version 7.0), 453:1234_5678_9012_3456 148:regular expressions 424:Examples include: 381:(from version 7), 328:credit card number 514:1'000'000 278:for long integer; 129:semantic analysis 71:, the expression 865: 837: 836: 828: 822: 821: 819: 810: 804: 803: 795: 789: 788: 786: 777: 771: 770: 768: 759: 753: 752: 750: 741: 735: 734: 727: 721: 720: 718: 717: 707: 701: 700: 693: 687: 686: 684: 683: 673: 667: 666: 664: 663: 653: 647: 646: 644: 643: 633: 627: 626: 619: 613: 612: 605: 599: 592: 572: 569: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 472: 469: 466: 463: 460: 457: 454: 451: 448: 447:creditCardNumber 445: 442: 439: 436: 433: 430: 412: 310:Digit separators 297: 293: 287: 283: 277: 273: 258: 254: 244: 240: 236: 226: 222: 212: 197: 193: 188:constant folding 182: 178: 174: 170: 163:until after the 138: 134: 125:lexical analysis 114: 110: 106: 98: 94: 90: 86: 74: 70: 67:By contrast, in 63: 59: 55: 51: 47: 43: 18:computer science 873: 872: 868: 867: 866: 864: 863: 862: 843: 842: 841: 840: 829: 825: 817: 811: 807: 796: 792: 784: 778: 774: 766: 760: 756: 748: 742: 738: 729: 728: 724: 715: 713: 709: 708: 704: 695: 694: 690: 681: 679: 675: 674: 670: 661: 659: 655: 654: 650: 641: 639: 635: 634: 630: 621: 620: 616: 607: 606: 602: 593: 586: 581: 576: 575: 570: 566: 561: 556: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 508:integer_literal 507: 504: 474: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 410: 351: 312: 295: 291: 285: 281: 275: 271: 256: 252: 242: 238: 234: 224: 220: 210: 204: 195: 191: 180: 176: 172: 168: 160: 136: 132: 121: 112: 108: 104: 96: 92: 88: 87:the expression 84: 72: 68: 61: 57: 53: 49: 45: 41: 22:integer literal 12: 11: 5: 871: 861: 860: 855: 839: 838: 823: 805: 790: 772: 754: 736: 722: 702: 688: 668: 648: 628: 614: 600: 583: 582: 580: 577: 574: 573: 563: 562: 560: 557: 523:binary_literal 503: 427: 419:comma operator 350: 347: 316:digit grouping 311: 308: 300: 299: 289: 279: 265: 264: 250: 232: 203: 200: 159: 156: 137:ListOfIntegers 120: 117: 101:string literal 9: 6: 4: 3: 2: 870: 859: 856: 854: 851: 850: 848: 834: 827: 816: 809: 801: 794: 783: 776: 765: 758: 747: 740: 732: 726: 712: 706: 698: 692: 678: 672: 658: 652: 638: 632: 624: 618: 610: 604: 597: 591: 589: 584: 568: 564: 501: 499: 495: 491: 487: 483: 479: 425: 422: 420: 416: 408: 404: 400: 396: 392: 388: 384: 380: 376: 372: 368: 364: 360: 356: 346: 344: 340: 335: 333: 329: 325: 321: 317: 307: 305: 290: 280: 270: 269: 268: 262: 251: 248: 233: 230: 219: 218: 217: 214: 209: 199: 189: 184: 166: 155: 153: 149: 144: 142: 130: 126: 116: 102: 82: 78: 65: 44:, the string 39: 35: 31: 27: 24:is a kind of 23: 19: 826: 808: 793: 775: 757: 739: 725: 714:. Retrieved 705: 691: 680:. Retrieved 671: 660:. Retrieved 651: 640:. Retrieved 631: 617: 603: 567: 484:as of 2022, 475: 423: 352: 336: 313: 301: 266: 215: 205: 185: 161: 145: 140: 122: 80: 66: 21: 15: 858:Source code 498:upper comma 468:123_45_6789 415:radix point 229:hexadecimal 52:the string 38:source code 847:Categories 716:2019-11-15 682:2015-08-12 662:2019-01-31 642:2020-11-05 579:References 547:'' 432:oneMillion 407:underscore 324:subitizing 320:bit fields 231:(base 16); 179:and value 165:parse tree 158:Evaluation 85:x = 2 + 2, 69:x = cos(0) 438:1_000_000 263:(base 2). 249:(base 8); 192:x = 2 + 2 81:literally 64:prefix). 853:Integers 349:Examples 150:, as in 50:x = 0x10 375:Haskell 211:0x10ULL 202:Affixes 177:integer 173:integer 133:integer 119:Parsing 93:x = "1" 30:integer 28:for an 26:literal 391:Python 367:Eiffel 339:nibble 304:sigils 261:binary 152:Python 77:cosine 73:cos(0) 32:whose 818:(PDF) 785:(PDF) 767:(PDF) 749:(PDF) 559:Notes 490:C++11 478:C++14 403:Swift 383:Julia 247:octal 196:2 + 2 113:"foo" 99:is a 89:2 + 2 42:x = 1 34:value 20:, an 535:auto 520:auto 505:auto 401:and 399:Rust 395:Ruby 387:Perl 379:Java 343:byte 259:for 245:for 227:for 181:0x10 169:0x10 141:just 95:the 54:0x10 486:C23 476:In 459:int 444:int 429:int 355:Ada 353:In 330:or 294:or 284:or 274:or 255:or 241:or 223:or 208:C++ 109:"1" 97:"1" 16:In 849:: 587:^ 397:, 389:, 385:, 371:Go 369:, 365:, 359:C# 357:, 286:LL 282:ll 257:0B 253:0b 243:0O 239:0o 237:, 225:0X 221:0x 183:. 154:. 115:. 62:0x 58:10 835:. 820:. 802:. 787:. 769:. 751:. 733:. 719:. 699:. 685:. 665:. 645:. 625:. 611:. 598:" 594:" 553:; 541:= 532:; 526:= 517:; 511:= 482:C 471:; 465:= 456:; 450:= 441:; 435:= 411:_ 409:( 363:D 296:U 292:u 276:L 272:l 235:0 105:1 46:1

Index

computer science
literal
integer
value
source code
cosine
string literal
lexical analysis
semantic analysis
regular expressions
Python
parse tree
constant folding
C++
hexadecimal
octal
binary
sigils
digit grouping
bit fields
subitizing
credit card number
social security numbers
nibble
byte
Ada
C#
D
Eiffel
Go

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