当前位置:网站首页>C language array and bubble sort
C language array and bubble sort
2022-07-28 20:00:00 【Embedded Xiaobai aspires to be a boss】
1、 Array
integer array 、 Character array 、 Two dimensional array
1.1 Concept
A set of elements with the same data type
characteristic :(1) Same data type (2) Address continuous
As shown in the figure above , The address of the array is continuous
1.2 Definition
Storage type data type Variable name ;
int a;// An integer variable is defined
Storage type data type Array name [ Number of elements ];
int a[5];// An array is defined , Array has 5 Elements
Storage type :auto、register、static、extern
data type : The data type of each element in the array
Data type of array : data type [ Number of elements ] int [5]
data type : Removing the variable name is the data type
Array name : Address of the first element of the array ( Entire array )
Number of elements : It must be a definite number ( integer constants )
1.3 Initialization of an array
1.3.1 Partial initialization
int a[5] = {1,2,3};
a[0] = 1 ,a[1] = 2, a[2] =3, a[3] = ? ,a[4] = ? a[5] = ?//error
summary : When the array is partially initialized , If it's not assigned , The default is 0, So we use this feature to clear the array
1.3.2 All initialization
int a[5] = {1,2,3,4,5};
int b[ ] = {1,2,3};// The number of elements of the array is determined by the specific number assigned later
- If the array is defined inside the function body , Not initialized , Its value is random
- If the array is defined outside the function , Not initialized , Its value is 0
- If you use static modification , Not initialized , Its value is 0
1.4 Array access
Array name [ Subscript ];
Be careful : The subscript of an array is from 0 At the beginning
1.5 Array input 、 Output
1.6 Bubble sort
thought : Compare from left to right , If the former number is larger than the latter one , Just swap places , Otherwise unchanged
2、 A character array
The essence of character arrays : It's a string , With ‘\0’ As a Terminator
Storage type data type Array name [ Number of elements ];
char str[6] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; // At most 5 Elements , Leave a place for ’\0’
char str[6] = “hello”;
char str[6] = {“hello”};
Clear the character array :char str[6] = {‘\0’};
2.1 String output function
puts( Array name );
function : Print the string in the array to the terminal , And automatically add and wrap lines
Be careful : encounter ’\0’ end
2.2 Input function of string
gets( Array name );
function : Put the string entered by the keyboard into the array , And automatically add ’\0’
Be careful : No cross-border inspection , So don't type more
2.3 summary
2.3.1 scanf and gets The difference between
gets It ends with carriage return , however scanf By return 、Tab Key and space as terminators
buffer :
gets: When you finish inputting the string , The buffer will be emptied automatically
scanf: When you finish inputting the string , Spaces will be left in the buffer 、 enter 、Tab key
Be careful :gets First check whether there is any residual content in the buffer , If there is , Just take it and use it , Otherwise input
scanf Is a standard input function , You can only get the value by entering at a time
2.3.2 puts and printf The difference between
puts Line breaks will be automatically added , and printf Can't
边栏推荐
- Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
- Redis notes
- shared_ptr 和 make_shared 的使用
- [experience] some suggestions and experience on repairing electronic equipment
- 时间转日期的sql语句应该怎么写?
- [网络]跨区域网络的通信学习路由表的工作原理
- [网络]跨区域网络的通信学习IPv4地址的分类和计算
- 利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
- Getting started with saltstack
- Leetcode Day2 consecutive numbers
猜你喜欢

MATLAB实现的图像分割之边缘检测和连接

Integration and implementation of login click graphic verification code in personal blog system

Information management system and games based on C language

Common APIs in string

This customized keyboard turns me on~

个人博克系统登录点击图形验证码的集成与实现

KPMG China: insights into information technology audit projects of securities fund management institutions

Prometheus deployment

Servlet learning notes

Read how to deploy highly available k3s with external database
随机推荐
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
[experience] some suggestions and experience on repairing electronic equipment
Servlet学习笔记
Leetcode day2 连续出现的数字
数字滤波器设计——Matlab
软考高级考试中有五大证书,哪个更值得考?
English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages
English Translation Spanish - batch English Translation Spanish tools free of charge
Saltstack advanced
Leetcode Day2 consecutive numbers
editor.md中markdown编辑器的实现
NetCoreAPI操作Excel表格
Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
MySQL8 Status Variables: Internal Temporary Tables and Files
Germany and Portugal have announced that they will not disable Huawei 5g equipment, but Germany will set strict restrictions!
Data system of saltstack
shared_ptr 和 make_shared 的使用
Return and job management of saltstack
My second blog - C language