CPP03 Data Types, 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.

Arrays

An array is used to store a collection of data, but it may be useful to think of an array as a collection of variables that are all of the same type.
Instead of declaring multiple variables and storing individual values, you can declare a single array to store all the values.
When declaring an array, specify its element types, as well as the number of elements it will hold.
For example:
int a[5];
In the example above, variable a was declared as an array of five integer values [specified in square brackets].
You can initialize the array by specifying the values it holds:
int b[5] = {11, 45, 62, 70, 88};
The values are provided in a comma separated list, enclosed in {curly braces}.

The number of values between braces { } must not exceed the number of the elements declared within the square brackets [ ].

Initializing Arrays

If you omit the size of the array, an array just big enough to hold the itialization is created.
For example:
int b[] = {11, 45, 62, 70, 88};
This creates an identical array to the one created in the previous example.
Each element, or member, of the array has an index, which pinpoints the element’s specifiec position.
The array’s first member has the index of 0, the second has the index of 1.
So, for the array b that we declared above:

**加粗样式**
To access array elements, index the array name by placing the element’s index in square brackets following the array name.

For example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值