2020-1-21 The first day of clocking in and learning C++


1. The first C++ program

Write hello world in C++

Example

#include <iostream>  //固定语句
using namespace std;   //固定语句

//1、单行注释

/* 2、多行注释 

	main是一个程序的入口
	每个程序只能有一个main函数
*/

int main()
{
    
    
	//在屏幕中输出hello world
	cout << "hello  world" << endl;

	system("pause");   //固定语句

	return 0;    //固定语句

}

2. Notes on the program

1. Single-line comment: // Comment content
2. Multi-line comment: /* Comment content*/

Three, variables

1. The significance of the existence of variables: it is convenient for us to manage the memory space

2. The syntax of variable creation: data type variable name = variable initial value (int a =10;)

Example

#include <iostream>
using namespace std;

int main()
{
    
    
	//定义一个变量
	int a = 10;

	cout << "a = "<< a << endl;

	system("pause");

	return 0;

}

Fourth, the constant

**1. The meaning of constants: **Used to record unchangeable data in the program

2. Two ways to define constants in C++

(1) #define macro constant #define constant name constant value

== Usually defined at the top of the file ==, which means a constant

**(2) const modified variable **const data type constant name = constant value

== Usually the keyword const == is added before the variable definition to modify the variable as a constant and cannot be modified

Example

#include<iostream>
using namespace std;

//常量的定义
//1.#define 宏常量

#define Day 7

int main()
{
    
    
	//Day = 10;  此处为错误,前面用define 定义后,不可再次更改
	cout << "一周总共有" << Day <<"天"<<endl;

	const int month = 12;
	//const = 13 ;此处错误,用const定义后,其值不可再次修改
	cout << "一年有" << month << "月" << endl;

	system("pause");
	
	return 0;
}

Five, keywords

Don't use keywords to name variables or constants
Insert picture description here

Six, identifier naming rules

**Function:** C++ has its own set of rules when naming identifiers (variables, constants)

  • Identifier cannot be a keyword

  • Identifiers can only consist of letters, numbers, and underscores

  • The first character must be a letter or underscore

  • Letters in identifiers are case sensitive

Seven, data type

C++ stipulates that when creating a variable or constant, the corresponding data type must be specified, otherwise the variable cannot be assignedAllocate memory

7.1 Integer

1. Short integer (short) occupies two bytes

2. Integer (int) occupies four bytes

3. Long Integer (long) Windows occupies four bytes

4. Long long occupies eight bytes

7.2 sizeof keyword

The sizeof keyword can be used == the memory size occupied by the statistical data type ==

Syntax: sizeof (data type/variable)

[Example]

#include<iostream>
using namespace std;

int main()
{
    
    
	//整型 :short (2)  int (4) long (4) long long (8)

	short num1 = 10;
	cout << "short占用的内存空间为" << sizeof(short) << endl;
	cout << "short占用的内存空间为" << sizeof(num1) << endl;

	int num2 = 10;
	cout << "int占用的内存空间为" << sizeof(int) << endl;
	cout << "int占用的内存空间为" << sizeof(num2) << endl;

	long num3 = 10;
	cout << "long占用的内存空间为" << sizeof(long) << endl;
	cout << "long占用的内存空间为" << sizeof(num3) << endl;

	long long num4 = 10;
	cout << "long long占用的内存空间为" << sizeof(long long) << endl;
	cout << "long long占用的内存空间为" << sizeof(num4) << endl;

	system("pause");
	return 0;
}

result
Insert picture description here

7.3 Real type

Represents a decimal

1. Single precision: float occupies four bytes and can be counted7th placeeffective number

2. Double precision: double occupies eight bytes and can be counted15~16Significant digits

Example

#include<iostream>
using namespace std;

//两种实型输出
int main()
{
    
    
	float f1 = 3.14529265f;
	cout << "f1 = " << f1 << endl;

	double d1 = 3.14159265;
	cout << "d1 = " << d1 << endl;

	//科学计数法

	float f2 = 3e2; //3*10^2
	float f3 = 2e-2; //2*0.1^2
	cout << "f2 = " << f2 << endl;
	cout << "f3 = " << f3 << endl;


	system("pause");
	return 0;
}

Output result
Insert picture description here

[Note] In C++, when outputting decimals, 6 significant digits are output by default

7.4 Character type

Display a single character ==
Syntax: char ch ='a'; (apostropheThere can only be
One character ==)

Example

#include<iostream>
using namespace std;

int main()
{
    
    

	//1、字符型变量创建方式
	char ch = 'a';
	cout << ch << endl;

	//2、字符型变量所占用内存大小
	cout <<"char型占用内存:"<< sizeof(char) << endl;

	//3、字符型变量对应ASCII编码
	cout << "a对应的ASCII编码:"<< (int)ch << endl;

	system("pause");
	return 0;
}

Output result
Insert picture description here

7.5 Escape characters

\n Newline, move the current position to the beginning of the next line

In C++, endl also means line break

\t Horizontal tab (skip to the next TAB position), a \t is eight characters

\ Represents a backslash character '\'

Example

#include<iostream>
using namespace std;

int main()
{
    
    
	//换行
	cout << "hello world\n";

	//制表
	cout << "a\tdivition" << endl;
	cout << "aa\tdivition" << endl;
	cout << "aaa\tdivition" << endl;
	cout << "aaaa\tdivition" << endl;

	//单斜杠
	cout << "\\" << endl;

	system("pause");
	return 0;
}

Output result
Insert picture description here

7.6 String type

1. C-style string: char variable name [] = "string value"

2. C++ style string: string variable name = "string value"

Example

#include<iostream>
#include<string>   //若用C++风格定义字符串,则需添加此头文件
using namespace std;

int main()
{
    
    
	//1、C风格
	char str1[] = "hello world" ;
	cout << str1 << endl;

	//2、C++风格
	string str2 = "hello world";
	cout << str2 << endl;


	system("pause");
	return 0;
}

7.7 Boolean type bool

Boolean data types represent true or false values

  • true (essentially 1)

  • false (essentially 0)

Example

#include<iostream>
using namespace std;

int main()
{
    
    
	//1、创建bool类型
	bool flag = false;
	cout << "结果为假时输出:" << flag << endl;

	flag = true;
	cout << "结果为真时输出:" << flag << endl;

	//2、查看bool类型所占内存空间
	cout << "bool类型所占内存空间为:" << sizeof(bool) << endl;

	system("pause");
	return 0;
}

Output result
Insert picture description here

7.8 Data input

Used to get data from the keyboard

Keyword: cin

Syntax: cin >> variable

Example

#include<iostream>
using namespace std;

int main()
{
    
    
	//1、整型
	int a = 0;
	cout << "请给整型变量a赋值:" << endl;
	cin >> a;
	cout<<"整型变量a = "<<a<<endl;

	//2、浮点型

	//3、字符型

	//4、字符串型

	//5、布尔类型
	



	system("pause");
	return 0;
}

Output result
Insert picture description here

[Note] The learning course is-Dark Horse Program C++ Tutorial

Guess you like

Origin blog.csdn.net/qq_42616280/article/details/112917848