C++学习笔记(1):常量、整型和实型、字符和字符串


本博文是学习黑马程序员C++视频时做的笔记,记录一下只是方便温故知新,不做其他用途。

一、常量define和const

#include <iostream>
using namespace std;
//    常量:用于记录程序中不可更改的数据
//1 #define 宏常量 常量值
#define Day 7

int main()
{
//    Day = 8;//会报错 
    //2  const修饰变量
    const int month = 12;
    return 0;
}

二、整型(short、int、long、long long)和实型(float、double)

整型:用于表示整数

#include <iostream>
using namespace std;

int main()
{
//整型:1字节=8位
//1 short 2字节(-2^15-2^15-1)即-32768-32767
    short num1 = 32769;
    cout<<"short num1="<<num1<<endl;//-32767 超出范围 
//2 int 4字节  (-2^31-2^31-1)
//3 long win32 4字节 ;win64 8字节;(-2^31-2^31-1)
//4 long long 8字节(-2^63-2^63-1)
    return 0;
}

实型:用于表示小数

#include <iostream>
using namespace std;

int main()
{
//      1 单精度 float 占用4字节
    float f1 = 3.1415926f;
//   默认输出6位有效数字
    cout << "f1 = "<<f1<<endl;//f1 = 3.14159 
    cout << "f1 size= "<<sizeof(f1)<<endl;//f1 size= 4 
//      2 双精度 double 占用8字节
    double d1 = 3.1415926;
//    默认输出6位有效数字
    cout << "d1 = "<<d1<<endl;//d1 = 3.14159 
    cout << "d1 size= "<<sizeof(d1)<<endl;//d1 size= 8
    return 0;
}

三、字符和字符串

#include <iostream>
//需要引用<string>
#include <string>
using namespace std;

int main()
{
//    字符型变量:用于显示单个字符,占用一个字节,只能用单引号
    char ch = 'a';
    cout << "ch = "<<ch<<endl;//ch = a
    cout << "Ascall(a) = "<<int(ch)<<endl;//Ascall(a) = 97
    char ch1 = 'A';
    cout << "Ascall(A) = "<<int(ch1)<<endl;//Ascall(a) = 65
//    字符串变量
    string str1 = "Hello world!";
    cout <<str1<<endl;//Hello world!

    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值