当前位置:网站首页>Standard C language 11
Standard C language 11
2022-07-27 04:23:00 【Luo CS】
thirty 、 character string
character : Patterns that people can understand
In a computer, characters are stored as integers , When it needs to be displayed as characters , Will be based on
ASCII The corresponding relationship in the code table shows the corresponding symbols or patterns
'\0' 0 Null character
'0' 48
'A' 65
'a' 97
strand : It's a data structure , It consists of a continuous set of data of the same type , There must be an end sign at the end
The processing of this data structure is batch , From the beginning to the end
character string :
A string structure consisting of characters , The end sign is ‘\0’
The existence form of string
A character array :
char str[5] = {'1','3','4','2'};
from char The formed string should be given '\0' Leave a place , Stack memory is used , Data can be modified halfway
string literal :
" Several characters contained in double quotation marks " Auto complement at the end '\0'
String literals exist as addresses , Character data exists in code segment , Do not modify the . Correct some errors
Be careful : Exactly the same string literal , There is only one copy in the code segment
const char* p = " string literal ";
sizeof(p) Operating system bits ?4:8
sizeof("1234") 5 Calculate the number of memory bytes occupied by the string literal in the code segment , Include '\0'
Commonly used way :
A character array [] = " string literal ";
Will automatically give '\0' Leave a place , You can modify the content , Initialization is simple
There are two copies of the string after the assignment , One in the code segment , One in stack memory ( Modifiable )
Input and output of string :
Input :
char str[200]={};
scanf("%s",str);
You cannot enter a string with spaces ,
char *gets(char *s);
Return value : return s, For chain calls , The return value of one function is directly used as the parameter of another function .
Can input a string with spaces , Do not limit the length of input , There are security risks , The compiler will have a warning .
char *fgets(char *s,int size,FILE *stream);
function : from stream Maximum input in size-1 Characters to s in
s: A character array
size: The maximum number of characters received +1
stream:stdin Equivalent to keyboard file Fixed write
Return value : For chain calls , The return value of one function is directly used as the parameter of another function .
If the input exceeds size-1 Characters , The extra ones will not be accepted
If not enough size-1,'\n' Will also be received
Output :
printf("%s", A character array | string literal );
int puts(const char *s);
function : Output string
Return value : Number of characters successfully output
Be careful : Will automatically print line breaks
Thirty-one 、 Output buffer
The content to be output in the program will not be immediately displayed on the screen , Instead, it is stored in the output buffer first ,
When certain conditions are met, it will be displayed on the screen from the buffer .
1. encounter '\n' after , All output
2. When encountering an input statement , Output
3. When the output buffer (4K) Full of
4. Program end
5. Linux Provides fflush(stdout); Manually refresh only Linux
Thirty-two 、 Input buffer
The program does not immediately get the input from the keyboard , But when you press enter , The content entered by the terminal will
Store it in the input buffer first , Then execute the input function and read data from the input buffer into the content .
1. When you want to read integer or floating-point data , But when the data in the buffer is symbols or letters , Read failed ;
Data will remain in the buffer , Affect the subsequent data reading
solve :
First judge whether all inputs are correct , If not , First clean up the cache and re-enter , Until the input is correct .
int a=-1,b=-1,c=-1;
while (3>scanf("%d%d%d",a,b,c))
{
printf(" There is a problem with the input , Please re-enter ");
stdin->_IO_read_ptr = stdin->_IO_read_end; // only Linux
}
printf("%d %d %d",a,b,c);
2. fgets() Can receive specified size-1 Characters , If there are extra characters , Will remain in the buffer , May affect the following data
solve :
1.
fgets(str,size,stdin);
int len = 0;
while (str[len])
len++;
if('\n' != str[len-1])
{
scanf("%*[^\n]");( Regular expressions )
Read any data from the buffer , If not '\n', Just keep reading , Until I met '\n', Stop reading
scanf("%*c");
Read a character from the buffer , And discard
}
2.
stdin->_IO_read_ptr = stdin->_IO_read_end;( Only in Linux/UNIX System use )
Pointer the current position in the input buffer , Move to the end of the buffer , It is equivalent to clearing the input buffer .
3. When entering integer or floating-point type first , Then type the characters . Enter characters in 、 When the string , The previous input will remain '\n'
Affect the following characters 、 Input of string
solve :
character :
Add a space before the next input
character string :
scanf("%d",&num);
scanf("%*c");// This is the solution
gets(str);
Thirty-three 、 String related operation functions
( Be able to realize it by yourself )
#include <string.h>
size_t strlen(const char *s);
function : Calculate string length ( barring '\0')
strlen And sizeof difference
strlen Is the function
sizeof It's the operator
#include <string.h>
char *strcpy(char *dest, const char *src);
function : hold src Copy to dest, It is equivalent to giving dest assignment .
Return value : call chaining
#include <string.h>
char *strcat(char *dest, const char *src);
function : hold src Append to dest At the end of , It is equivalent to merging two strings
Return value : call chaining
#include <string.h>
int strcmp(const char *s1, const char *s2);
function : Compare the size of two strings .
Start from scratch , Each character is compared one-on-one , In dictionary order , Who is in front and who is young .
Once the results are compared , Return results immediately , There will be no comparison later .
Return value :
0 s1 == s2 ( The main )
Positive numbers s1 > s2
negative s1 < s2
#include <stdlib.h>
int atoi(const char *nptr);
function : String rotation int
long atol(const char *nptr);
function : String rotation long
long long atoll(const char *nptr);
function : String rotation long long
double atof(const char *nptr);
function : String rotation double
#include <string.h>
char *strstr(const char *haystack, const char *needle);
function : stay haystack To find out whether there is a substring needle
Return value :needle stay haystrck The first place in , No return NULL
#include <stdio.h>
int printf(const char *format, ...);
int sprintf(char *str, const char *format, ...);
function : Output various types of data to string str
int sscanf(const char *str, const char *format, ...);
function : from str Parse and read data into variables .
Return value : The number of successfully resolved variables .
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
function : from src Location copy n Byte to dest The location of ;
function : call chaining
#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);
function : Compare the values of two pieces of memory , Compare by byte , Once the results come out, there will be no comparison
Return value :
0 s1 == s2 ( The main )
Positive numbers s1 > s2
negative s1 < s2
边栏推荐
- Ant JD Sina 10 architects 424 page masterpiece in-depth distributed cache from principle to practice pdf
- BigDecimal踩坑总结&最佳实践
- 匿名命名管道, 共享内存的进程间通信理解与使用
- People don't talk much, engineers don't talk much
- DINO 论文精度,并解析其模型结构 & DETR 的变体
- Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
- Redis面试题(2022)
- 微服务的feign调用header头被丢弃两种解决方案(附源码)
- 新互联网时代已来 WEB 3.0 会给我们带来哪些新机遇
- The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
猜你喜欢
![[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)](/img/6a/02c76f5bd35b2d89e4f471b9b688f5.png)
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)

Subject 3: Jinan Zhangqiu line 2

Convolution neural network -- a detailed introduction to convolution of 24 bit color images

BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明

【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)

卷积神经网络——24位彩色图像的卷积的详细介绍

Eureka-服务注册中心

influxDB 基础了解

Apachecon Asia preheating live broadcast incubator theme full review

The difference between ArrayList and LinkedList
随机推荐
Worship the 321 page PDF of the core technology of Internet entrepreneurship that Alibaba is pushing internally. It's really kneeling
二叉树的坡度
微服务化解决文库下载业务问题实践
Brightcove appoints Dan Freund as chief revenue Officer
List Simulation Implementation
[competition reference] pytorch common code snippet and operation collection
数据分析师岗位分析
influxDB 基础了解
e.target与e.currentTarget的区别
A. Round Down the Price
What is animation effect? What is the transition effect?
【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
细说Hash(哈希)
11. Zuul routing gateway
Practice of microservice in solving Library Download business problems
js三种遍历数组的方法:map、forEach、filter
Subject 3: Jinan Zhangqiu line 5
法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
对NIO的初步理解
452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition