当前位置:网站首页>初识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;
}边栏推荐
- Say yes, I will love you, and I will love you well
- 程序里随处可见的interface,真的有用吗?真的用对了吗?
- 剑指offer专项突击版第12天
- [深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
- JS event loop synchronous task, asynchronous task (micro task, macro task) problem analysis
- Xiaomi website homepage big module - small module + navigation (floating case)
- With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it
- Pytorch optimizer settings
- what‘s the meaning of “rc“ in release name
- 获取两个集合相差数据
猜你喜欢

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

MySQL 中的 INSERT 是怎么加锁的?(荣耀典藏版)
![[solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected](/img/66/99bd61223cbe622db3e28474f4fa15.png)
[solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected

How MySQL uses indexes (glory Collection Edition)

Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report

MySQL是如何利用索引的(荣耀典藏版)

Say yes, I will love you, and I will love you well

What is eplato cast by Plato farm on elephant swap?

Maskedauutoencoders visual learner cvpr2022

MySQL's way to solve deadlock - lock analysis of common SQL statements
随机推荐
第三章 队列
With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it
what‘s the meaning of “rc“ in release name
MySQL锁系列之锁算法详解(荣耀典藏版)
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
功能测试和非功能测试区别简析,上海好口碑软件测试公司推荐
11 Django basics database operation
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(2)小程序功能
Find - block search
MYSQL解决死锁之路 - 常见 SQL 语句的加锁分析
JVM tuning -xms -xmx -xmn -xss
How to put app on the app store?
Manual installation of Dlib Library
第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任
分层图解决的一些最短路问题
Network must know topics
[深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
unordered_map的hash function及hash bucket存储方式探索
支付宝小程序授权/获取用户信息
MySQL pymysql operation