C基础入门笔记
编程注意问题
- 包含头文件的重要性
- 以函数为单位来进行程序编写
- 声明部分 + 实现部分
- return 0;
- 多用空格空行
- 添加注释(下面有
包含头文件的重要性
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
//#include <string.h> // 不加入头文件就段错误
int main()
{
FILE *fp;
fp = fopen("tmp","r");
if(fp == NULL)
{
fprintf(stderr,"fopen() ; %s\n",strerror(errno)); // Segmentation fault
exit(1);
}
puts("ok!");
exit(0);
}
注释
单行
多行 文档注释
#if 0
#endif
#include <stdio.h>
#include <stdlib.h>
#if 0
void func()
{
}
#endif
int main()
{
exit(0);
}
常量 变量
- 变量没啥说的 : 数据类型 变量名字 = 值;
- 常量 define const
- define的使用与坑
- 变量的存储类型有四种 auto static register extern
define的使用与坑
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14
#define ADD 2+3
#define MAX(a , b) ((a) > (b) ? (a):(b))
// 改正 在内核中见的比较多
#define MAX(a,b) \
({typeof(a) A = a,B = b; ((A) > (B) ? (A) : (B)); })
int main()
{
/*
1、预编译阶段全部替换不做语法检查
2、使用注意加括号以为优先级
3、define的坑
*/
PI * 4; // gcc -E define.c 预编译全部替换不进行语法检查
PI + 4;
PI - 4;
PI / 4;
printf("%d \n",ADD * ADD); // printf("%d \n",2+3 * 2+3); // 11
int i = 2, j = 4;
printf("%d \n",MAX(i,j)); // 4 没有问题
printf("i = %d j = %d \n", i, j);
printf("%d \n",MAX(i++,j++)); // 5
printf("i = %d j = %d \n", i, j);
return 0;
}
变量存储类型
- auto:默认,自动分配空间,自动回收空间
- static:静态型,自动初始化为0值或空值,并且其变量的值具有继承性常用于修饰变量或函数
- register:寄存器类型(建议性关键字)
register int i = 1; 由编译器决定是否存储在寄存器中,大小有限制只能用来定义局部变量,32位机器只能定义32位大小的数据类型,如double就不可以,寄存器没有地址,所以无法打印寄存器类型变量的地址进行查看或使用 - extern:说明型,不能改变被说明的变量的值或类型
全局变量的坑
#include <stdio.h>
#include <stdlib.h>
int i = 0; // error 缺陷
void printf_stat(void)
{
for(i = 0;i < 5; i++) {
printf("*");
}
printf("\n");
printf("[%s] i = %d \n",__FUNCTION__,i);
}
int main()
{
for(i = 0;i < 5; i++)
printf_stat();
printf("[%s] i = %d \n",__FUNCTION__,i);
exit(0);
}
输出结果:
static的使用
- static 常用的两种方式 修饰变量或函数
- static修饰变量表示、防止全局变量发生意外使用、防止重名、
- 修饰函数不可扩展 用static 修饰
类似面向对象的思想
#include <stdio.h>
#include <stdlib.h>
#include "proj.h"
static int i = 10;
int main()
{
printf("[%s] i = %d \n",__FUNCTION__,i);
func_c();
exit(0);
}
#include "proj.h"
static int i = 100;
static void func(void)
{
printf("[%s] i = %d \n",__FUNCTION__,i);
return ;
}
void func_c() {
func();
}
#ifndef PROJ_H_
#define PROJ_H_
#include <stdlib.h>
#include <stdio.h>
static void func(void);
#endif
extern的使用
- extren 是生命行,不能改变原变量值
关键字
标识符规则
字符、数字、下划线
区分大小写
不能用关键字
第一个字符必须为字母或下划线
数据类型
所占字节数
存储区别
不同类型的数据之间进行转换(隐式转换、显示转换
特殊性:
(1)布尔类型
(2)float类型
(3)char型是否有符号 ? 不确定
(4)不同形式的0值: 0, “0”, ‘0’ , ‘\0’
(5)数据类型与后序代码中使用的输入输出要相匹配(不能相互矛盾)
- 整型
类型 | 大小 |
---|---|
int | 4 |
short | 2 |
long | 4 |
long long | 8 |
short < int <= long < long long
2. 浮点数
类型 | 大小 |
---|---|
float | 4 |
double | 8 |
#include <iostream>
using namespace std;
int main()
{
// 单精度
// 双精度
cout << sizeof(float) << endl; // 4
cout << sizeof(double) << endl; // 8
float f1 = 1.33f;
cout << f1 << endl;
double f2 = 8.887766;
cout << "f2 = " << f2 << endl;
return 0;
}
float注意事项:
int func(float f)
{
if(f < 0) return -1;
else if(f == 0) return 0;
else return 1;
// f == 0 不能比较
}
- 字符型 – C语言中不确定符号
声明
#include <iostream>
using namespace std;
int main()
{
char a = '88';
cout << sizeof(char) << endl; // 1
cout << (int)a << endl; //
system("pause");
return 0;
}
转义字符
\n \t 略
字符串
C语言
char str[] = "hhhh";
C++
string str = "hhhh"; // 包含头文件 string
布尔
占一个字节
运算符
- 位运算的重要性
000 >>
000 <<
000 ~
000 |
000 ^
000 &
将操作数中第n位置1,其他位不变:num = num | 1 << n;
将操作数中第n位清0,其他位不变:num = num & ~(1 << n);
测试第n位:if(num & 1 << n)
从一个指定宽度为w的数中取出第(m -> n)位:(num << (w-n)) >> (w-n+m)
输入输出
跳过
数据流程
if while for
数组
冒泡排序
#include <iostream>
using namespace std;
int main()
{
int arr[] = {2,4,62,36,3,1,64,33,775,998};
int n = 9; // 排序总伦树等于排序个数减1
// 冒泡排序
//1 2 3 4 33 36 62 64 775 998
for(int i = n; i > 0; i--)
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
for (auto x : arr) {
cout << x << " ";
}
cout << endl;
system("pause");
return 0;
}
二维数组
#include <iostream>
using namespace std;
int main()
{
int arr[2][4] = {2,4,62,36,3,64,33,775};
cout << "二维数组占用内存为: "<<sizeof(arr) << endl;
cout << "二维数组第一行占用内存为: " << sizeof(arr[0]) << endl;
cout << "二维数组第一个元素占用内存为: " << sizeof(arr[0][0]) << endl;
cout << "二维数组行数为: "<< sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组列数为: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << endl;
}
}
// 2、 查看二维数组的首地址
cout << "二维数组的首地址为: " << (int)arr << endl;
cout << "二维数组的第一行首地址为: " << (int)arr[0] << endl;
cout << "二维数组的第二行首地址为: " << (int)arr[1] << endl;
cout << "二维数组的第一个元素地址为: " << (int)&arr[0][0] << endl;
cout << "二维数组的第二个元素地址为: " << (int)&arr[0][1] << endl;
cout << endl;
system("pause");
return 0;
}
输出
函数
- 函数定义
- 调用
- 分文件编写
编写头文件
编写.cpp
包含
#pragma once
#include <iostream>
using namespace std;
int max(int a, int b);
#include "swap.h"
// 声明可以多次,但是定义只能一次
int max(int a, int b) {
return a;
}
#include "swap.h"
int main()
{
int n = max(1, 8);
cout << n << endl;
return 0;
}