当前位置:网站首页>初识C语言 -- 结构体,分支和循环语句
初识C语言 -- 结构体,分支和循环语句
2022-07-28 01:28:00 【稚子】
指针
#include <stdio.h>
int main()
{
int a = 10; //向内存申请了4个字节的空间
// &a取地址操作符
// printf("%p\n",&a);
int *p = &a; // *p指针变量,
// printf("%p\n",p);
*p = 15; // * --间接访问操作符
printf("a = %d\n",a);
return 0;
}1.结构体(重要)
结构体是我们创建出来的一种类型
#include <stdio.h>
// 创建一个结构体
struct Book
{
char name[20]; // 书名
short price; // 价格
}; // ;必不可少
int main()
{
// 利用结构体类型创建一个该类型的结构体变量
struct Book b1 = {"C语言", 20};
printf("书名:%s\n",b1.name);
printf("价格:%d\n",b1.price);
b1.price = 15;
printf("修改后的价格:%d\n",b1.price);
return 0;
}1.1 结构体+指针应用(重要)
操作符:
. 结构体变量.成员
-> 结构体指针-> 成员
使用这种方法就可以定义一个结构体用来存放传感器的数据
#include <stdio.h>
// 创建一个结构体
struct Book
{
char name[20]; // 书名
short price; // 价格
}; // ;必不可少
int main()
{
// 利用结构体类型创建一个该类型的结构体变量
struct Book b1 = {"C语言", 20};
struct Book *p = &b1;
printf("%s\n", p->name); // ->是指针指向对象的操作符
printf("%d\n", p->price);
return 0;
}#include <stdio.h>
#include <string.h>
// 创建一个结构体
struct Book
{
char name[20]; // 书名
short price; // 价格
}; // ;必不可少
int main()
{
// 利用结构体类型创建一个该类型的结构体变量
struct Book b1 = {"C语言", 20};
strcpy(b1.name, "C++"); // strcpy --字符串拷贝,库函数--string.h
// 凡是结构体成员是字符串类型的,都需要采用strcpy才能修改其内容
struct Book *p = &b1;
printf("%s\n", p->name); // ->是指针指向对象的操作符
printf("%d\n", p->price);
return 0;
}2.分支和循环
C语言是一门结构化的程序设计语言,顺序结构、选择结构、循环结构
2.1 分支语句(选择结构)
C语言中由一个分号;隔开的就是一条语句。
else匹配最近的一个if语句,除非加{}进行分割
代码风格优化,常量与变量的值进行比较判断时,常量放在左边,变量放在右边
int num = 1;
if(5 == num){
printf("%d\n",num);
}练习:
输出1--100之间的奇数
/*
*输出1~100之间的奇数
*思路1:奇数为除以2之后有余数的整数
*思路2:奇数就是2的倍数加1
*/
#include <stdio.h>
// int main()
// {
// int i;
// for(i = 1; i <= 100; i++)
// {
// if (i % 2 != 0)
// printf("%d\t", i);
// }
// return 0;
// }
// 方法2
int main()
{
int i;
for (i = 1; i < 100; i += 2)
{
if (i % 2 != 0)
printf("%d\t", i);
}
return 0;
}输出结果:

2.2 switch语句
switch内允许嵌套
2.3 循环语句
while for do--while
continue在while循环中的作用:
continue是用于终止本次循环,也就是本次循环中continue后边的代码不会再执行,而是直接跳转到while语句的判断部分,进行下一次循环的入口判断。
for循环:
1.for循环的初始化、调整、判断都可以省略,但是for循环的判断部分被省略,则判断条件恒为真
2.如果不是非常熟练,不要顺便省略for循环的各个部分
练习1:计算n的阶乘
#include <stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
ret = ret * i;
}
printf("ret = %d\n", ret);
return 0;
}练习2:计算1!+2!+....+10!
#include <stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
int sum = 0;
for (n = 1; n <= 10; n++)
{
ret = ret * n;
sum = sum + ret;
}
printf("sum = %d\n", sum);
return 0;
}练习3:在一个有序数组中查找具体的某个数字n,编写intbinsearch(int x, int v[],int n);功能:在v[0]<=v[1]<=v[2]<=.....<=v[n-1]的数组中查找x.
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k = 7;
// 写一个代码,在arr有序数组中找到7
int i = 0;
int sz = sizeof(arr) / sizeof(int);
for (i = 0; i < sz; i++)
{
if (k == arr[i])
{
printf("已找到,下表是:%d\n", i);
break;
}
}
if (i == sz)
printf("未找到\n");
return 0;
}二分法查找
/*
采用二分法查找,有序数组
*/
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sz = sizeof(arr) / sizeof(int); // 计算数组中元素的个数
int left = 0; // 左下标
int right = sz - 1; // 右下标
int mid = (left + right) / 2; // 中间元素的下标
int k = 7;
while (left <= right)
{
if (arr[mid] > k)
right = mid - 1;
else if (arr[mid] < k)
left = mid + 1;
else
{
printf("已找到,下表是:%d\n", mid);
break;
}
}
if (left > right)
printf("找不到\n");
return 0;
}边栏推荐
- 重要安排-DX12引擎开发课程后续直播将在B站进行
- 并发编程的三大核心问题(荣耀典藏版)
- MySQL create stored procedure ------ [hy000][1418] this function has none of deterministic, no SQL
- 学会这招再也不怕手误让代码崩掉
- [深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
- 软工必备知识点
- Learn this trick and never be afraid to let the code collapse by mistake
- Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
- Which database is the fastest to query data only?
- Flask1.1.4 werkzeug1.0.1 source code analysis: Blueprint
猜你喜欢

MySQL数据库InnoDB存储引擎中的锁机制(荣耀典藏版)

JVM tuning -xms -xmx -xmn -xss

Product axure9 English version, using repeater repeater repeater to realize multi-choice and single choice

Find - block search

Chapter 3 business function development (batch export of market activities, Apache POI)

Wechat campus bathroom reservation applet graduation design finished product (1) development outline

Two ways for wechat applet to realize dynamic horizontal step bar

Learn this trick and never be afraid to let the code collapse by mistake

分层图解决的一些最短路问题

怎么简单实现菜单拖拽排序的功能
随机推荐
Unity 保存图片到相册以及权限管理
Understand the "next big trend" in the encryption industry - ventures Dao
Achievements in science and Technology (XXVIII)
feign调用get和post记录
实际工作中,我是如何使用 Postman 做接口测试?
Flex layout - fixed positioning + flow layout - main axis alignment - side axis alignment - expansion ratio
POC模拟攻击利器 —— Nuclei入门(一)
MYSQL解决死锁之路 - 常见 SQL 语句的加锁分析
Chapter 3 business function development (batch export of market activities, Apache POI)
AWS elastic three swordsman
C # introducing WinAPI to pass the character set of Chinese string parameters
"The faster the code is written, the slower the program runs"
Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
Compile and use Qwt in qt|vs2017
剑指offer专项突击版第12天
软件产品第三方测试费用为什么没有统一的报价?
0 dynamic programming medium leetcode873. Length of the longest Fibonacci subsequence
cn+dt
APP如何上架App Store?
[understanding of opportunity -53]: Yang Mou stands up and plots to defend himself