C language string input and output study notes

**

C language string input and output study notes

Zero, create storage space

** First of all, c language strings are in the form of an array of characters to store and does not appear in the operation of the process to use an array (an array of masterpieces often single-handedly led address)
to create a solid-state storage space:

char zifu[MAX];       //创建完成 

Creating a dynamic storage space :( do not recommend)

char *zifu;
scanf("%s", zifu);

Drawbacks: zifu memory space is a place to find random, string assignment here will cover data and code, and may lead to abnormal program termination.
But if you do not make any changes to the operation, then this can create a dynamic array.

First, enter

1.scanf (): Enter the word for the type of

Space for the input string: string before skip spaces, blank character string (line feeds, spaces, tabs) to automatically end [not read the blank character] (with a final "/ 0" string)
About read format: scanf("%s/%10s",zifu);PS:% 10s for the specified field width, the reading indicates 10 characters. In addition, there is no assignment symbol!
About scanf () function: Return Value scanf () is the number of variables have been successfully assigned (0/1/2 ...) or if an error is encountered or met end of file, the return value is EOF.

Separator question: is mainly aimed at "% c": no default separator character (including all whitespace characters) *** Solution: *** (1) empty the buffer - there is a function called fflush (stdin) of It can be used to clear the buffer.
while(scanf("%c%c",&a,&b)!=EOF)  
    {  
        printf("a=%c,b=%c\n",a,b);  
        fflush(stdin);  
        printf("Input the value of a and b:");  
    }  

Namely: before the white characters are misreading the buffer temporarily empty.

(2) error induced by the read out data buffer (buffer indirect cleaning) - getchar () or gets ()

For getchar (): read the first character buffer (EOF), the function returns a value of the character (EOF == - 1) (if the integer variable is returned to its corresponding ASCII code)Brother extended function putchar (): putchar (ch / int) - This function output terminal to the parameter ch / int (ASCII code) corresponding to the value of char

2.gets {get string} (): read a line of things

First newline (\ n) determined input end face , with a line break and to replace the null character (\ 0).
gets () defect: the array does not check whether the input line let goSo often there will be an error warning
using the functions: gets (name): the read line string name assigned to the array. The first function return address of the string. (Str = gets (name) - str also point name string array)

char name[MAX];    
char *ptr; //指向char的指针     
printf("Please input your name.\n");    
ptr = gets(name); //使用一个地址吧字符串赋值给name   
                  //gets()函数使用return关键字返回字符串的地址    
printf("name, %s\n", name);    
printf("ptr, %s\n", ptr);

How to end a string of repeated line that reads : if () function encounters an error or end of file (EOF) when reading the string gets, it returns an empty (or 0) address, the address is called empty space pointer, and defined inside stdio.h constant NULL represented, the following code can be used to detect some errors.

while(get(name) != NULL)

3.fgets (): read the end of the string in two ways: the number of characters up to the upper limit line feed OR

One more than the previous two properties, not only to read a standard input mode (keyboard)
Reads the final line breaks

fgets(name, MAX, stdin);

PS: fgets () will be like gets () as in the end of the string plus a null character read
the third parameter to read instructionsWhich file! When reading data from the keyboard, can be used stdin (on behalf of standard input) as an argument, the identifier is defined in stdio.h.

Second, the output

1.printf (): output string

printf("%s",name);

2.puts {put string} (): only the first address need string parameters (i.e., array name)

Reaches the end of the string / 0 output is terminated, and automatically add a newline.

3.fputs (): the face of file versions

fputs(line,stdout);

And the puts () function is different, fputs () functionThe output does not automatically add a line break.
ps: puts () & fputs ( ) respectively gets () & fgets () corresponding to {Automatically add a line break is reversed}

发布了10 篇原创文章 · 获赞 0 · 访问量 113

Guess you like

Origin blog.csdn.net/weixin_45076393/article/details/104462333