当前位置:网站首页>First knowledge of C language -- structure, branch and loop statements
First knowledge of C language -- structure, branch and loop statements
2022-07-28 02:39:00 【 child】
The pointer
#include <stdio.h>
int main()
{
int a = 10; // Applied to memory 4 Bytes of space
// &a Take the address operator
// printf("%p\n",&a);
int *p = &a; // *p Pointer to the variable ,
// printf("%p\n",p);
*p = 15; // * -- Indirect access operators
printf("a = %d\n",a);
return 0;
}1. Structure ( important )
Structure is a type we created
#include <stdio.h>
// Create a structure
struct Book
{
char name[20]; // Title
short price; // Price
}; // ; essential
int main()
{
// Use the structure type to create a structure variable of that type
struct Book b1 = {"C Language ", 20};
printf(" Title :%s\n",b1.name);
printf(" Price :%d\n",b1.price);
b1.price = 15;
printf(" Revised price :%d\n",b1.price);
return 0;
}1.1 Structure + Pointer application ( important )
The operator :
. Structural variable . member
-> Structure pointer -> member
Using this method, we can define a structure to store the data of the sensor
#include <stdio.h>
// Create a structure
struct Book
{
char name[20]; // Title
short price; // Price
}; // ; essential
int main()
{
// Use the structure type to create a structure variable of that type
struct Book b1 = {"C Language ", 20};
struct Book *p = &b1;
printf("%s\n", p->name); // -> Is the operator whose pointer points to the object
printf("%d\n", p->price);
return 0;
}#include <stdio.h>
#include <string.h>
// Create a structure
struct Book
{
char name[20]; // Title
short price; // Price
}; // ; essential
int main()
{
// Use the structure type to create a structure variable of that type
struct Book b1 = {"C Language ", 20};
strcpy(b1.name, "C++"); // strcpy -- String copy , Library function --string.h
// All structure members are of string type , All need to be adopted strcpy To modify its content
struct Book *p = &b1;
printf("%s\n", p->name); // -> Is the operator whose pointer points to the object
printf("%d\n", p->price);
return 0;
}2. Branches and loops
C Language is a structured programming language , Sequential structure 、 Selection structure 、 Loop structure
2.1 Branch statement ( Selection structure )
C There is a semicolon in language ; Separated by a statement .
else Match the nearest if sentence , Unless added {} Segmentation
Code style optimization , When comparing and judging the values of constants and variables , The constant is on the left , Put the variable on the right
int num = 1;
if(5 == num){
printf("%d\n",num);
}practice :
Output 1--100 Between the odd numbers
/*
* Output 1~100 Between the odd numbers
* Ideas 1: Odd is divided by 2 An integer followed by a remainder
* Ideas 2: Odd number is 2 A multiple of 1
*/
#include <stdio.h>
// int main()
// {
// int i;
// for(i = 1; i <= 100; i++)
// {
// if (i % 2 != 0)
// printf("%d\t", i);
// }
// return 0;
// }
// Method 2
int main()
{
int i;
for (i = 1; i < 100; i += 2)
{
if (i % 2 != 0)
printf("%d\t", i);
}
return 0;
}Output results :

2.2 switch sentence
switch Nesting is allowed inside
2.3 Loop statement
while for do--while
continue stay while The role in the cycle :
continue Is used to terminate this cycle , In this cycle continue The following code is no longer executed , It's a jump to while The judgment part of a sentence , Make the entry judgment of the next cycle .
for loop :
1.for Initialization of the loop 、 adjustment 、 Judgment can be omitted , however for The judgment part of the loop is omitted , Then the judgment condition is always true
2. If not very skilled , Don't omit for Each part of the cycle
practice 1: Calculation n The factorial
#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;
}practice 2: Calculation 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;
}practice 3: Find a specific number in an ordered array n, To write intbinsearch(int x, int v[],int n); function : stay v[0]<=v[1]<=v[2]<=.....<=v[n-1] Find in the array of x.
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int k = 7;
// Write a code , stay arr Found... In an ordered array 7
int i = 0;
int sz = sizeof(arr) / sizeof(int);
for (i = 0; i < sz; i++)
{
if (k == arr[i])
{
printf(" Already found , The following table is :%d\n", i);
break;
}
}
if (i == sz)
printf(" Not found \n");
return 0;
}Binary search
/*
Find by dichotomy , Ordered array
*/
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sz = sizeof(arr) / sizeof(int); // Count the number of elements in an array
int left = 0; // Left subscript
int right = sz - 1; // Right subscript
int mid = (left + right) / 2; // The subscript of the intermediate element
int k = 7;
while (left <= right)
{
if (arr[mid] > k)
right = mid - 1;
else if (arr[mid] < k)
left = mid + 1;
else
{
printf(" Already found , The following table is :%d\n", mid);
break;
}
}
if (left > right)
printf(" Can't find \n");
return 0;
}边栏推荐
- Manual installation of Dlib Library
- MySQL's way to solve deadlock - lock analysis of common SQL statements
- Understand the "next big trend" in the encryption industry - ventures Dao
- Leetcode judge whether palindrome number
- POC模拟攻击利器 —— Nuclei入门(一)
- [understanding of opportunity -53]: Yang Mou stands up and plots to defend himself
- [solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected
- Wechat campus bathroom reservation applet graduation design finished product (1) development outline
- 作业7.27 IO进程
- 一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由
猜你喜欢

Emotional drama in the world Zhou Bingkun lost his job because he saw Tu Zhiqiang and was shot

Maskedauutoencoders visual learner cvpr2022

How to put app on the app store?

mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。

Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters

作业7.27 IO进程

From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project

retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'

初识C语言 -- 结构体,分支和循环语句

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(3)后台功能
随机推荐
How MySQL uses indexes (glory Collection Edition)
Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
What is eplato cast by Plato farm on elephant swap?
Common SQL statement query
Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
【TA-霜狼_may-《百人计划》】图形3.7 移动端TP(D)R架构
Use try-with-resources or close this
QT implementation disable shortcut key
[hcip] BGP Foundation
基于FPGA的64位8级流水线加法器
LETV responded that employees live an immortal life without internal problems and bosses; Apple refuses to store user icloud data in Russia; Dapr 1.8.0 release | geek headlines
Design of edit memory path of edit box in Gui
POC模拟攻击利器 —— Nuclei入门(一)
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
借助Elephant&nbsp;Swap打造的ePLATO,背后的高溢价解析
A 64 bit 8-stage pipelined adder based on FPGA
Use of Day6 functions and modules
Share an esp32 relay
【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码
Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters