当前位置:网站首页>C语言从入门到入土(三)
C语言从入门到入土(三)
2022-07-24 05:16:00 【泡泡牛奶】
前言
生活还要继续,学习不能停止,本期就给大家大致了解一下函数,数组的基本使用,以及一些常见操作符的认识,方便大家能更好的看懂大佬写的代码。
目录
一、函数
在C语言中大致可以分为库函数和自定义函数。
1.1 库函数
库函数是C语言编程时规定好的函数
#include <stdio.h>
#include <string.h>
int main()
{
char ch[] = "abcdef";
int i = 0;
i = strlen(ch);// strlen 函数用于求字符串长度
printf("%d", i);
return 0;
}
在上面这个例子中,我使用了一个 strlen 的函数,而调用这个函数的时候就需要引用一个<string.h>的头文件。
1.2 自定义函数
在编程的时候往往会碰到库函数无法完美实现自己想达到的功能的情况,而此时我吗们就需要,自己创建一个函数,我们称之为自定义函数。
假设要想自己实现一个加法的函数
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int a = 2;
int b = 3;
int c = 0;
c = add(a, b);
printf("%d", c);
return 0;
}自定义函数的优点:当想要多次使用一个功能的时候,可以避免代码过于沉长
二、数组
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
int c = 3;
return 0;
}当我们想定义很多同一种类型数据的时候,一个个定义会显的十分的长,此时,我们就引入了一个数组来存储同一个类型的数据。
#include <stdio.h>
int main()
{
//int a = 1;
//int b = 2;
//int c = 3;
//上面的太长了
int arr[] = { 1,2,3,4 };
return 0;
}2.1 数组的定义
数组可以有三种初始化的方式
#include <stdio.h>
int main()
{
int arr1[3] = { 1,2,3 };
int arr2[] = { 1,2,3 };
int ar3[3];
return 0;
}arr1 规定了3个元素个数,里面初始化3个元素
arr2 只进行了初始化,没有规定元素个数
arr3 只规定了3个元素,并没有进行初始化
2.2 数组的下标
C语言规定:数组每个元素下面都要有一个下标,下标从0开始。
数组可以通过下标来访问。
int arr[8] = { 1,2,3,4,5,6,7,8 };
注意:
[8] 方括号里面的数字代表数组元素的个数
而下标是从0开始的,在这个例子里,下标最大值为7 ( 范围:0 ~ 7 )
2.3 数组的使用
如果我们想对一个数组进行操作,使用一个循环会方便很多
#include <stdio.h>
int main()
{
int i = 0;
int arr[8] = { 0 };
for (i = 0; i < 8; i++)//给数组输入值
{
scanf("%d", &arr[i]);
}
for (i = 0; i < 8; i++)//输出数组的值
{
printf("%d ", arr[i]);
}
return 0;
}
当我使用scanf的时候,会只读取数字,中间用空格分开,空格是字符,类型与原来不符,所以不读取
三、操作符
在前期的学习中, 我们主要以认识为主,以后会详细介绍
算数操作符
+ - * / %
加 减 乘 除 模(取模运算就是算一个数的余数)
#include <stdio.h>
int main()
{
int a = 0, b = 3, c = 2;
a = b % 2;
printf("%d", a);
return 0;
} 
移位操作符
>> <<
位操作符
& ^ |
赋值操作符
= += -= *= /= &= |= >>= <<=
= : a = b , 将 a 的值给 b
+= : a += b ,相当于 a = a + b
-= : a -= b ,相当于 a = a - b
*= : a *= b ,相当于 a = a * b
/= : a /= b ,相当于 a = a / b
单目操作符
! 逻辑取反
- 负号
+ 正号
& 取地址
sizeof 操作数的字节长度( 单位: 字节)
~ 对一个二进制数按位取反
-- 前置、后置 --
++ 前置、后置 ++
* 间接访问操作符(解引用操作符)
(类型) 强制类型转换
逻辑反操作符:
#include <stdio.h>
int main()
{
int d = 2;
if (!(d == 2))// ! 对判断结果取反
printf("haha");
else
printf("hehe");
return 0;
}sizeof :
注意,sizeof是一个单目操作符号,用于计算字节大小
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(char));
return 0;
}
--和++:
自减和自加,使用的时候主要看在前面还是后面
例如:
++a : 表示先自加,后进行下一步运算
a++ : 表示先运算,后进行自加
#include <stdio.h>
int main()
{
int a = 0, b = 0, c = 1, d = 0;
b = a++;
d = --c;
printf("b = %d, d = %d", b, d);
return 0;
}
关系操作符:
> 大于>= 大于等于< 小于<= 小于等于!= 用于判断“不相等”== 用于判断“相等”
逻辑操作符:
&& 逻辑 与|| 逻辑 或
条件操作符:
exp1 ? exp2 : exp3
这个表示: exp1 为真吗?为真返回 exp2 ,为假返回 exp3
#include <stdio.h>
int main()
{
int a = 3, b = 4, c = 0;
c = (a > b) ? a : b;
printf("%d",c);
return 0;
}
逗号表达式:
exp1 , exp2 , exp3 , ...... , expN
使用逗号表达式,程序会一直运行下去
#include <stdio.h>
int main()
{
int a = 1, b = 3, c = 0;
c = a++, ++b, ++a;
printf("c = %d, a = %d, b = %d\n", c, a, b);
return 0;
}

下标引用、函数调用、结构体成员
[ ] 使用数组时候的下标引用操作符 例如:arr[ ]
( ) 调用函数使用的括号 例如: Add( )
. 指针访问的时候用到的操作符
-> 指针访问的时候用到的操作符
作者的小声BB:
在早期的学习中,先从读懂大佬写的代码才能一步步提高自我,总是要经过一段天天写BUG的时期的,( 我就是这么过来的 )一个错误在刚开始的时候错的多是很正常的,但在经过自己慢慢的专研后,能对这个知识点又更加深刻的理解。
边栏推荐
猜你喜欢

PPPoE gateway simulation environment setup

Performance test process

一步一步带你学C(其一)

C语言实现扫雷游戏
![[database connection] - excerpt from training](/img/7d/334b4b7f0f6e1de228c4ecb1e600a3.png)
[database connection] - excerpt from training

AiN 0722 签到

Beginners' preparation for the Blue Bridge Cup (University Programming learning history records, topic ideas for students who need to prepare for the Blue Bridge Cup)

Echo speaker pairing and operation method

Implementation and comparison of nine sorting (ten thousand words summary)

Skills of BeanShell dealing with JSON
随机推荐
深度剖析数据在内存中的存储
Relationship between sample and population in Statistics: sample success ratio + central limit theorem (sample mean)
【NumPy】
MySQL 远程连接错误解决方法
手写orm框架
Ia notes 2
Introduction to reflection
NFS shared services
酒店IPTV数字电视系统解决方案
SSM integration
The opening ceremony of the 2022 Huawei developer competition in China kicked off!
Generator generator, which generates only two methods
C语言进阶篇 三.字符串函数和内存操作函数
Data annotation learning summary
Jsp+dao integration
DHCP principle and configuration
C语言入门篇 三.数组 四.操作符
C语言进阶篇 七.程序的编译和预处理
空杯心态,重新开始
股票价格走势的行业关联性