C++day01光速入门指南--基本数据类型和输入输出

Clion的基本使用


插件安装
single file execution

打开cmakelist.txt

cmake_minimum_required(VERSION 3.19) 这是指定cmake执行的最小版本
project(c_workspace1)  项目名

set(CMAKE_CXX_STANDARD 11) # 语言版本

add_executable(c_workspace1 demo1.cpp) 将我们的cpp文件注册到cmakelist中
第一个参数是项目名, 第二个参数cpp文件名

自己编写一个helloword文件

#include <iostream>
using namespace std;

int main(){
    cout<< "hello" << endl;
}

增加第二个文件的操作

基本shell命令

tab键可以进行代码补全, / 是根目录, ~是用户目录

  • 切换文件夹
cd 目录名
  • 列出文件夹中所有文件列表
ls
  • 当前所在目录
pwd 
  • 清屏
clear 
  • 后退一级目录
cd .. 
  • 建立day01文件夹
mkdir day01
  • 建立demo1.cpp文件
touch demo1.cpp
  • 编辑demo1.cpp
vim demo1.cpp

i 是进入编辑状态; 编辑完毕后按esc ; 输入 :wq保存退出

  • 查看demo1.cpp
cat demo1.cpp
  • 编译c++文件
g++ -o demo1 demo1.cpp 
g++ -o 源文件名  编译源文件
  • 执行 demo1 这个可执行文件
./demo1

在这里插入图片描述

  • 删除hello.txt
rm -f hello.txt 
  • 重名命名文件
mv demo1.cpp demo2.cpp
  • 移动文件
mv demo2.cpp /Users/ericli/CLionProjects/houjiheng/demo2.cpp
  • 复制
cp demo2.cpp /Users/ericli/CLionProjects/houjiheng/demo2.cpp

C编译

使用gcc -o 编译文件的名字 源文件

延展Java的编译

在这里插入图片描述

C++注释

本身不会被执行, 是给别人看的, 对程序的解释
单行注释 //
多行注释 /* */

变量

给⼀段指定的内存空间起名,⽅便操作这段内存
格式: 数据类型 变量名 = 变量值;

#include <iostream>
using namespace std;

int main(){
    // c++中创建变量是必须要给变量一个初始值,否则会报错
    // 第一种变量声明方式. 声明+初始化
    int a = 888;
    a = 333;
    cout<< "a = " << a << endl;
    // 第二种变量声明方式  先声明 在初始化
    int b;
    b = 999;
    cout<< "b = " << b << endl;
}

常量

作用: ⽤于记录程序中不可更改的数据
C++定义常量两种⽅式

1. #define 宏常量: #define  常量名 常量值  通常在⽂件上⽅定义,表⽰⼀个常量
2. const修饰的变量 const 数据类型 常量名  = 常量值 
通常在变量定义前加关键字const,修饰该变量为常量,不可修改

举个例子

#include <iostream>
#define day 7
#define PI 3.14
using namespace std;
// 常量
int main(){
    // 宏常量
    cout<< "a week have "<< day << " days"<< endl;
    cout<< "PI = "<< PI << endl;
    // const 修饰变量
    const int month = 12;
//    month = 13;  常量是不能修改的 会报错
    cout<< "month = "<< month << endl;
}

关键字(保留字)

注意: 在定义变量或者常量时候,不要⽤关键字
作⽤:关键字是C++中预先保留的单词(标识符)

标识符

C++规定给标识符(变量、常量)命名时,有⼀套⾃⼰的规则

  • 标识符不能是关键字
  • 标识符只能由字⺟、数字、下划线(美元符也行)组成
  • 第⼀个字符必须为字⺟或下划线
  • 标识符中字⺟区分⼤⼩写
#include <iostream>
using namespace std;

int main(){
   # 两个变量
    int num = 100;
    int NUM = 200;
    cout<< num << endl;
    cout<< NUM << endl;
}

数据类型

C++规定在创建⼀个变量或者常量时,必须要指定出相应的数据类型,否则⽆法给变量分配内存
1 字节(Byte) = 8 位(bit)

  • 整型
    整型变量表⽰的是整数类型的数据
sizeof关键字

利⽤sizeof关键字可以统计数据类型所占内存⼤⼩
语法: sizeof( 数据类型/ 变量)

#include <iostream>
using namespace std;
// size of
int main(){
    int a =100;
    cout<< "short = "<< sizeof(short)<<endl;
    cout<< "int = "<< sizeof(int)<<endl;
    cout<< "long = "<< sizeof(long)<<endl;
    cout<< "long long = "<< sizeof(long long )<<endl;
    cout<< "a = "<< sizeof(a)<<endl;
    // 结论
    // short < int <= long <= long long
}

浮点型(小数/实型)

浮点型有两种

  • 单精度float
  • 双精度double
    两者的区别在于表示有效数字范围不同
#include <iostream>
using namespace std;

int main(){
    cout.precision(10);// 设置consle输出有效数字的范围
    float f1 = 3.141592653f;//整数部分也算有效位的范围
    double d1 = 3.141592653;
    cout<< f1 << endl;
    cout<< d1 << endl;
    cout<<"float sizeof = " <<sizeof(f1)<< endl; // 4
    cout<<"double sizeof = " <<sizeof(d1)<< endl; // 8
    // 科学计数法
    float f2 = 3e2; // 3*10^2
    cout<<"f2 = " <<f2<< endl; //
    float f3 = -4e3; // -4*10^3
    cout<<"f3 = " <<f3<< endl; //
    float f4 = 4e-2; // 4* 10 -2 次方 计算机浮点数计算会损失精度
    cout<<"f4 = " <<f4<< endl; //

}

字符型 (char型)

作用: 字符型变量⽤于显⽰单个字符

  • C和C++中字符型变量只占⽤1个字节
  • 字符型变量并不是把字符本⾝放到内存中存储,⽽是将对应的ASCII编码放⼊到存储单元
#include <iostream>
using namespace std;
// 字符型
int main(){
    char ch = 'a'; // 单引号
    cout<<"ch = " <<ch<< endl; //
    cout<<"size of char " <<sizeof(ch)<< endl; // 占一个字节
//    ch = 'abcde'; // 错误 单引号内只能是一个字符
//    ch = "abcde"; // 错误 不能用双引号
    // 查看字符对应的 ASCII码
    cout<<"a ASCII码 = " <<(int)ch<< endl; // 97
    char ch2 = 'A'; // 单引号
    cout<<"A ASCII码 = " <<(int)ch2<< endl; // 97
    // 可以直接用ascii码给字符型变量赋值
    ch = 97;
    char ch3 = 98;
    cout<<"ch = " <<ch<< endl; // 97
    cout<<"ch3 = " <<ch3<< endl; // 97
}

ASCII 码⼤致由以下两部分组成:

  • ASCII ⾮打印控制字符: ASCII 表上的数字 0-31 分配给了控制字符,⽤于控制像打印机等⼀些外围 设备。
  • ASCII 打印字符:数字 32-126 分配给了能在键盘上找到的字符,当查看或打印⽂档时就会出现。

转义字符

作⽤:⽤于表⽰⼀些不能显⽰出来的ASCII字符
现阶段我们常⽤的转义字符有: \n \ \t

#include <iostream>
using namespace std;
// 转义字符
int main(){
    // \本身代表转义的意思, 他已经不是本身\的意思了
    cout<< "\\" << endl; // 只输出一个斜杠
    //    换行
    cout<< "hello\nhaha" << endl; // 只输出一个斜杠
    // 等价于tab键
    cout<< "hello\thaha" << endl; // 只输出一个斜杠
}

字符串型

作⽤:⽤于表⽰⼀串字符
两种风格

    1. C风格字符串 : char 变量名[] = “字符串值”
#include <iostream>
using namespace std;
// 字符串
int main(){
    char str1[] = "hello world";
    cout<< str1 << endl;
}
    1. C++风格字符串 : string 变量名 = “字符串值”
#include <iostream>
#include <string>
using namespace std;
// 字符串
int main(){
    char str1[] = "hello world";
    cout<< str1 << endl;
    string str = "hello world";
    cout<< str << endl;
}

c++风格字符串需要引入头文件#include

布尔数据类型 bool

布尔数据类型代表真或假的值
布尔数据类型代表真或假的值
bool类型只有两个值:

  • true — 真(本质是1)
    -false — 假(本质是0)
    bool类型占1个字节⼤⼩
#include <iostream>
using namespace std;

int main(){
    bool flag = true;
    cout<< flag << endl;// 1
    flag = false;
    cout<< flag << endl;// 0
    cout<< "bool size" <<sizeof(flag)<< endl;// 1
}

console输入

cin >> 变量

#include <iostream>
using namespace std;

int main(){
    // 整型输入
    int a;
    cout << "please input a number "<<endl;
    cin>>a;
    cout << "a = "<< a << endl;

    // 浮点型输入
    double d;
    cout << "please input double number "<<endl;
    cin>>d;
    cout << "d = "<< d << endl;

    // 字符型输入
    char ch;
    cout << "please input a char "<<endl;
    cin>>ch;
    cout << "ch = "<< ch << endl;

    // 字符串型输入
    string str;
    cout << "please input a string "<<endl;
    cin>>str;
    cout << "str = "<< str << endl;

    // 布尔型输入
    bool flag;
    cout << "please input a bool value "<<endl;
    cin>>flag;
    cout << "flag = "<< flag << endl;
}

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值