C++基础知识(一)数据类型,运算符,表达式
- 数据类型
基本类型(整型、实型、字符型、枚举类型)、构造类型(数组、结构体、共用体)、指针类型、空类型。
- VC 6.0中数据长度
char 1B, shot int 2B, int 4B, long int 4B, float 4B, double 8B, long double 8B,指针长度取决于CPU字长度。
- 定义常量
const关键字用于修饰只读常量。const后面接什么,什么就是常量。
#define AA 5
#define BB AA+3int x,y;
x=AA+3;
y=AA*BB+x; // y=5*5+3+x
//x=8, y=36
int b[] = {1,2};
const int *a = &b; //变量值是常量
int const *a = &b; //变量值是常量
int* const a = &b; //指针是常量,不能a++
const int* const a = &b; //指针和变量值均是常量
- 输入输出
在VC 6.0中,要用scanf和printf要添加头文件“#include <stdio.h>”
cin和scanf输入数组时可以用空格、回车、Tab分割,均不能以逗号分割!!!除非:
scanf("%d,%d", &a, &b); //这里原理同下边
scanf("a=%d,b=%d", &a, &b); //输入必须为“a=数字,b=数字”
getchar()不能以空格、回车、Tab分割输入,会存入变量中
char str[5];
for(int i=0;i<sizeof(str)-1;i++){
str[i] = getchar();
}
str[sizeof(str)-1]='\0';
printf("%s", str);
- 赋值运算
x *= y+8; // x=x * (y+8)
逗号表达式,有括号全部括起来时返回最后一项,没有返回第一项。
int x=(1,2,3,4,5);
cout<<x<<endl; //输出5
x=1,2,3,4,5;
cout<<x<<endl; //输出1

浙公网安备 33010602011771号