typedef struct student
{int age;char name[10];int num;
}student;int main()
{//在c++中可以利用花括号进行初始化struct student student1{12,"zs",123456 };int a = 10, b = 20;int b{ 20 }, a{ 10 };double c{ 20 };int* p{ nullptr };int arr[10]{ 1,2,3,4,5,6,7 };
}c语言的输入输出
int main()
{int a = 10;char b = '\0';scanf_s("%d %c", &a, &b);printf("%d %c", a, b);return 0;
}c++的输入输出
#include
using namespace std;
//cin->stdin
// >> 提取符号
// cout->stdout
// << 插入符
// endl;->'\n'
// cerr->stderr
int main()
{int a = 0;char ch = '\0';cin >> a >> ch; //scanf与cin的区别 1.& 2.输入格式cout << a << " " << ch << endl;
} 在使用cin标准输入流以及cout标准输出流的时候,必须引入头文件
输入字符串
int main()
{const int a = 10;char str[a]{};//scanf("%s", str);//不安全 存在数组的溢出scanf_s("%s", str, a);cin >> str;//不安全,把空格作为字符串的定界符cin.getline(str, a);cin.getline(str, a, '#');//以#作为字符串的结束
}int main()
{const int a = 10; int b = 0;int* p =(int*) &a;*p = 100;b = a;printf("%d %d %d", a, *p, b);
}在c编译器上的运行结果:

在c++编译器上的运行结果:

为什么运行结构会不同呢?
总结一:
c语言中的const
const修饰的变量是只读的,本质还是变量
const 修饰的局部变量在栈上分配空间
const修饰的全局变量在只读存储区分配空间
const只在编译期有用,在运行期无用(编译的时候只能替换数值,不能确定地址。在执行的时候可以确定地址。)
C语言中的const使得变量具有只读属性
const将具有全局生命周期的变量存储于只读存储区
c++中的const
C++在C的基础上对const进行了进化处理
当碰见const声明时在符号表中放入常量
编译过程中若发现使用常量则直接以符号表中的值替换
编译过程中若发现下述情况则给对应的常量分配存储空间
C++编译器虽然可能为const常量分配空间,但不会使用其存储空间中的值。
所以在c编译器上和c++编译器上面我们会发现值的不同
总结二:
关于const修饰限定的作用:c++const修饰限定的作用基本上与c++相同,const可以进行能力的收缩,但是不能进行及能力的扩张
引用的定义:
类型&引用变量名称=变量名称
int main()
{int a = 10;int b = a;int& c = a; //引用
}引用的特点:
int main()
{int a = 10;int& b; //error,引用定义必须进行初始化int& c = nullptr; //error,没有null引用int&& d = a; //error,没有二级引用的说法int& e = a; //right
}const的引用:
int main()
{const int a = 10;int b = 20;int& c = a;//errorconst int& c = a; //okconst int& d = b; //ok 为了安全const int& e = 10; //ok
}
下一篇:Conda创建虚拟环境以及包