Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Integer constant

      From cppreference.com
      <c‎ |language
       
       
       
       

      Allows values of integer type to be used in expressions directly.

      Contents

      [edit]Syntax

      An integer constant is anon-lvalue expression of the form

      decimal-constantinteger-suffix (optional) (1)
      octal-constantinteger-suffix (optional) (2)
      hex-constantinteger-suffix (optional) (3)
      binary-constantinteger-suffix (optional) (4)(since C23)

      where

      • decimal-constant is a non-zero decimal digit (1,2,3,4,5,6,7,8,9), followed by zero or more decimal digits (0,1,2,3,4,5,6,7,8,9)
      • octal-constant is the digit zero (0) followed by zero or more octal digits (0,1,2,3,4,5,6,7)
      • hex-constant is the character sequence0x or the character sequence0X followed by one or more hexadecimal digits (0,1,2,3,4,5,6,7,8,9,a,A,b,B,c,C,d,D,e,E,f,F)
      • binary-constant is the character sequence0b or the character sequence0B followed by one or more binary digits (0,1)
      • integer-suffix, if provided, may contain one of the following (except the unsigned prefix can be combined with one of the others; if two suffixes are used they can appear in any order):
      • unsigned-suffix (the characteru or the characterU)
      • long-suffix (the characterl or the characterL)or thelong-long-suffix (the character sequencell or the character sequenceLL)(since C99)
      • bit-precise-int-suffix (the character sequencewb or the character sequenceWB)(since C23)

      Optional single quotes (') may be inserted between the digits as a separator. They are ignored by the compiler.

      (since C23)

      [edit]Explanation

      1) Decimal integer constant (base 10, the first digit is the most significant).
      2) Octal integer constant (base 8, the first digit is the most significant).
      3) Hexadecimal integer constant (base 16, the first digit is the most significant, the lettersa throughf represent the decimal values 10 through 15).
      4) Binary integer constant (base 2, the first digit is the most significant).

      The following variables are initialized to the same value:

      int d=42;int o=052;int x=0x2a;int X=0X2A;int b=0b101010;// C23

      The following variables are also initialized to the same value:

      unsignedlonglong l1= 18446744073709550592ull;// C99unsignedlonglong l2=18'446'744'073'709'550'592llu;// C23unsignedlonglong l3=1844'6744'0737'0955'0592uLL;// C23unsignedlonglong l4=184467'440737'0'95505'92LLU;// C23

      [edit]The type of the integer constant

      The type of the integer constant is the first type in which the value can fit, from the list of types which depends on which numeric base and whichinteger-suffix was used.

      Types allowed for integer constants
      suffix decimal bases other bases
      no suffixint

      longint
      unsignedlongint(until C99)
      longlongint(since C99)

      int

      unsignedint
      longint
      unsignedlongint
      longlongint(since C99)
      unsignedlonglongint(since C99)

      u orUunsignedint

      unsignedlongint
      unsignedlonglongint(since C99)

      unsignedint

      unsignedlongint
      unsignedlonglongint(since C99)

      l orLlongint

      unsignedlongint(until C99)
      longlongint(since C99)

      longint

      unsignedlongint
      longlongint(since C99)
      unsignedlonglongint(since C99)

      bothl/L andu/Uunsignedlongint

      unsignedlonglongint(since C99)

      unsignedlongint

      unsignedlonglongint(since C99)

      ll orLLlonglongint(since C99)longlongint(since C99)

      unsignedlonglongint(since C99)

      bothll/LL andu/Uunsignedlonglongint(since C99)unsignedlonglongint(since C99)
      wb orWB_BitInt(N) where the width N is the smallest N greater than 1 which can accommodate the value and the sign bit(since C23)_BitInt(N) where the width N is the smallest N greater than 1 which can accommodate the value and the sign bit(since C23)
      bothwb/WB andu/Uunsigned _BitInt(N) where the width N is the smallest N greater than 0 which can accommodate the value(since C23)unsigned _BitInt(N) where the width N is the smallest N greater than 0 which can accommodate the value(since C23)

      If the value of the integer constant is too big to fit in any of the types allowed by suffix/base combination,it does not have suffixeswb,WB,uwb, orUWB(since C23) and the compiler supports extended integer types (such as__int128), the constant may be given the extended integer type; otherwise, the program is ill-formed.

      [edit]Notes

      Letters in the integer constants are case-insensitive:0xDeAdBaBeU and0XdeadBABEu represent the same number (one exception is thelong-long-suffix, which is eitherll orLL, neverlL orLl)(since C99).

      There are no negative integer constants. Expressions such as-1 apply theunary minus operator to the value represented by the constant.

      When used in a controlling expression of#if or#elif, all signed integer constants act as if they have typeintmax_t and all unsigned integer constants act as if they have typeuintmax_t.

      (since C99)

      Integer constants may be used ininteger constant expressions.

      Due tomaximal munch, hexadecimal integer constants ending ine andE, when followed by the operators+ or-, must be separated from the operator with whitespace or parentheses in the source:

      int x=0xE+2;// errorint y=0xa+2;// OKint z=0xE+2;// OKint q=(0xE)+2;// OK

      Otherwise, a single invalid preprocessing number token is formed, which causes further analysis to fail.

      [edit]Example

      Run this code
      #include <inttypes.h>#include <stdio.h> int main(void){printf("123 = %d\n",123);printf("0123 = %d\n",0123);printf("0x123 = %d\n",0x123);printf("12345678901234567890ull = %llu\n", 12345678901234567890ull);// the type is a 64-bit type (unsigned long long or possibly unsigned long)// even without a long suffixprintf("12345678901234567890u = %"PRIu64"\n", 12345678901234567890u); // printf("%lld\n", -9223372036854775808); // Error:// the value 9223372036854775808 cannot fit in signed long long, which// is the biggest type allowed for unsuffixed decimal integer constant printf("%llu\n",-9223372036854775808ull);// unary minus applied to unsigned value subtracts it from 2^64,// this gives unsigned 9223372036854775808 printf("%lld\n",-9223372036854775807ll-1);// correct way to form signed value -9223372036854775808}

      Output:

      123 = 1230123 = 830x123 = 29112345678901234567890ull = 1234567890123456789012345678901234567890u = 123456789012345678909223372036854775808-9223372036854775808

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.4.4.1 Integer constants (p: 57-60)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.4.4.1 Integer constants (p: 45-46)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.4.4.1 Integer constants (p: 62-64)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.4.4.1 Integer constants (p: 54-56)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.1.3.2 Integer constants

      [edit]See also

      C++ documentation forInteger literal
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/integer_constant&oldid=159816"

      [8]ページ先頭

      ©2009-2025 Movatter.jp