Ctype,sdtio and Stdlib

Source: Internet
Author: User
Tags uppercase letter

CType

The functions in ctype.h are used to parse characters. Common methods are as follows:

ToLower (): Returns the lowercase form of the parameter. If it is lowercase, it will return to that lowercase character directly.

ToUpper (): Returns the uppercase form of the parameter.

isdigit (): Whether it is an Arabic numeral.

isalpha (): Whether it is a letter or not.

       isalnum () : Whether it is a letter or a number. That is, isdigit () or Isalpha () returns True, the function returns True, otherwise false is returned.

isblank (): Whether it is a space or a horizontal tab .

isspace (): Whether it is a null character (including spaces, horizontal tabs, line breaks, vertical tabs, etc.).

islower (): Whether it is a lowercase letter.

isupper (): Whether it is an uppercase letter.

isxdigit (): Whether it is a hexadecimal numeric character.

Stdio

Io, like the meaning in Java, represents a in/out stream. Conceptually, c handles a file in a basic stream, rather than working directly on the file. the process of opening the file then becomes the process of associating the stream with the file and reading and writing through the stream . In particular, the display device is treated as a file automatically opened by each C program, and the keyboard input is represented by a stream called stdin, and the output to the screen is represented by a stdout stream. GetChar (), Putchar (), printf () and scanf () functions are standard I/O package members, both of which deal with this two stream. So you can use the same technology as the processing file to handle keyboard input and output.

End of File

Different systems have different representations of the end of the file, but C masks the differences and returns an EOF(end of file, defined in stdio.h) at the end of the file. Therefore, you only need to compare the values read to the EOF to be equal. such as: while ((c = GetChar ()) = EOF), where c is of type int.

Reason for declaring C as an int type instead of char: Because the range of the char variable is represented by an unsigned integer in 0-255, and EOF may fetch 1, a variable of type char cannot read the value of EOF, whereas a variable of type int is possible. and GetChar () also returns an int type, so it is best to use the int type instead of the char type .

redirect

By default, stdin and stdout correspond to the keyboard and screen. The redirect is to point the two streams to another location (such as a file, etc.) to read and write to the file-a way to read and write files, and C provides a function specifically for manipulating the file.

Note: C CTRL + Z simulates EOF.

<: treats the specified file as stdin

>: Treats the specified file as stdout

As follows:


Redirection has the following rules:

1, redirection connects an executable program to a data file, one data file cannot be associated with another data file, and one executable program is connected to another executable program.

2, the input cannot come from more than one file, and the output cannot be directed to more than one file.

Skip a line of other files

In each line of text that the user enters, only the first one is taken, and the second and subsequent characters are ignored. As follows:

