C语言基础知识

天下无敌C语言

作为宇宙第一语言,学一万遍都不为过。。。。。。

二刷C语言基础,奥里给!!!

1.第一行代码

#include <stdio.h>
int main()
{
	printf("Hello World\n");
	return 0;
}

2.变量

C语言输出格式

#include <stdio.h>
int main()
{
	int a = 10;
	printf("Hello World %d\n",a);
	return 0;
}
%d表示将后面紧跟着的a作为整形数据编译到%d的位置

#include <stdio.h>
int main()
{
	int i;
	int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
	char b[20] = "asasc";
	for (i = 1; i < 10; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");

	return 0;
}

3.变量与宏定义

宏定义使用#define表示

#include <stdio.h>
#define URL "www.baidu.com"
#define NAME "单岱印"
#define YEAR 1998
#define MONTH 10
#define DAY 16
int main()
{
	printf("%s出生于%d年%d月%d日",NAME,YEAR,MONTH,DAY);

	return 0;
}
  • 用单引号括起来表示单个字符
  • 用双引号括起来表示一个字符串

4.数据类型

  • 整数类型 int

  • 浮点数类型 float、double

  • 布尔类型 _Bool

  • 枚举类型 enum

    sizeof运算符:

    用于获得数据类型或者表达式的长度

    • sizeof(object); //sizeof对象

    • sizeof(type_name)

    • sizeof object

      #include <stdio.h>
      int main()
      {
      	int a;
      	char b;
      	float c;
      	int d = 10;
      	float f = 3.14;
      	char e = 'asc';
      	char g[20] = "scascascac";
      	printf("size of int is %d\n", sizeof(int));
      	printf("size of a is %d\n", sizeof(a));
      	printf("size of float is %d\n", sizeof(float));
      	printf("size of c is %d\n", sizeof f);
      	printf("size of char is %d\n", sizeof(char));
      	printf("size of b is %d\n", sizeof e);
      	printf("size of char is %d\n", sizeof(g));
      	return 0;
      }
      

​ signed和unsigned运算符:

​ 类型整形限定符:signed表示可以存放负数

​ unsigned表示可以存放0,1

5.取值范围

1B = 8b

一个字节最大存放的数为11111111
取值范围

6.字符和字符串

字符

#include <stdio.h>
#include <math.h>
int main()
{
	char a = 'C';
	printf("%c = %d\n", a, a);
	return 0;
}

打印结果:
C = 67
采用了查表原理,67就是C的ASCII码

字符串

声明字符串的语法: char 变量名[数量]

#include <stdio.h>
#include <math.h>
int main()
{
	char name[10] = {'s','h','a','n','h','u','a','n','g','\0'};
	char ns[5] = "name";
	printf("%s", name);
	return 0;
}

7.运算符

运算符

一个运算符,是双目还是单目,取决于有几个操作数

例如:1+2 有左右两个操作数,所以是双目 +2有一个操作数,所以是单目

表达式

用运算符和括号将操作数连接起来的式子,称之为表达式。

1+1

‘a’ +‘b’

a+b

a+‘b’+pow(a,b)*3/4+5

运算符优先级

优先级

都大于加减乘除运算符

#include <stdio.h>
#include <math.h>
int main()
{   
	int i, j, k;
	i = 1 + 2;
	j = 1 + 2 + 3;
	k = i + j + -1 + pow(2,3);
	printf("%d\n", i);
	printf("%d\n", j);
	printf("%d\n", k);
	return 0;
}

当运算符操作数类型不同时

编译器在运算之前转化为某种操作类型,通常为占用坑位较小的操作数转换为站用坑位较大的操作数。

#include <stdio.h>
#include <math.h>
int main()
{   
	int a = 1;
	double b = 2.3;
	printf("%f\n",a+b);
	return 0;
}


#include <stdio.h>
#include <math.h>
int main()
{   
	int a = 1;
	int b = 2.3;
	printf("%d\n",a+b);
    printf("%d\n", 1 + (int)3.2);//强制转换
	return 0;
}
打印结果为34,说明当把一个浮点型值用整型定义时,会自动舍弃小数点后面的值

关系运算符和逻辑运算符

关系运算符

关系表达式

#include <stdio.h>
#include <math.h>
int main()
{   
	int a = 5;
	int b = 4;
	printf("%d\n",1>2);
	printf("%d\n", a>b);
	printf("%d\n",a<=b+1);
	printf("%d\n", 'a'+'b'<= 'c');
	printf("%d\n", (a = 3)>(b =5));
	return 0;
}
输出结果为:
0
1
1
0
0

逻辑运算符

逻辑运算符

#include <stdio.h>
#include <math.h>
int main()
{   
	
	printf("%d\n" ,3 > 1 && 4 > 5);
	printf("%d\n", 3 + 1 || 2 == 0);
	printf("%d\n", !(3+4));
	printf("%d\n", !0);
	printf("%d\n", 'a' - 'b' + 1 && 'c');
	printf("%d\n", 'a' - 'b' || 'c');
	return 0;
}
运行结果
0
1
0
1
0
1

短路运算符

#include <stdio.h>
#include <math.h>
int main()
{   
	
	int a = 3;
	int b = 4;
	(a = 0) && (b = 6);
	printf("a = %d,b = %d\n" ,a,b);
	(a = 1) && (b = 7);
	printf("a = %d,b = %d\n", a, b);
	return 0;
}
运行结果
a = 0,b = 4
a = 1,b = 7
分析:
   当a = 0的时候,后面的赋值语句就已经执行不下去了

8.if语句

对于使用vs时scanf()函数提示unsafe 的问题

#include <stdio.h>
#include <math.h>
int main()
{
	unsigned int a, b, c;
	scanf_s("%d%d%d", &a,&b,&c);
	printf("%d%d%d\n", a,b, c);
	return 0;
}
把scanf、fopen改为scanf_s、fopen_s
#include <stdio.h>
#include <math.h>
int main()
{
	int a;
	printf("您老贵庚啊:");
	scanf_s("%d", &a);
	if (a >= 18) {
		printf("进门左拐\n");
	}
	else
	{
		printf("进门右拐\n");
	}
	return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
	int a;
	printf("输入英语成绩:");
	scanf_s("%d", &a);
	if (a >= 90) {
		printf("成绩为A\n");
	}
	else if (a<90 && a >= 80) {
		printf("成绩为B\n");
	}
	else if(a < 80 && a >= 70)
	{
		printf("成绩为C\n");
	}
	else if (a < 70 && a >= 60)
	{
		printf("成绩为D\n");
	}
	else
	{
		printf("成绩为E\n");
	}
	return 0;
}

9.switch语句和分支嵌套

1.switch

switch语句

#include <stdio.h>
#include <math.h>
int main()
{
	char ch;
	printf("请输入成绩:");
	scanf_s("%c",&ch);
	switch (ch)
	{
	case 'A':
		printf("你的成绩在90分以上");
	case 'B':
		printf("你的成绩在80~90分以上");
	case 'C':
		printf("你的成绩在70~80分以上");
	case 'D':
		printf("你的成绩在60~70分以上");
	case 'E':
		printf("你的成绩不及格");
	default:
		printf("请输入有效成绩评级");
	}

	return 0;
}
输出结果:
请输入成绩:A
你的成绩在90分以上
你的成绩在80~90分以上
你的成绩在70~80分以上
你的成绩在60~70分以上
你的成绩不及格
请输入有效成绩评级
原因:
当第一句符合要求的时候,后面的代码如果不加上break都会默认为可执行语句
加上break之后,会跳出switch语句
#include <stdio.h>
#include <math.h>
int main()
{
	char ch;
	printf("请输入成绩:");
	scanf_s("%c",&ch);
	switch (ch)
	{
	case 'A':
		printf("你的成绩在90分以上\n");
		break;
	case 'B':
		printf("你的成绩在80~90分以上\n");
		break;
	case 'C':
		printf("你的成绩在70~80分以上\n");
		break;
	case 'D':
		printf("你的成绩在60~70分以上\n");
		break;
	case 'E':
		printf("你的成绩不及格\n");
		break;
	default:
		printf("请输入有效成绩评级\n");
		break;
	}

	return 0;
}

2.分支嵌套

分支嵌套

例题1

#include <stdio.h>
#include <math.h>
int main()
{
	
	int a, b;
	printf("请输入a和b的值:");
	scanf_s("%d%d",&a,&b);
	if (a != b) {
		if (a > b) {
			printf("a > b");
		}
		else
		{
			printf("a < b");
		}
	}
	else
	{
		printf("a = b");
	}
	return 0;
}

10.while和do while

强调:表达式非0为真

1.while语句:先判断表达式

#include <stdio.h>
#include <math.h>
int main()
{
	
	int i = 1, sum = 0;
	while (i<=100) {
		sum = sum + i;
		i++;
	}
	printf("结果是:%d",sum);
	return 0;
}

2.getchar()函数

//例1
#include <stdio.h>
#include <math.h>
int main()
{
	
	int test = getchar();
	printf("%d",test);
	return 0;
}

//例2
#include <stdio.h>
#include <math.h>
int main()
{
	
	int count = 0;
	while (getchar() != '\n')
	{
		count++;
	}
	printf("总共输入了%d个字符",count);
	return 0;
}

3.do while:先执行,在判断表达式

#include <stdio.h>
#include <math.h>
int main()
{
	int i = 0;
	do
	{
		printf("你好:%d\n",i);
	} while (i); 
}

11.for语句和循环嵌套

1.for语句

#include <stdio.h>
#include <math.h>
int main()
{
	for (int i = 0;i<10;i++){
		printf("执行");
	}
}

灵活的for

#include <stdio.h>
#include <math.h>
int main()
{
	for (int i = 0,j = 10; i < j; i++,j--) {
		printf("执行\n");
	}
}

2.循环嵌套

#include <stdio.h>
#include <math.h>
int main()
{
	printf("打印九九乘法表\n");
	for (int i = 1; i < 10;i++) {
		for (int j = 1; j <= i; j++)
		{
			printf("%d*%d=%-2d ",i,j,i*j);
		}
		printf("\n");
	}
}

12.break语句和continue语句

break 语句,它不仅可以跳出“循环体”,还可以跳出 switch。但事实上,break 也只能用于这两种情况。break 语句不能用于循环语句和 switch 语句之外的任何其他语句中。

不管是 for 循环,还是 while 循环,或者是 do…while 循环,都可以用 break 跳出来,

但是 break 只能跳出一层循环。当有多层循环嵌套的时候,break只能跳出“包裹”它的最里面的那一层循环,无法一次跳出所有循环。

同样,在多层 switch 嵌套的程序中,break 也只能跳出其所在的距离它最近的 switch。但多层 switch 嵌套实在是少见。

continue 的用法十分简单,其作用为结束本次循环,即跳过循环体中下面尚未执行的语句,然后进行下一次是否执行循环的判定。

continue 语句和 break 语句的区别是,continue 语句只结束本次循环,而不是终止整个循环。break 语句则是结束整个循环过程,不再判断执行循环的条件是否成立。而且,continue 只能在循环语句中使用,即只能在 for、while 和 do…while 中使用,除此之外 continue 不能在任何语句中使用。

所以,再次强调:continue 不能在 switch 中使用,除非 switch 在循环体中。此时 continue 表示的也是结束循环体的本次循环,跟 switch 也没有关系。

//break
#include <stdio.h>
#include <math.h>
int main()
{
	for (int i = 1; i < 10;i++) {
		for (int j = 1; j <= i; j++)
		{
			printf("j=%d\n",j);
			break;
		}
		printf("i=%d\n",i);
	}
}
打印结果:
j=1
i=1
j=1
i=2
j=1
i=3
j=1
i=4
j=1
i=5
j=1
i=6
j=1
i=7
j=1
i=8
j=1
i=9
    
    
#include <stdio.h>
#include <math.h>
int main()
{
	for (int i = 1; i < 10;i++) {
		printf("i=%d\n", i);
		break;
		printf("执行");
	}
}
打印结果:
i=1
//
#include <stdio.h>
#include <math.h>
int main()
{
	for (int i = 1; i < 10;i++) {
		printf("i=%d\n", i);
		continue;
		printf("执行");
	}
}
打印结果:
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9

13.拾遗

1.条件运算符

语法:exp1 ? exp2 : exp3;

-exp1是条件表达式

-如果结果为真,返回exp2

-如果结果为假,返回exp3

#include <stdio.h>
#include <math.h>
int main()
{
	int a = 5, b = 6;
	int max = a < b ? a : b;
	printf("%d",max);
}
打印结果:
5

2.goto语句:执行任意一行语句

#include <stdio.h>
#include <math.h>
int main()
{
	int i = 0;
	while (++i)
	{
		if (i>10) {
			goto A;
		}
	}
A:printf("%d",i);
	return 0;
}
//打印结果
11

3.注释 // /**/

14.数组

1.定义一个数组

类型 数组名[元素个数]

计算数组的长度,C语言不能在程序运行中修改数组的空间大小

#include <stdio.h>
#include <math.h>
int main()
{
	/*
	  下方三个变量的大小是一样的
	  int占用4个字节,char占用1个字节,double占用8个字节
	*/
	int a[6] = {1,2,3,4,5,6};
	char b[24];
	double c[3];
	int test = sizeof(a)/sizeof(int);
	printf("%d",test);
	return 0;
}

2.访问数组中的元素

  • 数组名[下表]

​ a[0]; //表示访问数组中的第一个元素

​ a[5]; //访问数组中的第六个元素

  • 注意

    int a[5] //创建一个具有五个元素的数组

    a[5]; //数组越界

#include <stdio.h>
#include <math.h>
#define SUM 10
int main()
{
	int b[SUM];
	int i, sum = 0;
	for (int i = 0; i < 10;i++) {
		printf("请输入第%d同学的成绩",i+1);
		scanf_s("%d",&b[i]);
		sum += b[i];
	}
	printf("平均成绩为:%d",sum/SUM);

	return 0;
}

注意

int a = 11, b = 10;
int c = a / b;
printf("c=%d\n", (double)c);

输出结果为:
1.0000

printf("c=%f\n", (double)11 / 10);
输出结果为:
1.1000

输出结果是不一样的。。。。。。。。。

3.数组初始化

数组的初始化

4.程序运行时,动态改变数组

1. c99支持可变数组
//注意不要使用vs编译,代码会报错,使用Dev-c编译
//原因:
//   vs不支持c99
#include <stdio.h>
#include <math.h>
int main()
{   
	int i, n;
	printf("请输入字符的个:");
	scanf("%d",&n);
	char a[n+1];
	for(i = 0;i<n;i++){
		scanf("%c",&a[i]);
	}
	a[n] = '\0';
	printf("你输入的字符串是:%s",a);
	return 0;
}
2.C语言对于数组越界有保护机制,不会出现崩溃现象
#include <stdio.h>
#include <math.h>
int main()
{
	int a[5] = {1,2,3,4,5};
	for (int i = 0; i <= 5;i++) {
		printf("%d\n",a[i]);
	}
	return 0;
}

5.二维数组

二维数组

1.二维数组的定义

类型 数组名[常量表达式] [常量表达式]

int a[6] [6] //六行六列

char b[4] [5] //四行五列

2.二维数组的访问

二维数组的访问

3.二位数组的初始化
#include <stdio.h>
#include <string.h>
int main()
{
	int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			printf("%-3d", a[i][j]);
		}
		printf("\n");
	}
	return 0;
}
  • 为了更直观的表述,可以将每一行用大括号单独括起来

    int a[3][4] = { 
        {1,2,3,4},
        {5,6,7,8},
        {9,10,11,12}
    };
    
  • 二维数组也可以仅仅对部分元素赋初值,没有值的自动赋值为0

    int a[3][4] = {{1},{5},{9}};
    
  • 二维数组自动赋值

