cpp_data_type_arrays_pointers
Integers
Use the int keyword to define the integer data type.
int a =42 ;
Several of the basic types, including integers, can be modified using one or more of these type modifiers:
- signed: A signed integer can hold both negative and positive numbers.
- unsigned: An unsigned integer can hold only positive values.
- short: Half of the default size.
- long: Twice the default size.
For example:
unsigned long int a;
The integer data type reserves 4-8 bytes depending on the operating system.
Floating Point Numbers
A floating point type variable can hold a real number, such as 420.0, -3.33, or 0.03325.
The words floating point refer to the fact that a varying number of digits can appear before and after the decimal point. You could say that the decimal has the ability to “float”.
There are three different floating point data types: float, double, and long double.
In most modern architectures, a float is 4 bytes, a double is 8, and a long double can be equivalent to a double (8 bytes), or 16 bytes.
For example:
double tem = 4.21;
Floating point data types are always signed, which means that they have the capability to hold both positive and negative values.
Strings
A string is an ordered sequence of characters, enclosed in double quotation marks.
It is part of the Standard Library.
You need to include the library to use the string data type. Alternatively, you can use a library that includes the string library.
#include <string>
using namespace std;
int main() {
string a = "I am learning C++";
return 0;
}
The library is included in the library, so you don’t need to include separately, if you already use .
Characters
A char variable holds a 1-byte integer. However, instead of interpreting the value of the char as an integer, the value of a char variable is typically interpreted as an ASCII character.
A character is enclosed between single quotes (such as ‘a’, ‘b’, etc).
For example:char test = 'S
;
American Standard Code for Information Interchange (ASCII) is a character-encoding scheme that is used to represent text in computers.
Booleans
Boolean variables only have two possible values: true (1) and false (0).
To declare a boolean variable, we use the keyword bool.
bool online = false;
bool logged_in = true;
If a Boolean value is assigned to an integer, true becomes 1 and false becomes 0.
If an integer value is assigned to a Boolean, 0 becomes false and any value that has a non-zero value becomes true.
Variable Naming Rules
Use the following rules when naming variables:
- All variable names must begin with a letter of the alphabet or an underscore( _ ).
- After the initial letter, variable names can contain additional letters, as well as numbers. Blank spaces or special characters are not allowed in variable names.
There are two known naming conventions:
- Pascal case: The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. For example: BackColor
- Camel case: The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example: backColo
- C++ keyword (reserved word) cannot be used as variable names.
For example, int, float, double, cout cannot be used as a variable name.
There is no real limit on the length of the variable name (depends on the environment), but try to keep your variable names practical and meaningful.
Case-Sensitivity
C++ is case-sensitive, which means that an identifier written in uppercase is not equivalent to another one with the same name in lowercase.
For example, myvariable is not the same as MYVARIABLE and not the same as MyVariable.
These are three different variables.
Choose variable names that suggest the usage, for example: firstName, lastName.
上一篇: jdk1.8时间API