int C;while ((c = GetChar ())! = EOF) {Putchar (c); while (GetChar ()! = ' \ n ') {//non-newline characters are ignored directly. Continue;} At the end of the loop, GetChar () reads the newline character, so the first characters in the next line are read in the outer while (). printf (' \ n ');}
Gets ()

gets () reads a string from the standard input device and reads the newline character to produce a string: all characters before the line break are read (excluding line breaks, newline characters are read, but newline characters are discarded), plus ' character, which produces a string.

it uses an address as a parameter. . And the return value is char*, whose value represents the first address of the string being read, so the returned pointer is the same pointer that was passed to it. The input string has only one backup, which is placed in the address passed as a parameter.

Returns the address of the read-in string when correctly read to the string, or null if the error or gets () encounters the end of the file. Therefore, it is necessary to judge when reading:

while (gets (name)!=null)

Such an instruction can be used to determine whether the end of a file is read, and a value can be read. If it is the end, nothing in name will be stored.

Fgets ()

Gets () does not check that the reserved store is sufficient to store the read string, and it fills the extra characters into the adjacent memory area. Fgets () improves this problem by specifying the maximum number of read-in characters.

1, the second parameter specifies the maximum number of read-in characters. If this parameter is n, the maximum number of characters to read is n-1 or a newline character can be read. The first satisfied one in the two comes to the end of the read-in.

When 2,fgets () reads a newline character, it stores a newline character, rather than discarding the newline character like gets ().

3, the third parameter specifies which file to read. When reading from the keyboard, you can use stdin (defined in stdio.h) as the parameter.

Since get () does not check the size of the reserved memory when reading the string, it is unsafe to overwrite the data in the original memory area by entering a large number of characters. Therefore, you should use Fgets () instead of gets ().

scanf ()

When using the format symbol%s, scanf () can read a string, but it reads a word, while fgets () reads the entire string-possibly containing several words.

SCANF () starts with the first non-whitespace character until the next white-space character ends, which is a word. If you specify a read field width, such as%10s, then scanf () reads 10 characters or encounters a white space character. If a word is not read, the remainder will be read when the next scanf () is left.

Summary: Use Get () and fgets () to read text from the keyboard, while scanf () is used primarily for reading and converting mixed-type data entered in a standard form.

Puts ()

In contrast to get (), used to output a string, puts () automatically appends a newline key to the string when the string is output.

Fputs ()

is the puts () function-oriented version of the file, and the main difference between it and puts () is:

1, a second parameter is required to indicate the output to the file, using stdout as a parameter for output display.

2,fputs () does not automatically add a line break.

In summary:gets () discards the newline character, while the puts () output adds a newline character, fgets () stores a newline character, and fputs () does not add a newline character to the output . Of course, you can also customize the output, such as using Putchar () to output a last puts () without a newline character:

while (*STR) {//const char* Str;putchar (*str++);}

Because it is for output only, it needs to be decorated with a const. + + priority is higher than *, so the role of *str++ is to first *str output, then str++, not (*STR) + +. The string terminator is ' *str ', which is 0, thus ending the loop, so there is no need to write *str!= '.

sprintf ()

Writes formatted data to a buffer, similar to using + in Java to combine strings with non-strings into strings. Such as:

sprintf (R, "%s+%d", "P", 1);//p+1
Getc () and PUTC ()

Similar to GetChar (), Putchar (), but you need to specify the files they want to use. Like Getc (stdin), PUTC (c,stdout) acts like Putchar (c), as is the case with GetChar.

Moreover, GetChar () and Putchar () are generally defined by getc () and PUTC ().

A summary of Getter and setter

GetChar and Putchar (): reads (outputs) a single character, including spaces.

Getc () and PUTC (): GetChar and Putchar () can be read or output to the specified stream.

Gets () and puts (): reads or outputs a string.

Fgets () and fputs (): reads or outputs a string, but can specify a read or output source. General area in file.

printf () and scanf () summary

printf () and scanf (): Used for formatted output and input, output to stdout, input from stdin.

sprintf () and sscanf (): Similar to printf (), scanf (), but requires a string that is input to the output. The former outputs the formatted data to the first parameter, which reads the data in the specified format from the first parameter. Such as

    char* s = malloc (ten);    SSCANF ("2v A34", "%s", s);    Puts (s);//output: 2v
fprintf () and fscanf () are similar to printf () and scanf (), but you need to specify a file* parameter to indicate where the formatted data will be exported, or where to read the data from.

SCANF () reads a number with a space delimiter, and printf () ends with the end character of the string at output. If the output "VA reqwr" is read with scanf () and printf (), then scanf () reads two strings: "Va" and "REQWR", but the printf () output is still one.

malloc ()

Allocating more memory while the program is running, it takes a parameter: The number of bytes of memory required. malloc () allocates memory, but does not specify a name for memory, but returns the address of the first byte of memory, typically strongly turning the return value of malloc to the type it needs .

If malloc () does not find the memory it needs, it returns a null pointer. If found, returns a void type pointer (that is, void*).

A void pointer value is assigned to a pointer of another type and does not constitute a type conflict, so a pointer of type void is called a generic pointer. Such as

double* ptd = (double*) malloc (20*sizeof (double));

The returned void* is strongly converted directly to double* because void* is a generic pointer and does not constitute a type conflict. The code is a space that requests a value of 30 double types.

malloc () and variable-length arrays

Both can be used to create an array that is determined at run time by a size.

But the variable-length array is automatically stored, and the memory space it uses is automatically freed after the code block runs out of the definition section.

The array created by malloc () does not have to be confined to a function, for example, a pointer to the array can be returned for access by the function that called the function. The calling function then calls free () at the end to release.

Free ()

For each malloc () call, you need to call free () to release the memory you just allocated . The parameter of the function free () is the address returned by the previous malloc (). In this way, the duration of the allocated memory starts at malloc () and ends with the call to free ().

Note: free () cannot release memory allocated by other means--such as the memory obtained by declaring an array as an array cannot be freed with free ().

Calloc ()

Similar to malloc (), it is also used to dynamically allocate memory, or it can be freed with free (). But it will place all the locations in the memory block at 0.

Input Summary

SCANF (): reads data in the specified format and can read values of types such as int. when reading non-character type data, whitespace is used as a delimiter, and when characters are read, they are read sequentially, including whitespace characters.

SSCANF (): Similar to scanf (), but you can specify the input source-the string specified by the first parameter.

FSCANF (): Similar to scanf (), but you can specify the input source-the file* pointer specified by the first parameter.

Gets (): Reads a string, takes a carriage return as a delimiter, and does not store the end of the newline character, that is, one can read the complete line at a time.

Fgets (): reads a string, but can specify the input source (usually a file), the maximum number of characters to read, and the end line break.

GetChar (): reads a character that has the same effect as when using scanf () in%c mode.

GETC (): Similar to GetChar (), but you can specify an input source, which is typically used to read characters from a file.

Output Summary

printf (): formatted output to stdout.

sprintf (): Formats the output to the char* specified in the first parameter.

fprintf (): Formats the output to the file* specified in the first parameter.

Puts (): output string and automatically add line break.

Fputs (): Outputs the second parameter specified in the file*.

Putchar (): Outputs a character to the stdout.

PUTC (): Outputs one character to the file* specified by the second parameter.

Stdlibexit ()

closing all open files and terminating the program will terminate the program by calling exit () in any function. In addition, its usual parameter values are exit_success and exit_failure, which indicate a successful termination, which indicates a non-successful termination.

Atexit ()Specifies the function that is called when exit () is executed, whose argument is a function pointer. Atexit () registers its parameters with the list of functions that are executed when exit () is called (the list stores at least 32 functions). The list executes these functions in the order in which they are advanced. And a function that is a atexit () parameter must be a void function with no arguments.
void Exitat (); int main (int argc, const char * argv[]) {    atexit (exitat);//function name is the address of the function    return 0;} void Exitat () {    printf ("Exit exit");
When you call Exit (), the atexit () registered function is executed before it does some cleanup work before formally exiting the program. QsortQuick Sort.
The first argument is the first address of the array to be sorted, the second parameter is the size of the group, the third parameter is the size of each element in an array, and the fourth parameter is a function pointer that specifies the function to compare two elements when sorting. The function has two requirements: the return value is int, and two parameters are the const void* type. as follows:
int Exitat (const void*,const void*); int main (int argc, const char * argv[]) {    int a[] = {4,6,2,8,7};    Qsort (A, 5, sizeof (int), exitat);    printf ("[2] =%d\n", a[2]);    return 0;} The return value is int, and two parameters are void*. A pointer to the array element type is first used when void*. int Exitat (const void* arg1,const void* arg2) {    return * ((int*) arg1)-* ((int*) arg2);}






Ctype,sdtio and Stdlib

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.