Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What does char ch mean in C language?
What does char ch mean in C language?
Char indicates that the data type is character type, and ch is variable name (which can be called field).

Example of fgetc () usage:

Is to read a character from the D:\\demo.txt file and save it in the variable ch.

There is a position pointer inside the file, which is used to point to the current reading and writing position, that is, which byte to read and write. When a file is opened, the pointer always points to the first byte of the file. After using the fgetc function, the pointer will move backwards by one byte, so you can use fgetc to read multiple characters continuously.

Note: The position pointer in this file is different from that in C language. The location pointer is just a symbol, indicating the location where the file is read and written, that is, what bytes are read and written, not the address. Every time a file is read or written, the position pointer moves once. There is no need to define and assign values in the program, but it is set automatically by the system, which is transparent to users.

This example displays the contents of the D:\\demo.txt file on the screen.

Create a demo.txt file under drive D, enter any content, save it, run the program, and you will see all the content just entered displayed on the screen.

The function of this program is to read the characters one by one from the file and display them on the screen until they are read.

The 14 line of the program is the key, and the condition of the while loop is (ch=fgetc(fp))! = EOF .Fget () reads one character at a time from the position of the position pointer and saves it to the variable ch, and the position pointer moves backward by one byte. When the file pointer moves to the end of the file, fget () can't read the characters, so it returns EOF, indicating that the file reading is over.