What is the ascii value of EOF in c.?

前端 未结 7 785
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 13:51

Any one knows what is the ASCII value of i.

I try printf(\"%d\",EOF);

but its print -1

and also try printf(\"%c\",EOF

相关标签:
7条回答
  • 2020-12-01 14:25

    for all intents and purposes 0x04 EOT (end of transmission as this will normaly signal and read function to stop and cut off file at that point.

    0 讨论(0)
  • 2020-12-01 14:30

    As Heffernan said, it's system defined. You can access it via the EOF constant (is it a constand?):

    #include <stdio.h>
    
    int main(void)
    {
        printf("%d\n", EOF);
    }
    

    After compiling:

    c:\>a.exe
    -1
    c:\>
    
    0 讨论(0)
  • 2020-12-01 14:34

    EOF is not an ASCII character. Its size is not 1 byte as against size of a character and this can be checked by

    int main() {
        printf("%lu", sizeof(EOF));
        return 0;
    }
    

    However, it is always defined to be -1. Try

    int main() {
        printf("%d",EOF);  
        return 0;
    }
    

    The key combination for EOF is Crtl+D. The character equivalent of EOF is machine dependent. That can be checked by following:

    int main() {
        printf("%c", EOF)
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 14:41

    The actual value of EOF is system defined and not part of the standard.

    EOF is an int with negative value and if you want to print it you should use the %d format string. Note that this will only tell you its value on your system. You should not care what its value is.

    0 讨论(0)
  • 2020-12-01 14:41

    there is not such thing as ascii value of EOF. There is a ASCII standard that includes 127 characters, EOF is not one of them. EOF is -1 because that's what they decided to #defined as in that particular compiler, it could be anything else.

    0 讨论(0)
  • 2020-12-01 14:41

    EOF do not have ASCII value as they said .... no problem with this

    also you can avoid the strange character that appear in the end (which is the numeric representation of EOF ) by making if condition here is an example:

    #include <stdio.h>
    
    int main(void)
    {
     FILE *file  = fopen("/home/abdulrhman/Documents/bash_history.log", "r");
     FILE *file2 = fopen("/home/abdulrhman/Documents/result.txt", "w");
     file  = 
     file2 = 
     char hold = 'A';
    
     while(hold != EOF)
      {
        hold = getc(file);
        if(hold == EOF) break; // to prevent EOF from print to the stream
        fputc(hold, file2);
      }
    
     fclose(file);
     return 0;
    }
    

    that is it

    0 讨论(0)
提交回复
热议问题