We think this is one of the basic part of c + + program that deserves to know more, that is constant. In c + + program, there are three ways to make a constant. The first way is the literal, the second is to define constants, and the third is to declare a constant. The literal is divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.
Literal: Integer Numerals
Function defines an integer constant. As we have explained earlier on how to declare a variable, then it must be an integer constant begins with the statement int. When using an unsigned, then we must add the letter u from a numerical. When using a long int then it should add the letter l from a numerical. When using the unsigned long int, then it should add the letters ul, on a numeric.
Meanwhile, to make a decimal numeric, then we simply write it as it is numerical, without the quotes. As for writing an octal number, then the numerical form it should be preceded by a 0. As for writing hexadecimal numbers, then the numerical form it must be preceded by 0x.
Literal: Floating-Point Numerals
The purpose of the floating-point is to express numerals and decimal or exponent. So writing a decimal can use this method, while for the exponent a numerical order (10 ^ (x), where x is a certain number) can be represented by the character e. We can also incorporate decimals and exponents. Basically, floating point is double. Meanwhile, if you want to write a long double decimal then we can add the letter L on the numeric. As for the float is to add the letter f.
Literal: Character
Every single character should be written in single quotes ('). Every single character is a non-numeric constants.
Literal: String
Each set of characters (more than one character) is a string when written in double quotes ("). There are some special codes that are owned by the characters and strings that can function in certain circumstances.
Special code | Meaning |
\n | newline |
\r | carriage return |
\t | tab |
\v | vertical tab |
\b | backspace |
\f | form feed |
\a | alert |
\' | single quote |
\" | double quote |
\? | question mark |
\\ | backslash |
Literal: Boolean
Boolean is the most frequently used by game programmers. Because the boolean works based on mathematical logic. If correct it will be worth one, if one is zero. As each declare a boolean variable, must be preceded bool statement.
Defining a constant
This method can save computer memory consumption. You can make a name for a constant, then used freely in your program. The trick is simple, simply by using # define preprocessor directive.
Declaring a constant
This method is the application of the creation of a variable. The emphasis is to add a statement const.
Post a Comment