The Two Types of Constants in C Programming
The two types of constants are Real constants and Character constants. There are also Numeric and String constants.
Character constants
Character constants are used in C programs to describe a character. They are a byte-wide sequence of characters, usually letters or numbers. The value of a character constant is a numerical representation of that character. Constants may be real or imaginary, floating-point or integer, and can be single, double, or multi-byte.
The value of a character constant is interpreted in terms of the source character set or the implementation character set. If the source character set is not defined, the corresponding value is the machine byte-wide code of that character. In other words, it is the ASCII character for that character.
When defining a character constant in C, you can use one of two different character quotes: the’s’ or the ‘h’. You can also write a special character in an octal form. However, the character quote is not important to NASM.
Escape sequences are predefined characters that appear in output functions, such as ‘printf’. These sequences begin with a backslash and include the character from the table. Generally, the first nonhexadecimal character is the end of the escape sequence.
The escape sequences are typically expressed in hexadecimal format, but the characters can appear in a range of other forms. For example, a ‘L’ can be used to indicate that the value is a wide character, while a ‘0x’ specifies a hexadecimal version.
Unlike string constants, character constants do not allow blanks. Therefore, they are only valid if they contain at least one digit. But this doesn’t mean that the length is restricted. This can be as large as 2 billion bytes.
Floating-point constants have an integer value, but they may contain a decimal part. The decimal part can be a positive or negative number.
String constants
String constants are a way to represent a character or data structure. The value of a string constant can be assigned to a variable. It can also be used as a return value in a function. A string constant may be set to display a message such as ‘password’. In addition, a string constant may contain special character escape sequences.
Although a string constant may be any length, the most common is an arbitrary sequence of characters enclosed in double quotation marks. A character string can contain up to 70 characters.
The ISO/IEC 8859-1 character set is used. If a character has code zero, it cannot be a string constant.
This doesn’t mean that all type names are available. However, a string constant’s size is restricted by the memory space DTrace can consume. On 64-bit machines, this size is eight bytes.
One of the most common mistakes in programming with C strings is forgetting to allocate space for a null character at the end. This is due to the fact that the compiler uses instruction pointer relative addressing. Consequently, if a string constant is larger than the contents of the memory space, the vertical scroll bar is not displayed.
To create a string constant, you need to open the string header library. You then need to select the appropriate constant in the list. Right-clicking the constant will launch the Edit dialog box.
Unlike a string literal, a string constant doesn’t change in value while the VI runs. It is therefore a good idea to indicate its status by putting it into a label.
While there isn’t a single best way to write a string constant, the most important thing is to make sure it is marked as a constant. When you do, the compiler will automatically intern the string constant and it will only be consumed once.
Integer constants
Integer constants are used in programming to represent numbers. They can be hexadecimal, decimal or octal. A hexadecimal integer constant is composed of a sequence of digits, starting with 0 and ending with 9.
Decimal integer constants can be positive or negative. A positive integer constant can have a plus sign before the value. The minus sign must be present before a negative integer constant.
Integer constants are typically used as masks in code. However, they can also be used in expressions. Several factors will determine the type of integer constant. For example, the case of letters can affect the KIND type parameter of INTEGER data.
An octal integer constant is defined by a sequence of digits, ranging from 0 to 7. Using hexadecimal values, octal constants are identified by the prefix “0x”.
Constants without a suffix are usually assigned unsigned int. If the constant has no suffix, it can be assigned either short or unsigned long int.
Integer constants are not permitted to include fractional parts. Also, integer constants cannot contain period or blanks.
The range of integer constants in C depends on the compiler. However, the smallest literal representation of a constant is -2 147 483 647. This is the smallest representation of a big integer constant.
When using integer constants, you should always specify the correct type. Usually, the first digit is the most important. Otherwise, you may get an error.
Floating-point constants must have a nonnegative value. If a constant is large enough, the compiler will use an extended type. Examples include 6.3f and 59L.
To get the maximum number of digits in an integer constant, use the scale option. Alternatively, you can add a leading or a trailing sign.
Numeric constants
There are two main types of numerical constants: integer and character constants. Integer and character constants can be used together or alone. Both types have similar attributes, such as being non-negative or not a null value, having a value that does not change after definition, and having a NOT NULL attribute.
Integer constants are defined by a sequence of digits ranging from 0 to 9. They are unsigned. However, they can be combined with octal or hexadecimal numbers. In addition, they can be mixed with a decimal number to form a large integer. The data type of an integer constant is a big integer.
Decimal constants are signed or unsigned. They can have an optional decimal exponent. This part of the number after the exponent is called the mantissa. Depending on the number’s size, it can be positive or negative. If the exponent has a plus or minus sign, it is the optional multiplier.
Complex constants are ordered pairs of integer constants. They behave like floating-point constants, but are enclosed in parentheses. Unlike floating-point constants, complex constants are not interpreted as strings. A complex constant can have an exponent or an irrational part.
Character constants are single characters. They are treated as byte-wide code and have an equivalent integer value. They are usually written in uppercase and hexadecimal, but can be written in other basic data types.
Floating-point constants are also represented in conventional notation. These constants are interpreted as strings if they are not preceded by a numeric prefix. Their internal representations can vary according to the compiler.
Decimal constants can have an optional decimal point and a period. Similarly, float constants can have an optional exponent. Likewise, octal constants can have an optional decimal point, a period, or a + or – sign.
Real constants
Real constants are mathematical representations of a numerical value. They are generally represented as numbers in decimal notation. However, they can also be represented in fractional and exponential forms. The most important thing to remember is that a real constant is a numerical value that has a decimal point and an exponent. It can be either positive or negative.
In addition, a real constant may also be double precision or single precision. A double precision numeric constant is one that contains 16 or 17 digits, depending on its type. Single precision numeric constants contain six digits.
The exponent for a real constant is a letter E. If you don’t see the letter E in a real constant, the value of the exponent will be assumed to be zero.
An exponent is a mathematical representation of the power of ten. The number of digits in the exponent can be a decimal number, a fractional number, or an integer. There is an optional plus or minus sign at the beginning of the positive exponent, which indicates that the exponent is larger than the value of the number. Similarly, there is an optional minus sign at the beginning of the negative exponent, which indicates that the exponent value is smaller than the number.
As for the exponent, it can be written in lower or uppercase. If the exponent is a signed integer, the letter E is acceptable. Likewise, if the exponent is a quad-precision real constant, the letter Q is acceptable.
One of the main differences between a numeric and a character constant is that a numeric constant can be positive, negative or zero. In contrast, a character constant can only be positive or negative, or contain a digit, an alphabet, or a character.