1. 数据类型
数据类型
所有数据均有固定的数据类型,C中数据的类型不能改变
- 整数:int / short / long /
- 浮点数 : float / double
- 字符型 : char
- 数组
- 结构体
数据类型决定了该数据的内存,存储方式,取值范围
1.1 常量
定义固定常量
#define PI 3.14159
const float pi = 3.14159
数据:名称/值/地址
int a;
char b;
float c;
double d;
cout << sizeof(a) << " "<< sizeof(b) << " "<< sizeof(c) << " "<< sizeof(d) <<endl;
// 4 1 4 8
1.2整型
进制
在C语言里,整数有三种表示形式:十进制,八进制,十六进制。
进制的表示:
1.十进制:除表示正负的符号外,以数字1~9开头,如,128,+234,-278。
2,八进制:以0开头,由0~7组成的数。如,0126,050000.
3,十六进制:以0X或0x开头,由0-9,A-F或a~f 组成。如,0x12A,0x5a000.
输出不同进制:
C
%c | 字符 |
%d | 十进制 |
%x | 无符号十六进制 |
%o | 无符号八进制 |
C++
dec | 十进制 |
hex | 无符号十六进制 |
oct | 无符号八进制 |
例:
int a = 100;
printf("%d\n", a); //100
printf("%c\n", a); //d
printf("%x\n", a); //64
printf("%o\n", a); //144
int a = 100;
int b = 14;
cout << dec;
cout << "a="<<a << " " << "b="<< b << endl;
cout << hex;
cout << "a="<<a << " " << "b="<< b << endl;
cout << oct;
cout << "a="<<a << " " << "b="<< b << endl;
int | int |
short | short int |
long | long int |
signed | 默认, 有符合 |
unsigned | 无符号 |
定义:
int a;
long b;
unsigned long c;
1.3浮点型
类别
float | 单精度浮点型 |
double | 双精度浮点型 |
定义
float a = 3.14;
float b = 0.314E1;
1.4字符型
定义:单引号
char c = 'a';
2. 数组
定义:
int a[] = {1,2,3};
int a[3];
int a[]; // error
数组作为函数输入与输出
int sum(int a[], int n){
int c = 0;
for(int i=0; i<n; i++){
c += a[i];
}
return c;
}
int main(){
int a[] = {1,2,3};
int s = sum(a, 3);
cout << s << endl;
}
3. 面向过程
3.1 判断语句
常用符号
> < >= <= | 大于/小于/大于等于/小于等于 |
== != | 相等/不等 |
! | 非 |
&& | 与 |
|| | 或 |
例:
int a = 8;
int b = 4;
cout << (a>b) << endl; // 1
cout << (a==b) << endl; // 0
int c = 10;
cout << ((b>a)||(c>a)) << endl; // b大于a或者c大于a 成立
cout << ((b>a)&&(c>a)) << endl; // b大于a且c大于a 不成立
cout << (!(b>a)) << endl; //b大于a是错的 成立
if… else…
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void rand_go(){
srand((unsigned)time(NULL));
int a = rand() % 10; // 随机产生0~9
if (a<5) {cout << "go" << endl;} // 0,1,2,3,4
else {cout << "not go" << endl;} // 5,6,7,8,9
}
if… else… if else…
void rand_go(){
srand((unsigned)time(NULL));
int a = rand() % 10; // 随机产生0~9
if (a<5) {cout << "go" << endl;} // 0,1,2,3,4
else if(a==5) {cout << "not go" << endl;} // 5,6,7,8,9
//else {cout << "restart" << endl;}
}
3.2 循环语句
for()…
for(表达式1; 表达式2; 表达式3){
语句块
}
执行顺序:
- 先执行“表达式1”。
- 再执行“表达式2”,如果它的值为真(非0),则执行循环体,否则结束循环。
- 执行完循环体后再执行“表达式3”。
- 重复执行步骤 2) 和 3),直到“表达式2”的值为假,就结束循环。
例:
for(int i=0; i<10; i++){
cout << i << " ";
}
while()…
while(condition)
{
statement(s);
}
例:
int i=0;
while(i<10){
cout << i << " ";
i++;
}
do… while()
do{
statement(s);
}while( condition );
int i =0;
do{
cout << i << " ";
i++;
}while(i<10);
3.3 选择语句
switch… case…
switch(表达式){
case 值1:
表达式的值和 值1匹配上了,需要执行的代码;
break;
case 值2:
表达式的值和 值1匹配上了,需要执行的代码;
break;
default:
如果表达式的值和以上的case后面的值都没有匹配上,那么就执行这里的代码。
break;
}
例:
void rand_go(){
srand((unsigned)time(NULL));
int a; // 随机产生0~9
switch(a = rand() % 10){
case 1:
cout << "beef" << endl;
break;
case 2:
cout << "BBQ" << endl;
break;
case 3:
cout << "fish" << endl;
break;
default:
cout << "hotpot" << endl;
break;
}
}
3.4 break continue
循环语句中:
break 跳出一个循环,结束一个循环
continue 结束本次循环,执行下一次循环
例
void func1(int a){