当前位置:网站首页>初识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;
}边栏推荐
- Achievements in science and Technology (XXVIII)
- 并发编程的三大核心问题(荣耀典藏版)
- 数字赋能 创新未来:海丝埃睿迪亮相第五届数字中国建设峰会
- Sqlserver problem solving: replication components are not installed on this server. Please run SQL Server Setup again and select the option to install replication components
- Flume (5 demos easy to get started)
- MySQL high availability and master-slave synchronization
- 【HCIP】BGP 特性
- Flex layout - fixed positioning + flow layout - main axis alignment - side axis alignment - expansion ratio
- 【HCIP】BGP 基础
- MySQL数据库InnoDB存储引擎中的锁机制(荣耀典藏版)
猜你喜欢

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

How is insert locked in MySQL? (glory Collection Edition)

关于Sqli-labs单引号不报错的问题

OBS keyboard plug-in custom DIY

regular expression

Wechat campus bathroom reservation applet graduation design finished product (3) background function

程序里随处可见的interface,真的有用吗?真的用对了吗?

作业7.27 IO进程

Soft test - database (2) relational model

Alipay applet authorization / obtaining user information
随机推荐
MySQL high availability and master-slave synchronization
pytorch优化器设置
数字赋能 创新未来:海丝埃睿迪亮相第五届数字中国建设峰会
使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)
Sword finger offer special assault edition day 12
"The faster the code is written, the slower the program runs"
[in depth study of 4g/5g/6g topic -42]: urllc-14 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -8-low delay technology-2-slot based scheduling and
Today in history: the father of database passed away; Apple buys cups code; IBM chip Alliance
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
MySQL's way to solve deadlock - lock analysis of common SQL statements
Class notes (5) (1) - 593. Binary search
[Yugong series] use of tabby integrated terminal in July 2022
MySQL数据库InnoDB存储引擎中的锁机制(荣耀典藏版)
Xiaomi website homepage big module - small module + navigation (floating case)
[hcip] routing strategy, strategic routing
Pytorch optimizer settings
[data processing] boxplot drawing
Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
MySQL 中的 INSERT 是怎么加锁的?(荣耀典藏版)
[understanding of opportunity -53]: Yang Mou stands up and plots to defend himself