对于变量定义的位置可以随用随定义
#include
using namespace std;
int main(void){
int i = 10;
for(int i = 0; i < 10; i++){
cout << "i = " << i << endl;
}
return 0;
}
c语言早期版本不支持随用随定义
int g_val;
// int g_val = 10; error: Redefinition of 'g_val'
int main(void){
return 0;
}
一个变量不管是声明还是定义,只能出现一次
struct teacher{
int age;
char name[64];
};
int main(void){
teacher t1;
//c语言中必须使用 struct teacher t1; 否则报错:Must use 'struct' tag to refer to type 'teacher'
return 0;
}
#include
using namespace std;
int main(void){
//1 bool类型的值为0或1
bool flag = true;
cout << " flag = " << flag << endl;
//2 bool类型占一个字节
cout << "sizeof(flag) = " << sizeof(flag) << endl;
//3 给bool类型赋值非0的值,他的值都为1
flag = 10;
cout << "flag = " << flag << endl;
}
打印结果
flag = 1
sizeof(flag) = 1
flag = 1
#include
int main(void){
//1 c++中的const,确实对a起了保护作用, 不能通过指针间接改变a的值
const int a = 10;
int *p = (int*)&a;
// c语言中可以通过指针的间接赋值改变const的值
*p = 20; //此时*p改变的是临时变量的值,而不是a的值
printf("a = %d\n", a);
return 0;
}
输出结果
a = 10
//1 常整型数
const int a;
//2 常整型数
int const b;
//3 c是一个指向常整数的指针(所指向的内存数据不能被修改,但是本身可以修改)
int const * c;
//4 d常指针(指针变量不能被修改,但是它所指向内存空间可以被修改)
int * const d;
//5 指向常整型的常指针(指针和它指向的内存空间,均不能被修改)
const int * const e;
示例:
#include
int main(void){
//1 常整型数
const int a = 10;
//a = 5;//error:Cannot assign to variable 'a' with const-qualified type 'const int'
//2 常整型数
int const b = 20;
//b = 20;//error:Cannot assign to variable 'b' with const-qualified type 'const int'
//3 c是一个指向常整数的指针
int c_value = 30;
const int *c = &c_value;
printf("*c = %d\n",*c);
//本身可以修改
c = &b;
printf("*c = %d\n",*c);
//指向的内存数据不能修改
//*c = 40; error:Read-only variable is not assignable
//4 d常指针
int d_value = 50;
int * const d = &d_value;
*d = 60;
//指向的内存的值可以修改
printf("d_value = %d\n", d_value);
// 本身不可以修改
//d = &c_value; //error:Cannot assign to variable 'd' with const-qualified type 'int *const'
return 0;
}
输出结果
*c = 30
*c = 20
d_value = 60
本来是想记录一下,C语言中的const是个冒牌货,但实际上C语言中也不并不能修改const修饰的变量的值。
理论上C语言中是能通过指针间接修改const修饰的变量的值的,但是我实际上测试是并不能
int main(void){
const int a = 10;
printf("a = %d\n", a);
int *p = &a;
*p = 20;
int array[a] ={10,20};
printf("a = %d\n", a);
printf("*p = %d\n", *p);
printf("array[1] = %d\n", array[1]);
return 0;
}
打印结果
a = 10
a = 10
*p = 20
array = 20
c语言中枚举的本质就是整型,枚举变量可以用任意整形赋值
c++ 中不能直接把int值赋给enum类型,只能用枚举元素初始化
#include
using namespace std;
enum season{
SPR,
SUM,
AUT,
WIN
};
int main(void){
enum season s = SPR;
cout << "s = " << s << endl;
// s = 0; error:Assigning to 'enum season' from incompatible type 'int'
s = SUM;
cout << "s = " << s << endl;
return 0;
}
打印结果
s = 0
s = 1
#include
#include
int main(void){
std::cout << "hello world" << std::endl;
return 0;
}
输出结果
Hello world
using namespace std;
int main(void){
cout << "Hello world" << endl;
return 0;
}
输出结果
Hello world
using std::cout;
int main(void){
cout << "Hello world" << std::endl;
return 0;
}
输出结果
Hello world
namespace China {
int peopleSize = 14;
}
using namespace std;
int main(void){
using namespace China;
std::cout << " China::peopleSize = " << China::peopleSize<< std::endl;
return 0;
}
输出结果
China::peopleSize = 14
namespace China {
int peopleSize = 14;
namespace HeiBei{
int peopleSize = 5;
}
}
using namespace std;
int main(void){
using namespace China;
std::cout << " China::HeiBei::peopleSize = " << China::HeiBei::peopleSize<< std::endl;
return 0;
}
输出结果
China::HeiBei::peopleSize = 5
#include
// c++中函数不加返回值类型报错 error:Unknown type name 'f'
// c++中函数参数不加类型报错 error:Unknown type name 'i'
f(i){
printf("i = d% \n",i);
};
void f_2(){
printf("method f_2()\n");
};
int main(void){
f_2(10);
return 0;
}
输出结果
method f_2()
void f_3(void){
printf("method f_3(void)\n");
}
int main(void){
// f_3(10); error: Too many arguments to function call, expected 0, have 1
return 0;
}
c++中,int f() 和 int f(void)具有相同的意义,都表示返回值为int的无参函数
#include
int main(void){
int a = 10;
int b = 20;
int min_value = 0;
(a < b ? a : b) = 30;
printf("a = %d,b = %d \n", a, b);
return 0;
}
输出结果
a = 30,b = 20
原理:C++编译器做了一个取地址的操作
*(a < b ? &a : &b) = 30;