There's NULL and then there's NUL.
NULL is defined in stddef.h, is used very widely, and is a reference to a null pointer.
NUL is different - it is the first character in the standard ASCII character set, and more importantly, it is not a standard macro. You may have to define it yourself.
To define NUL, do:
#define NUL '\0'
Answer from CrimsonDiego on Stack OverflowThere's NULL and then there's NUL.
NULL is defined in stddef.h, is used very widely, and is a reference to a null pointer.
NUL is different - it is the first character in the standard ASCII character set, and more importantly, it is not a standard macro. You may have to define it yourself.
To define NUL, do:
#define NUL '\0'
No, that's not standard. Add this at the beginning of your code or just use 0:
#define NUL 0
I infered you're not looking for NULL since you're doing ch == NUL
In various texts it is quite frequent to refer to '\0' as NUL.
'NULL' undeclared (first use in this function)
'NULL' undeclared...
file - null type and compiler error in c - Stack Overflow
C Errors (Beginner) - Stack Overflow
The check should be against NULL not null.
while (fgets(string,17,f)!=NULL )
As the error message says: the compiler does not know the symbol null.
The reason: there is no symbol null defined by the c standard.
You may (the recommended way) use NULL that is defined in stddef.h or make your own define.
FILE and NULL are wrongly written (C is case sensitive). fopen line was missing a semicolon.
Code below compiles and runs.
#include <stdio.h>
main(void) {
FILE *file = fopen("words.txt","r");
if(file != NULL) {
char line[128];
while(fgets( line, sizeof line, file) != NULL)
{
fputs ( line, stdout );
}
fclose ( file );
}
}
The following works fine and has been cleaned up with consistent braces and spaces in function calls and definitions and loops/ifs. It also works in C, if that was your intent from the tags, and prints an error and returns 1 if the file cannot be opened.
#include <stdio.h>
#include <errno.h>
int main (void) {
char line[128];
FILE *file = fopen ("words.txt", "r");
if (file != NULL) {
while (fgets (line, sizeof line, file) != NULL) {
fputs (line, stdout);
}
fclose (file);
} else {
fprintf (stderr, "Cannot open 'words.txt', error = %d\n", errno);
return 1;
}
return 0;
}
NULL is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal 0 instead, the compiler will do the right thing depending on the context.
In newer C++ (C++11 and higher), use nullptr (as pointed out in a comment, thanks).
Otherwise, add
#include <stddef.h>
to get the NULL definition.
Do use NULL. It is just #defined as 0 anyway and it is very useful to semantically distinguish it from the integer 0.
There are problems with using 0 (and hence NULL). For example:
Copyvoid f(int);
void f(void*);
f(0); // Ambiguous. Calls f(int).
The next version of C++ (C++0x) includes nullptr to fix this.
Copyf(nullptr); // Calls f(void*).