当前位置:网站首页>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
边栏推荐
猜你喜欢

【比赛参考】PyTorch常用代码段以及操作合集

The difference between ArrayList and LinkedList

How CentOS installs mysqldump

Plato Farm全新玩法,套利ePLATO稳获超高收益

微信小程序轮播图

ArrayList与LinkedList区别

JMeter download and installation

C how to set different text watermarks for each page of word

356 pages, 140000 words, weak current intelligent system of high-end commercial office complex, 2022 Edition

一张图看懂KingbaseES V9
随机推荐
从根到叶的二进制数之和
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
微信input组件添加清除图标,点击清空不生效
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
CloudCompare&PCL 匹配点距离抑制
List Simulation Implementation
webpack打包vue项目添加混淆方式,解决缓存问题
E-commerce system combined with commodity spike activities, VR panorama continues to bring benefits
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
Specified interval inversion in the linked list
记一次TCP丢包带来的重大性能问题
452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition
xxx is not in the sudoers file.This incident will be reported.的解决方法
Is VR panoramic production a single weapon in the home decoration industry? Why is this?
Internet of things smart home project - Smart bedroom
【小样本分割】MSANet: Multi-Similarity and Attention Guidance for Boosting Few-Shot Segmentation
微信小程序轮播图
The difference between ArrayList and LinkedList
F - Pre-order and In-order(Atcoder 255)
Ribbon负载均衡策略与配置、Ribbon的懒加载和饥饿加载