String input in C language (gets_s, fgets, scanf, fscanf) and related memory allocation knowledge

0. Memory allocation knowledge of C language

There are two ways to allocate memory space: static memory allocation and dynamic memory allocation

0.1 Static memory allocation

Refers to the determination of the size of data types such as arrays at compile time, and then allocated by the computer, usually the data stored on the stack

For example: when declaring an array, the size of the array needs to be displayed

char name[81]; // 一个大小为81bytes的字符数组

0.2 Dynamic memory allocation

The popular point is to ask the computer for memory space, which is freely controlled by the programmer, and the programmer is also responsible for releasing it. The memory will be applied for when the corresponding program segment is executed, instead of applying in advance like static memory allocation.

Usually use malloc, callocand other memory application functions

#include<stdlib.h>
void* malloc(size_t size);
// https://www.runoob.com/cprogramming/c-function-calloc.html
void* calloc(size_t nitems, size_t size);
// 申请失败时返回NULL

Corresponding to the application of memory space is to release the memory space, usually using the free function

//释放之前调用 calloc、malloc 或 realloc 所分配的内存空间
//https://www.runoob.com/cprogramming/c-function-free.html
void free(void *ptr)

1.gets_s()

char *gets_s( char *str, rsize_t n );// (C11 起)

Function : This function means to read characters from stdin( can be understood as input from the console\n ) until a newline character is found or the end of the file appearsEOF

Features : Note that this function discards newlines\n

Noten-1 : At most characters can be written to strthe array pointed to, and a null termination character ' '(that is, the nth character) is written after reading.

So it can be known that the gets_s function reads a C-style string

Return value : return on success str, return on failureNULL

Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
    
    
    char* str = malloc(10);
    if(str)
        gets_s(str, 10);
    printf("%s\n", str);
    return 0;
}
// VS2019中gets_s函数可执行 但是在Linux中无法编译

result:

Because:

Microsoft has implemented this function, but GCC has not implemented this function, and the detailed reason is unknown.

Question link created by me

2.fgets( )

char *fgets( char *restrict str, int count, FILE *restrict stream );

Function : This function means 文件流streamto read a maximum count-1number of characters from one, and store the read characters into strthe pointed space, and end the reading when the function reads a character EOFor a newline character;\n

Features : If \nthe end of reading is caused by reading a newline character, this function will \nalso store the newline character into the space pointed to by str.(This is a big difference from the gets_s() function, the gets_s() function will discard the newline character \n)

Note : If data is read without errors, a null termination character will be written immediately after the character data read in' '

Return value : str on success, NULL on failure

Example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
    
    char *str = malloc(10);
    if (str)
        fgets(str, 10, stdin); // 表示从控制台输入
    printf("%s\n", str);
    free(str);
    return 0;
}

result:

This function is available in both Linux and Win VS

3.scanf()、fscanf()

int scanf( const char *restrict format, ... );// 从stdin格式化的读取数据
int fscanf( FILE *restrict stream, const char *restrict format, ... );// 从某个文件流stream格式化的读取数据

scanf()and fscanf()the way the function determines the end of the string (that is, the end of reading) is:

①From non-blank to the first blank character (including blank lines, spaces, tabs, newlines)

②If the last reading of the keyboard input is not finished, the next reading will start from the place where the last calling function ended

③If the reading width is specified, the word width and the first blank character **"do or logic"** will be satisfied

Example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
    
    char *str = malloc(10);
    if (str)
        scanf("%s", str);

    printf("%s\n", str);
    free(str);
    return 0;
}

result:

This function is available in both Linux and Win VS


If this article is useful to you, you can like and bookmark this article. It will be much easier to find next time you use it. It would be great
if you can follow the author. The author will continue to learn, output, and share! Thank you for your encouragement!

Guess you like

Origin blog.csdn.net/qq_40459977/article/details/131446187