Skip to main content

What is the difference between EOF and NULL

EOF and NULL are standard defines which normally equate to an integer value,


NULL is always 0

EOF   is compiler dependent         most commonly its -1 (in gcc & g++ it is -1).


Functions that are defined to return a pointer (like fgets()) typically return NULL to indicate that an exceptional condition has prevented them from doing their usual job. NULL is used because it is not a value that the function could ever return in the normal, successful case. The only time the function will return NULL is when something strange happened.

Functions that are defined to return an integer (like fscanf()) typically return EOF when something exceptional happens. Functions that do this do it because EOF is a value that can not possibly occur in the normal case where the function was able to succeed at doing whatever it was supposed to do. The EOF value is unambiguous.

So, generally you can figure out whether you should be looking for NULL or for EOF by considering the return type of the function. However, it's always a good idea to check the documentation because it's possible that there could be several conditions that might cause the function to return one of these distinctive values. End-of-file is not the only possible cause.

BTW, a scary thing about those code fragments is that they do not check the result of fopen(). (Incidentally, since fopen() returns a pointer you'd expect it to return NULL if it fails. That's exactly what it does.) In a real program you would always, always check that return value.

Comments

Popular posts from this blog

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)