栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习笔记之基础认识

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习笔记之基础认识

提示:文章# C++学习
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用

文章目录
  • 一、C++标识符
  • 二、C++注释
  • 三、C++数据类型
  • 总结


一、C++标识符

C++ 标识符是用来标识变量、函数、类、模块,或任何其他用户自定义项目的名称。一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)。
注:即不以数字开头以及不与关键字相同。
下表列出了 C++ 中的保留字。这些保留字不能作为常量名、变量名或其他标识符名称。

asmelsenewthis
autoenumoperatorthrow
boolexplicitprivatetrue
breakexportprotectedtry
caseexternpublictypedef
catchfalseregistertypeid
charfloatreinterpret_casttypename
classforreturnunion
constfriendshortunsigned
const_castgotosignedusing
continueifsizeofvirtual
defaultinlinestaticvoid
deleteintstatic_castvolatile
dolongstructwchar_t
doublemutableswitchwhile
dynamic_castnamespacetemplate

同时:C/C++可以使用带有 $ 的标识符:

#include "stdio.h"

int main(){
    int $8=0x99;   //它甚至可以这样
    printf("%dn", $8);
    return 0;
}
二、C++注释
  • 在代码前加"//",这种注释仅能注释此符号后内容,不跨行。
  • 将代码包括""这两个符号之间,可以跨行,但不能嵌套使用。
  • 此外,#if … #endif 虽属于条件编译,却也可以作为注释,如:
#if 0
   code
#endif 

三、C++数据类型

C++中七种基本的数据类型有bool ,int ,char ,float ,double , void , wchar_t 。

其实 wchar_t 是这样来的:typedef short int wchar_t;
所以wchar_t的申请空间和short int 一样大的

一些基本类型可以使用一个或多个类型修饰符进行修饰:

  • signed … 有符号
  • unsigned … 无符号
  • short …短
  • long…长

变量的大小会根据编译器和所使用的电脑而有所不同。
下面实例会输出您电脑上各种数据类型的大小。

#include  
#include 
using namespace std;  
int main()  
{  
    cout << "type: tt" << "************size**************"<< endl;  
    cout << "bool: tt" << "所占字节数:" << sizeof(bool);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "tt最小值:" << (numeric_limits::min)() << endl;  
    cout << "char: tt" << "所占字节数:" << sizeof(char);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "tt最小值:" << (numeric_limits::min)() << endl;  
    cout << "signed char: t" << "所占字节数:" << sizeof(signed char);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "tt最小值:" << (numeric_limits::min)() << endl;  
    cout << "unsigned char: t" << "所占字节数:" << sizeof(unsigned char);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "tt最小值:" << (numeric_limits::min)() << endl;  
    cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "tt最小值:" << (numeric_limits::min)() << endl;  
    cout << "short: tt" << "所占字节数:" << sizeof(short);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "tt最小值:" << (numeric_limits::min)() << endl;  
    cout << "int: tt" << "所占字节数:" << sizeof(int);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "long: tt" << "所占字节数:" << sizeof(long);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "unsigned long: t" << "所占字节数:" << sizeof(unsigned long);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "double: t" << "所占字节数:" << sizeof(double);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "long double: t" << "所占字节数:" << sizeof(long double);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "float: tt" << "所占字节数:" << sizeof(float);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "size_t: t" << "所占字节数:" << sizeof(size_t);  
    cout << "t最大值:" << (numeric_limits::max)();  
    cout << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "string: t" << "所占字节数:" << sizeof(string) << endl;  
    // << "t最大值:" << (numeric_limits::max)() << "t最小值:" << (numeric_limits::min)() << endl;  
    cout << "type: tt" << "************size**************"<< endl;  
    return 0;  
}

typedef int bao:为int类型定义了一个新名字为bao,实际上他们是同一种类型。

如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型:

enum 枚举名{ 
     标识符[=整型常数], 
     标识符[=整型常数], 
... 
    标识符[=整型常数]
} 枚举变量;

例如,下面的代码定义了一个颜色枚举,变量 c 的类型为 color。最后,c 被赋值为 “blue”。

enum color { red, green, blue } c;
c = blue;

因为没有对red,green,blue这几个整型元素初始化,故默认red = 0,后面依次加 1 。

总结

感谢菜鸟教程!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/512019.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号