补充:VS 2013中”This function or variable may be unsafe.”警告的解决办法
#include <stdio.h> int main() { char c[20]; scanf("%s", c); puts(c); return 0; }
如图打出的代码段会出现:error C4996: ‘scanf’: This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
输入后面的use的代码段即可解决 #define_CRT_SECURE_NO_WARNINGS 1 加在源文件的第一行
为什么会产生这样的错误?
===》因为类似 scanf/strcpy/strlen/strcat 都是不安全的函数
都会变为 scanf_s 的相似形式、
五、常量
C语言中的常量和变量的定义的形式有所差异
1、字面常量
#include <stdio.h> int main() { int num = 4; //3 = 5;//错误 3 5 100 3.14都是字面常量 return 0; }
2、const修饰的常变量
#include <stdio.h> int main() { //const - 常属性 //const修饰的常变量 const int num = 4;//本质还是变量 但是具有了常属性 int num = 4; printf("%d\n",num); num =8; printf("%d\n",num); return 0; }
#include <stdio.h> int main() { int arr[10] = {0};//可运行 int n = 10; int arr[n] = {0};//不可运行 n是变量,但是又有常属性所以n是常变量 return 0; }
3、#define 定义的标识符常量
#include <stdio.h> #define MAX 10 int main() { int arr[MAX] = {0}; printf("%d\n",MAX); return 0; }
4、枚举常量
#include <stdio.h> //枚举关键字 - enum enum Sex { MALE, FEMALE, SECRET }; //MALE,FEMALE,SECRET - 枚举常量 int main() { enum Sex s = FEMALE; printf("%d\n",MALE);//0 printf("%d\n",FEMALE); //1 printf("%d\n",SECRET);//2 return 0; }
六、字符串
用 " " 引起的东西
1、
#include <stdio.h> int main() { char arrl1[] = "abc";//数组 char arrl2[] = {'a','b','c'}; printf("%s\n",arrl1); printf("%s\n",arrl2); return 0; } //abc //abc烫烫烫烫蘟bc
F10 调到两个字符串后面 ===》调试 ===》 快速监视
#include <stdio.h> int main() { char arrl1[] = "abc";//数组 //"abc" -- 'a' 'b' 'c' '\0'--'\0'字符串的结束标志 char arrl2[] = {'a','b','c',0};//'\0'也可以 printf("%s\n",arrl1); printf("%s\n",arrl2); return 0; } //abc //abc
\0 是结束标志 每一个字母都有对应的值 --->ASCII 编码
#include <stdio.h> int main() { char arrl1[] = "abc";//数组 char arrl2[] = {'a','b','c','\0'}; printf("%d\n",strlen(arrl1));//strlen - srting length - 计算字符串长度的 printf("%s\n",strlen(arrl2)); return 0; }
ps:VS 2019 需要引入一个 #include <string.h> 的头文件
2、转义字符
\n 换行
\t 水平制表符
七、注释
C语言 ---> // 不能嵌套
C++ ---> /* */
ps:VS2019 注释快捷键 ctrl + k + c 取消注释快捷键 ctrl + k + u
八、选择语句
#include <stdio.h> int main() { int input = 0; printf("加入比特\n"); printf("你要好好学习吗?(1/0)>:"); scanf("%d",&input); if(input ==1); printf("好offer\n"); else printf("卖红薯\n"); return 0; }
九、循环语句
#include <stdio.h> int main() { printf("加入比特\n"); while(line<2000) { printf("敲一行代码:%d\n",line);//加上 “:%d”和“,line”可以在“敲一行代码”后加上行数 line++; } if(line>=2000) printf("好offer\n"); return 0; }
十、函数
十一、数组
一组相同类型元素的集合
#include <stdio.h> int main() { int arr[10] = {1,2,3,4,5,6,7,8,9,10};//定义一个存放10个整数数字的数组 //char //float //…… printf ("%d\n",arr[4]);//下标的方式访问元素“arr[下标]” int i = 0; while(i<10) { printf("%d ",arr[i]); i++; } return 0; }
十二、操作符
1、算数操作符
+ - * / %
#inculude<stdio.h> int main() { int a = 5%2;//取模(取余数) printf("%d\n",a); return 0; }
2、移(2进制)位操作符
<< 左移 >>右移
#include <stdio.h> int main() { //移(二进制)位操作符 int a = 1; //整型1占4个字节=32bit位 //00000000000000000000000000000001 //左移 变为0000000000000000000010 = 2(十进制) int b = a<<1; printf("%d\n",b); printf("%d\n",a);//左移了之后a的值不会变 return 0; }
3、(2进制)位操作
& 按位与 | 按位或 ^ 按位异或
#include <stdio.h> int main() { int a = 3; int b = 5; int c = a&b;///a|b a^b //3 = 011 //5 = 101 //C语言中 0 为假 1 为真 //按位与 & 一假必假 //所以 & 中得出 c = 001 = 1 //按位或 | 一真必真 //所以 | 中得出 c =111 = 7 //按位异或 ^ 对应的二进制位相同,则为0 // 对应的二进制位相异,则为1 //所以 ^ 中得出 c =110 = 6 printf("%d\n",c);//所以c=1 return 0; }
4、赋值操作符
= += -= *= /= &= ^= |= >>= <<=
#include <stdio.h> int main() { int a = 10; a = 20;// = 赋值 ==判断相等 a = a=10; a += 10;//上下等价 a = a-20; a -= 20;//上下等价 a = a & 2; a &= 2;//上下等价 //复合赋值符 += -= …… return 0; }
5、单目操作符、双目操作符和三目操作符
! 逻辑反操作
- 负值
+ 正值
& 取地址
sizeof 操作数的类型长度(以字节为单位)
~ 对一个数的二进制按位取反
-- 前置、后置--
++ 前置、后置++
* 间接访问操作符(解引用操作符)
(类型) 强制类型转换
逻辑反操作
#include <stdio.h> int main() { int a = 10; int b = 20; a+b;//+ 双目操作符 //C语言中我们表示真假 //0 - 假 //非0 - 真 printf("%d\n",a);// 10 printf("%d\n",!a);// 0 // ! 逻辑反操作(把真改成假把假改成真) return 0; }
#include <stdio.h> int main() { int a = 0; printf("%d\n",a);// 0 printf("%d\n",!a);// 1(1也是真 固定的) // ! 逻辑反操作(把真改成假把假改成真) return 0; }
- +
#include <stdio.h> int main() { int a = -2;//- 单目操作符 int b = -a;// int c = +3;//通常情况 +正号 都会省略 return 0; }
sizeof
#include <stdio.h> int main() { int a = 10; //sizeof 计算的是变量/类型所占空间的大小,单位是字节 printf("%d\n",sizeof(a));//4 printf("%d\n",sizeof(int));//4 printf("%d\n",sizeof a);//4 printf("%d\n",sizeof int); return 0; }
#include <stdio.h> int main() { int arr[10] = {0};//10个整型元素的数组 int sz = 0; printf("%d\n",sizeof(arr));//40 //计算数组的元素个数 // 数组总大小/每个元素的大小 = 个数 sz = sizeof(arr)/sizeof(arr[0]); printf("%d\n",sz);//10 return 0; }
~
#include <stdio.h> int main() { int a = 0;//4个字节,32个bit位 int b =~a;//b是有符号的整形, //~ -- 按(2进制)位取反 //0000000000000 原码 //1111111111111 按位取反 即反码 //负数在内存中存储的时候,存储的是二进制的补码 printf("%d\n",b);//使用的,打印的是这个数的原码 return 0; }
++
#include <stdio.h> int main() { int a = 10; int b = a++;//后置++,先使用,再++ 11 10 int b = ++a;//前置++,先++,再使用 11 11 int b = a--;//后置--,先使用,再-- 9 10 int b = --a;//前置--,先--,再使用 9 9 printf("a = %d b = %d\n",a,b); return 0; }
(类型) 强制类型转换
include <stdio.h> int main() { int a = (int)3.14 return 0; }
6、关系操作符
>
>=
<
<=
!=
==
7、逻辑操作符
&& 逻辑与
|| 逻辑或
逻辑与
#include <stdio.h> int main() { //真 -非0 //假 - 0 //&& - 逻辑与 int a = 3; int b = 5; int c = a && b; printf("c = %d\n",c);// c = 0 return 0; }
逻辑或
#include <stdio.h> int main() { //真 -非0 //假 - 0 //|| - 逻辑或 int a = 0; int b = 5; int c = a || b; printf("c = %d\n",c);// c = 1 return 0; }
8、条件操作符
exp1 ? exp2 : exp3
#include <stdio.h> int main() { int a = 10; int b = 20; int max = 0; max = (a > b ? a : b); if(a>b) max = a else return 0; }
9、逗号表达式
exp1,exp2
10、下标引用、函数调用和结构成员
[] () . ->
#include <stdio.h> int Add(int x, int y) { int z = 0; z = x + y; return z; } int main() { int arr[10] = {0}; arr[4];//[] - 下标引用操作符 int a = 10; int b = 20; int sum = Add(a,b);//() - 函数调用操作符 return 0; }
11、常见关键字
typedef - 类型定义/类型重定义
static - 修饰局部变量 ---- 局部变量的生命周期变长
#include <stdio.h> void test() { static int a = 1;//static使a 是一个静态的局部变量,出了这个函数范围不会销毁,即局部变量的生命周期变长 a++ printf("a = %d\n",a); } int main() { int i = 0; while(i<5) { test(); i++; } return 0; }
十二、#define 定义常量和宏
#include <stdio.h> #define MAX 100//定义标识符常量 int main() { int a = MAX; return 0; }
#include<stdio.h> #define MAX(X,Y)(X>Y?X:Y) int main() { int a = 10; int b = 20; max = MAX(a,b); printf("max = %d\n",max) return 0; }