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:

NULL

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
🌐
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
🌐
Flavio Copes
flaviocopes.com › c-null
How to use NULL in C
NULL is not available by default: you need to include stdio.h to use it (or if you prefer, stddef.h: #include <stdio.h> int main(void) { int * p_some_variable = NULL; } ... hello.c:3:26: error: use of undeclared identifier 'NULL' int * ...
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:

NULL

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.

🌐
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
NULL 0L: A long integer literal representing a null pointer. 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. This macro doesn't return any value.
🌐
cppreference.com
en.cppreference.com › w › c › types › NULL.html
NULL - cppreference.com
#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 ...
🌐
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.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 124426-how-write-null-character-file.html
how to write a null character to a file
Program terminated.", filename); abort(); } str_length = strlen(buffer); fwrite(buffer, str_length, 1, pFile); //writing the string to the file fclose(pFile); printf("\nFile write complete\n"); //reading that data from file pFile = fopen("test_write.txt","r"); //opening the file for reading if(pFile == NULL) { printf("Error opening %s for writing. Program terminated.", filename); abort(); } char data[1024]; memset(data,'\0',sizeof(data));//initializing with nulls o avoid buffer problems fread(data,sizeof(char),34,pFile);//reading the data what i have written to file fclose(pFile); printf("%s\n",data);//this prints this is for testing pFile = fopen(filename, "w"); //again opening for writing if(pFile == NULL) { printf("Error opening %s for writing.
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 // Unstop
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.
🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
It is always recommended to check ... #include <stdio.h> int main(){ int *ptr = NULL; // null pointer if (ptr == NULL) { printf("Pointer is a NULL pointer"); } else { printf("Value stored in the address referred by the pointer: %d", *ptr); } return 0; }...
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.)

🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › null-statement-c
Null Statement (C) | Microsoft Learn
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 ...
🌐
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.
🌐
Handmade Network
hero.handmade.network › forums › code-discussion › t › 1292-null_define_in_c__c_differs!
NULL Define in c & c++ differs?! | Handmade Network
It is defined as (void *)0 because then, the pointer then points to nothing. void is a special type that refers to nothing, but this makes it useful to refer directly to a location in memory.
🌐
Scaler
scaler.com › topics › null-pointer-in-c
What is Null Pointer in C? - Scaler Topics
November 9, 2022 - In the C programming language, a null pointer is a pointer that does not point to any memory location and hence does not hold the address of any variables. 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.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
August 29, 2025 - Learn about the null pointer in C language, including its syntax, uses, how to check it, best practices, examples, and more. Read now!
🌐
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.
🌐
Javatpoint
javatpoint.com › null-character-in-c
Null character in C - javatpoint
Null character in C with Tutorial, C language with programming examples for beginners and professionals covering concepts, c pointers, c structures, c union, c strings etc.