Day 3: C for Pythonist. EOF (End Of File) and more

Day 3: C for Pythonist. EOF (End Of File) and more

This is a classic example of reading characters from the standard input and writing them to the standard output until the End Of File (EOF) condition is encountered.

#include <stdio.h>

int main(){
    int c;
    while ((c = getchar()) != EOF)
        putchar(c);
}        

Let's break down the script:

  • getchar() is equal input() in Python
  • while is while, what can I add...
  • != is 'not equal', like in Python (seems like I know C already)
  • EOF or End Of File. This is something new for us. Let's talk about it below.
  • putchar() is a kind of print() in Python, don't think about it now. We'll come back later.

More about EOF:

EOF indicates the end of input. Also, we can find that EOF is a macro is a negative integer, which indicates that the end-of-file has been reached.

In C, file input/output operations often involve reading or writing data from/to files. When reading from a file, the program needs to know when it has reached the end of the file.

The EOF value is typically defined as -1 in C libraries. This consistency allows C programs to work across different systems and platforms without worrying about the specifics of EOF representation.

Actually, EOF is a Universal Signal for End of Input. It provides a standardized way to signal the end of input across different platforms and operating systems. Regardless of the underlying file system (such as NTFS, ext4, or FAT32) or input source (such as keyboard input, file input, or network input), C programs can rely on EOF to know when to stop reading input. This ensures the portability and interoperability of C programs across various environments.

Now you know that EOF is more than just a useless feature of C. EOF is everywhere! lol.

PS: ((c = getchar()) != EOF): Why double brackets? The precedence of '!=' is higher than that of '=', not like in Python, eah?

#C #cprogramming #softwaredevelopment

要查看或添加评论,请登录

Ilia Munaev的更多文章

社区洞察

其他会员也浏览了