Actually, you can use a literal 0 anyplace you would use NULL.

Section 6.3.2.3p3 of the C standard states:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

And section 7.19p3 states:

The macros are:

CopyNULL

which expands to an implementation-defined null pointer constant

So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0.

Answer from dbush on Stack Overflow
Top answer
1 of 5
7

Actually, you can use a literal 0 anyplace you would use NULL.

Section 6.3.2.3p3 of the C standard states:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

And section 7.19p3 states:

The macros are:

CopyNULL

which expands to an implementation-defined null pointer constant

So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0.

2 of 5
5

NULL is used to make it clear it is a pointer type.

Ideally, the C implementation would define NULL as ((void *) 0) or something equivalent, and programmers would always use NULL when they want a null pointer constant.

If this is done, then, when a programmer has, for example, an int *x and accidentally writes *x = NULL;, then the compiler can recognize that a mistake has been made, because the left side of = has type int, and the right side has type void *, and this is not a proper combination for assignment.

In contrast, if the programmer accidentally writes *x = 0; instead of x = 0;, then the compiler cannot recognize this mistake, because the left side has type int, and the right side has type int, and that is a valid combination.

Thus, when NULL is defined well and is used, mistakes are detected earlier.

In particular answer to your question “Is there a context in which just plain literal 0 would not work exactly the same?”:

  • In correct code, NULL and 0 may be used interchangeably as null pointer constants.
  • 0 will function as an integer (non-pointer) constant, but NULL might not, depending on how the C implementation defines it.
  • For the purpose of detecting errors, NULL and 0 do not work exactly the same; using NULL with a good definition serves to help detect some mistakes that using 0 does not.

The C standard allows 0 to be used for null pointer constants for historic reasons. However, this is not beneficial except for allowing previously written code to compile in compilers using current C standards. New code should avoid using 0 as a null pointer constant.

🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
We just have to assign the NULL value. Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as “stdio.h”, “stddef.h”, “stdlib.h” etc.
Published   January 10, 2025
Discussions

How do I print out the null value in a string?
You can have characters that don't have any associated glyphs — that is, that have no graphical representation. This will almost certainly be the case with the null character (and other so-called "control" characters) on your system. So what do you expect to see? More on reddit.com
🌐 r/C_Programming
23
0
December 4, 2023
string - Writing NULL char to file in C - Stack Overflow
I am attempting to write an array of char to a BMP file in C. The problem with this is that whilst 0x00 values are required for the file, it seems C interprets this as the end of string when writin... More on stackoverflow.com
🌐 stackoverflow.com
Old compilers and NULL
NULL is a macro that should be defined in a header . In theory it's possible that some compiler ships without a definition for it (can anyone comment if there's a C standard where this is permitted?), in practice you've simply forgotten to include the right header. More on reddit.com
🌐 r/C_Programming
31
36
March 29, 2022
Why do we use Null pointers?
The value where a null pointer is pointing is irrelevant. A null pointer is a pointer that is pointing nowhere--there is no value. In C, the number 0 was defined as the representation of the null pointer. They could have picked any number, but it would have been painful. More on reddit.com
🌐 r/C_Programming
56
0
August 30, 2024
🌐
W3Schools
w3schools.com › c › c_null.php
C NULL
For example, fopen() returns NULL if a file cannot be opened, and malloc() returns NULL if memory allocation fails. We can check for this using an if statement, and print an error message if something goes wrong. In this example, we try to open a file that does not exist.
🌐
Reddit
reddit.com › r/c_programming › how do i print out the null value in a string?
r/C_Programming on Reddit: How do I print out the null value in a string?
December 4, 2023 -

I learned in C that a string ends with a null value, "\0". How do I print out this null value in C?

I tried doing this by scanning the string "paint". However, it doesn't seem to work -

```
#include <stdio.h>
int main() {
char name[100];
scanf("%s", name);
printf("The name is %c", name[5]);
}

```

This is my output -
```
paint

The name is some weird symbol looking like 0

Process finished with exit code 0

```

🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_macro_null.htm
C library - NULL Macro
Following is the C library syntax of the NULL Macro. #define NULL ((char *)0) or, #define NULL 0L or #define NULL 0 · This is not a function. So, it doesn't accept any parameter.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › c programming languages › how to check null in c: 7 steps (with pictures) - wikihow
How to Check Null in C: 7 Steps (with Pictures) - wikiHow
June 9, 2025 - Since testing for (in)equality treats the operands symmetrically, you can get exactly the same result by writing if (NULL == ptr) instead. This is more typo-resistant, since an accidental NULL = ptr creates a simple compile error. This looks a little awkward to some programmers, but it's perfectly valid.
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Null-Pointers.html
Null Pointers (GNU C Language Manual)
Next: Dereferencing Null or Invalid Pointers, Previous: Dereferencing Pointers, Up: Pointers [Contents][Index] A pointer value can be null, which means it does not point to any object. The cleanest way to get a null pointer is by writing NULL, a standard macro defined in stddef.h.
Find elsewhere
🌐
Unstop
unstop.com › home › blog › null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples
May 3, 2024 - These pointers are used for error handling, memory management, and ensuring program stability. By initializing pointers to NULL, verifying against NULL before dereferencing, and using null pointers in C code to indicate failure or exceptional conditions in functions, developers can write robust and reliable programs.
🌐
Sanfoundry
sanfoundry.com › c-tutorials-null-character
NULL Character in C with Examples
December 31, 2025 - That ‘\0‘ at the end helps functions like printf(), strlen(), strcpy() know where to stop reading the string. ... Both are valid, but the string literal version is cleaner and more common. 📌 Limited Seats! Register Now - Free AI-ML Certification (June 2026) Example 1: Auto Null-Termination with String Literal · #include <stdio.h> int main() { char language[] = "C Programming"; // '\0' is added automatically printf("Language: %s\n", language); return 0; }
🌐
cppreference.com
en.cppreference.com › c › types › NULL
NULL - cppreference.com
January 12, 2024 - #include <inttypes.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> int main(void) { // any kind of pointer can be set to NULL int* p = NULL; struct S *s = NULL; void(*f)(int, double) = NULL; printf("%p %p %p\n", (void*)p, (void*)s, (void*)(long)f); // many pointer-returning functions ...
🌐
WsCube Tech
wscubetech.com › resources › c-programming › null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
March 18, 2026 - Learn in this tutorial about the null pointer in C, including its syntax, uses, how to check it, best practices, and examples to write efficient programs.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › null-statement-c
Null Statement (C) | Microsoft Learn
August 3, 2021 - The correct way to code a null statement is: ... Statements such as do, for, if, and while require that an executable statement appear as the statement body. The null statement satisfies the syntax requirement in cases that do not need a substantive ...
Top answer
1 of 2
9

Don't use fprintf() to write binary data, of course it's going to interpret its formatting string as a string. That's what it does!

Use fwrite(), and open your file in binary mode with "wb".

You can use sizeof to compute the size of the array, no need to hardcode the value:

FILE *picFile = fopen("pic.bmp", "wb");
if(picFile != NULL)
  fwrite(bmp1, sizeof bmp1, 1, picFile);
fclose(picFile);

This works because it's in the same scope as the array declaration of bmp1.

2 of 2
2

The function fprintf() and its relatives are used to format some information and produce a string then write its characters1 into a file or put it on screen or store it into a given array of characters.

Use function fwrite() to write binary data; this function does not interpret the data you give it in any way and just writes the number of bytes you specify into the file.

Try this:

FILE *picFile = fopen("pic.bmp","w");
fwrite(bmp1, sizeof(bmp1), 1, picFile);
fclose(picFile);

(your call to fprintf() was erroneous, anyway)


1 The functions sprintf() and snprintf() (they put the generated string into a provided buffer of characters) copy the entire generated string onto their destination buffer, including the null terminating character.
The functions fprintf() (writes the string into a file) and printf() (puts the string on screen) do not put the null terminating character of the generated string into the output stream.

(Thanks @chux for pointing out that the C strings include the null terminating character.)

🌐
Scaler
scaler.com › home › topics › what is null pointer in c?
What is Null Pointer in C? - Scaler Topics
September 4, 2023 - In the C programming language, ... It just stores the segment's base address. That is, the null pointer in C holds the value Null, but the type of the pointer is void....
🌐
ThoughtCo
thoughtco.com › definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - The C and C++ programming, a pointer is a variable that holds a memory location. The null pointer is a pointer that intentionally points to nothing. If you don't have an address to assign to a pointer, you can use null. The null value avoids memory leaks and crashes in applications that contain pointers.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
The NULL constant is defined in the header files stdio.h, stddef.h as well as stdlib.h. A pointer is initialized to NULL to avoid the unpredicted behavior of a program or to prevent segmentation fault errors.
🌐
Quora
quora.com › What-are-all-ways-a-null-is-used-in-C-and-C-besides-a-null-pointer
What are all ways a null is used in C and C++ besides a null pointer? - Quora
That’s it - Because otherwise it’s just 0. In fact in C, NULL is “defined” as 0 and if you have a string, they’re terminated with a null but if you look at a string(character buffer that ...
🌐
Quora
quora.com › How-can-we-assign-null-value-to-an-integer-variable-in-C
How can we assign null value to an integer variable in C? - Quora
Answer (1 of 4): You cannot set the value of an integer to null. You can only set a pointer to null. So, you can set an integer pointer to null.
🌐
Javatpoint
javatpoint.com › null-pointer-in-c
Null Pointer in C - javatpoint
Null Pointer in C with programming examples for beginners and professionals covering concepts, control statements, c array, c pointers, c structures, c union, c strings and more.