Rules for Constructing Integer Constants in C
There are a few rules to follow when constructing integer constants in C:
- Integer constants can be written in decimal (base 10), octal (base 8), or hexadecimal (base 16) notation. Decimal constants are written using the digits 0-9, octal constants are prefixed with a 0, and hexadecimal constants are prefixed with 0x or 0X. For example:
- Decimal: 10, -5, 0
- Octal: 012, -07, 00
- Hexadecimal: 0xA, -0xF, 0X0
- Integer constants can have an optional suffix to specify their type. The suffixes
u
orU
indicate an unsigned integer, and the suffixesl
orL
indicate a long integer. If no suffix is specified, the constant is assumed to be of typeint
. For example:- Unsigned int: 10u, 100U
- Long int: 100l, 1000L
- Integer constants can include underscores for readability. These underscores are ignored by the compiler. For example:
- Decimal: 1_000_000, -500_000
- Hexadecimal: 0xFF_FF_FF
- The minimum and maximum values of integer constants depend on their type. The
INT_MIN
andINT_MAX
macros in thelimits.h
header file define the minimum and maximum values for theint
type. TheLONG_MIN
andLONG_MAX
macros define the minimum and maximum values for thelong
type. TheULLONG_MAX
macro defines the maximum value for theunsigned long long
type. - Integer constants can be converted to different types using type casting. For example, you can convert an
int
constant to along
by using a type cast like this:(long) 10
.
Leave a Comment