0

I am using nullptr becuase it is handy and makes my code more readable. But the compiler gives me error below.

My question is what header file must be included in order to resolve the problem.

Here a piece of code:

FILE *fptr;
fptr=fopen("E:\\text.txt","w");
if(fptr==nullptr)
{
   perror("open()");
   return  EXIT_FAILURE;
}

error

 ||=== Build: Debug in 1 (compiler: GNU GCC Compiler) ===|
 E:\COOP\1\main.c||In function 'main':|
 E:\COOP\1\main.c|12|error: 'nullptr' undeclared (first use in this function)|
E:\COOP\1\main.c|12|note: each undeclared identifier is reported only once for each function it appears in|
 E:\COOP\1\main.c|27|error: expected declaration specifiers or '...' before
Pouyan
  • 363
  • 5
  • 22
  • The brilliant accepted answer to https://stackoverflow.com/questions/7016861/why-are-null-pointers-defined-differently-in-c-and-c explains it well. Note there is no `nullptr` keyword in C. – Bathsheba Jul 27 '17 at 11:08
  • 3
    C doesn't have a `nullptr` keyword or alias. It exists in C++ only. – Some programmer dude Jul 27 '17 at 11:08
  • Related: https://stackoverflow.com/questions/2419800/can-nullptr-be-emulated-in-gcc – CinCout Jul 27 '17 at 11:10
  • @Someprogrammerdude But IDE gives suggestion when I enter Nu.. – Pouyan Jul 27 '17 at 11:10
  • Then your IDE is wrong. Which IDE are you using? Which version of it? – Some programmer dude Jul 27 '17 at 11:12
  • 1
    @Ceeker: Well your IDE is being naughty in bringing C++ language into C code. – Bathsheba Jul 27 '17 at 11:12
  • @Ceeker: Are you compiling with g++ (C++) or gcc (C)? What Some programer dude said is absolutely right. There is no `nullptr` in C it's introduced in C++11. If you're compiling with g++ you maybe have to enable features from C++11. – Andre Kampling Jul 27 '17 at 11:12
  • @Someprogrammerdude I use CodeBlocks 16.1 and I've downloaded GNU GCC recently and I added to IDE correctly. – Pouyan Jul 27 '17 at 11:14
  • 2
    Then it seems there is a bug in CodeBlocks, in that it doesn't differentiate between C and C++ sources. If you type `ne` do the IDE attempt to replace it with `new` (another C++-only keyword)? If so then the problem is probably generic and not only for one or a few keywords. – Some programmer dude Jul 27 '17 at 11:16
  • @Bathsheba When I want to write code I choose whether I want to code in C or C++. There isn't anything wrong with compiler because I see that in compiler setting C compiler is gcc. – Pouyan Jul 27 '17 at 11:18
  • @Some programmer dude YES IT DOES!!!!! So what am I supposed to do if I want to use only CodeBlocks? Because I am comfortable with it. – Pouyan Jul 27 '17 at 11:20
  • 1
    Stop being comfortable with it! How can you be comfortable with an IDE that has autocompletion that autocompletes identifiers and keywords from a **wrong** language. What next? [`mysql_real_escape_string`](http://php.net/manual/en/function.mysql-real-escape-string.php)?? – Antti Haapala -- Слава Україні Jul 27 '17 at 11:25
  • 5
    I would go to [their ticket system](https://sourceforge.net/p/codeblocks/tickets/) and report it as a bug. They really need to separate C and C++. – Some programmer dude Jul 27 '17 at 11:25
  • @Someprogrammerdude In the early version of GCC which I used, there wasn't any suggestion when I type NU.. . Since I've upgraded it shows me suggestions both NULL and nullptr. – Pouyan Jul 27 '17 at 11:27

1 Answers1

2

nullptr is C++ only; it is not needed in C because in C ((void*)0) is convertible to any other pointer type without casts.

If you really really really like to type nullptr in C, you can use

#define nullptr ((void*)0)

and it would then work mostly the same.


Notice that C has the NULL macro from <stddef.h>; it is readable too, but its expansion is implementation-defined, so it might be either ((void*)0) or 0 (or something really strange); if it expands to 0, you wouldn't get any diagnostics from

int a = NULL;