Home » C programming language

Value of EOF in c programming language.



EOF is a predefined MACRO with the value of -1 that means EOF is not a character. So EOF is returned through the function which is going to read content from the file.

Here is a C program, that will print the value of EOF

#include <stdio.h>

int main(){
	
	printf("Value of \"EOF\" is = %d\n",EOF);
	return 0;
}

Output

Value of "EOF" is = -1

Another program, to read and print characters (integer values) from file

There is a file "text1.txt" containing "Hello World"

#include <stdio.h>

int main(){
	FILE *fp;
	int ch;
	
	fp=fopen("text1.txt","r");
	if(fp==NULL){
		printf("Error in file opening...\n");
		return -1;		
	}
	
	printf("Content of the file:\n");
	/*here we are using infinite loop to print value of 
	EOF, loop will be terminate as we got EOF*/
	while(1){
		ch=getc(fp); //read one character
		printf("%c [%d],",ch,ch);
		if(ch==EOF){
			break; //terminate loop
		}
	}
	
	//close the file
	fclose(fp);
	
	return 0;	
}

Output

H [72],e [101],l [108],l [108],o [111], [32],W [87],o [111],r [114],l [108],d [100], 
 [10],� [-1],
EOF in C programming Language

Reference: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&id=1043284351






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.