Patel Nik
Patel Nik

Reputation: 520

What is the ascii value of EOF in c.?

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);

but its print blank screen.

so anyone know which key for EOF.

Upvotes: 41

Views: 127975

Answers (7)

Vineet Kapoor
Vineet Kapoor

Reputation: 8289

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;
}

Upvotes: 2

user1541890
user1541890

Reputation: 31

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.

Upvotes: -6

Abdulrhman.Z
Abdulrhman.Z

Reputation: 53

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

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613612

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.

Upvotes: 15

BlackBear
BlackBear

Reputation: 22989

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:\>

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215647

EOF (as defined in the C language) is not a character/not an ASCII value. That's why getc returns an int and not an unsigned char - because the character read could have any value in the range of unsigned char, and the return value of getc also needs to be able to represent the non-character value EOF (which is necessarily negative).

Upvotes: 58

daniel
daniel

Reputation: 9845

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.

Upvotes: 2

Related Questions