C language re-learning series of strings

String input functions gets, fgets, scanf

char *gets(char *s);

Read the string until a newline character (\n) is encountered, which is produced by pressing the Enter key. It reads all characters up to and including the newline, and appends a null character (\0) after them. It will read the newline and discard it so that the next read starts on a new line.
The pointer returned by gets is the same pointer that was passed to it. Returns NULL on error or end-of-file.

char *fgets(char *s, int n, FILE *stream);

The difference between fgets and gets:

  • The second parameter of fegts specifies the maximum number of characters to read. fegts will read up to n-1 characters or a newline position .
  • If fgets reads a newline, it stores it in the string instead of discarding it like gets does.
  • The third parameter of fegts specifies which file to read. When reading data from the keyboard, you can use stdin.
int scanf(const char * restrict format,...);

If the %s format is used, the string is read up to (but not including) the next whitespace character (such as space, tab, and newline). If a field width is specified, such as %10s, scanf will read 10 characters or until the first whitespace character is encountered.
Return Value: Returns the number of items successfully read. Or return an EOF when an end-of-file is encountered.

What happens if the scanf input format does not match the set one?
insert image description here

s reads in a sequence of characters, followed by a null byte, and when a blank character (\t \r \n space, etc.) is encountered, the reading is completed. (Source: Baidu Encyclopedia)

Return Values
Both scanf and wscanf return the number of fields converted and assigned; the return value does not include fields that were read but not assigned.
A return value of 0 indicates that no fields were assigned.
The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.(来源:scanf, wscanf (Windows CE 5.0)

What you input is all strings. scanf just converts strings to variables through format control characters. For example, if I enter a 1, this 1 is not an int or a double, it is just a character. (source: scanf() input format mismatch )

String output functions puts, fputs, printf

int puts(const char *s);

Puts automatically appends a newline character to the string when it displays it . Stop when a null character is encountered.
Characters enclosed in double quotes are string constants and are treated as addresses.

int fputs(const char *str, FILE *stream);

The difference between fputs and puts:

  • The status parameter of fputs specifies the file to write. You can use stdout as a parameter for output display.
  • fputs does not automatically add newlines to output

gets discards newlines in the input, and puts adds newlines to the output.
fegts stores newlines in input, fputs does not add newlines to output.

// C99 前
int printf( const char *format, ... );
// C99 起
int printf( const char *restrict format, ... );

String concatenation functions: strcat, strncat

char *strcat(char *s1, const char *s2);
将第二个字符串的内容拷贝到第一个字符串的末尾(第一个字符串的\0位置)
返回值:第一个字符串的地址
char *strncat(char *s1, const char *s2, size_t n);
添加n个字符或遇到空字符为止。考虑到空字符占了一个字符,计算n最大值时要少1个字节。

String comparison functions: strcmp, strncmp

int strcmp(const char *s1, const char *s2);
返回值:
两个字符串相同,返回0
第一个字符串ASCII码值大,则返回正数
第一个字符串ASCII码值小,则返回负数
int strcmp(const char *s1, const char *s2, size_t n);

String copy functions: strcpy, strncpy

char *strcpy(char *s1, const char *s2);
返回第一个字符串的值。
char *strncpy(char *s1, const char *s2, size_t n);
如果源字符串(s2)个数大于等于n,空字符则不会被复制。

Character lookup functions strchr, strrchr, strpbrk

char *strchr(const char* s, int c);
返回一个指向字符串s中存放字符c的第一个位置的指针(标志结束的空字符是字符串的一部分,因此也一搜索它)。如果没有找到该字符,函数返回空指针。
char *strrchr(const char* s, int c);
返回一个指针,指向字符串s中字符c最后一次出现的地方(标志结束的空字符是字符串的一部分,因此也一搜索它)。如果没有找到该字符,函数返回空指针。
char *strpbrk(const char *s1, const char *s2);
该函数返回一个指针,指向字符串s1中存放s2字符串中的任何字符的第一个位置。如果没有找到任何字符,返回空指针。

String lookup function strstr

char *strstr(const char *s1, const char *s2);
该函数返回一个指针,指向s1字符串中第一次出现字符串s2的地方。如果没有找到,则返回空指针。

number to string

insert image description here
Source: Baidu Encyclopedia

expand:

int sprintf(char *string, char *format [,argument,...]);
如果成功,则返回写入的字符总数,不包括追加在字符串末尾的空字符。
如果失败,则返回一个负数。

int snprintf(char* dest_str,size_t size,const char* format,...);
若成功则返回源字符串的长度(不包括\0),若出错则返回负值。

String length function strlen

insert image description hereFunction prototype:
insert image description here

Source: Baidu Encyclopedia

The difference between strlen and sizeof

sizeof:
insert image description hereinsert image description hereinsert image description here.png)

Extension: How to calculate the size of the structure

insert image description hereinsert image description hereinsert image description here
The following link explains more clearly: C language structure size calculation

insert image description here

insert image description hereSource: Baidu Encyclopedia

Reference document:
"C Primer Plus"

Guess you like

Origin blog.csdn.net/yuuu_cheer/article/details/129033351