二维数组自动赋值

#include <stdio.h>
#include <string.h>
int main()
{
	int a[][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12}};
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			printf("%-3d", a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

15.字符串处理函数

1.定义字符数组

  • 方法一

    char a[4];
    	a[0] = 'O';
    	a[1] = 'A';
    	a[2] = 'B';
    	a[3] = '\n';
    
  • 方法二

    char a[4] = {'A','B','C','\n'};
    
  • 方法三

    //加上双引号之后会自动谭家\n
    char a[4] = {"ABC"};
    char a[4] = "ABC";
    

2.字符串处理函数

  • 获取字符串的长度strlen
#include <stdio.h>
#include <string.h>
int main()
{
	char a[8] = "ABCDEFG";
	printf("sizeof str = %d\n",sizeof(a));
	printf("strlen str = %u",strlen(a));
	return 0;
}
//打印结果
sizeof str = 8
strlen str = 7
//原因:
    sizeof(尺寸)包含\n
    strlen(长度)不包含\n
  • 拷贝字符串strcpy和strncpy
#include <stdio.h>
#include <string.h>
int main()
{
	char one[11] = "Old String";
	char tow[11] = "New String";
	char three[100];
	strcpy_s(one,tow);
	strcpy_s(three, "Copy Successful");
	printf("one = %s\n", one);
	printf("tow = %s\n", tow);
	printf("three = %s\n", three);
	return 0;
}
//打印结果
one = New String
tow = New String
three = Copy Successful
//说明:第一个参数拷贝第二个参数
//注意:第一个参数必须要容纳第二个参数的长度


#include <stdio.h>
#include <string.h>
int main()
{
	char one[11] = "Old String";
	//char tow[11] = "New String";
	char three[100];
	strncpy_s(three,one,4);
    //最好追加结束符
    three[4] = '\0';
	//strcpy_s(three, "Copy Successful");
	//printf("one = %s\n", one);
	//printf("tow = %s\n", tow);
	printf("three = %s\n", three);
	return 0;
}
//打印结果
three = Old(一个空格)
//第一个参数拷贝第二个参数,第三个参数表示限制拷贝多少字符


  • 连接字符串strcat和ctrncat

    //同样的,vs不支持此函数运用
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	char one[11] = "Old String";
    	char tow[11] = "New String";
    	strcat(one," ");
    	strcat(one, tow);
    	printf("拼接 = %s\n",one);
    	return 0;
    }
    //打印结果
    拼接 = Old String New String
    
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	char one[11] = "Old String";
    	char tow[11] = "New String";
    	strncat(one, tow,3);
    	printf("拼接 = %s\n",one);
    	return 0;
    }
    
  • 比较两个字符串是否一致strcmp和strncmp(若一致则返回0,若不一致则返回1)

    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	char one[11] = "Old String";
    	char tow[11] = "Old String";
    	int test = strcmp(one, tow);
    	printf("拼接 = %d\n", test);
    	return 0;
    }
    
    
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	char one[11] = "Old String";
    	char tow[12] = "Old Strings";
    	int test = strncmp(one, tow,10);
    	printf("拼接 = %d\n", test);
    	return 0;
    }
    

    16指针

请看下一篇文章

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值