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
🌐
Flavio Copes
flaviocopes.com › home › how to use null in c
How to use NULL in C - Flavio Copes
February 13, 2020 - It’s not a general “no value” marker you can assign to an int or a float. When we initialize a pointer, we might not always know what it points to. That’s when it is useful: ... NULL is not available by default: you need to include stdio.h to use it (or if you prefer, stddef.h):
🌐
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 - No surprises here: ... Write the NULL first to avoid errors (optional). The main disadvantage to the PTR == NULL method is the chance that you'll accidentally type ptr = NULL instead, assigning the NULL value to that pointer.
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
Why is there a NULL in the C language? - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
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
Lecturers rarely give out any relevant advice when it comes to programming languages, and they tend to write horrific code, so take everything they say with a grain of salt. ... No compiler defines or recognizes NULL. It's just a macro you can, but do not have to define. ... You can't define it in ... More on reddit.com
🌐 r/C_Programming
31
36
March 29, 2022
🌐
W3Schools
w3schools.com › c › c_null.php
C NULL
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... NULL is a special value that represents a "null pointer" - a pointer that does not point to anything.
🌐
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

```

🌐
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
🌐
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.
Top answer
1 of 2
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 2
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.

Find elsewhere
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
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 ...
🌐
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.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › null-pointer
Null Pointer in C Language (Uses, Best Practices, Examples)
July 27, 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.
🌐
Sanfoundry
sanfoundry.com › c-tutorials-null-character
NULL Character in C with Examples
December 31, 2025 - These terms may look similar, but they serve different purposes in C: ‘0’: A character that represents the digit zero. It has an ASCII value of 48. ... NULL: A macro that represents a null pointer, usually defined as ((void*)0). ‘\0’: ...
🌐
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.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 124426-how-write-null-character-file.html
how to write a null character to a file
March 4, 2010 - 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.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_null_pointer.htm
NULL Pointer in C
A NULL pointer in C is a pointer that doesn't point to any of the memory locations. 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
🌐
cppreference.com
en.cppreference.com › w › c › types › NULL.html
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 ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › c programming tutorial › null pointer in c
Null pointer in C | How Null pointer work in C with Examples
March 28, 2023 - So the null pointer is defined as the pointer that is assigned to zero to make it null pointer or a pointer that does not store any valid memory address or an uninitialized pointer are known as a NULL pointer. In general, we can a pointer that does not point to any object is known as a null pointer.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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, 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.
🌐
Tutorial and Example
tutorialandexample.com › null-character-in-c
Null character in C - TAE
March 28, 2022 - In other words, the Null character is used to represent the end of the string or end of an array or other concepts in C. The end of the character string or the NULL byte is represented by ‘0’ or ‘\0’ or simply NULL. The NULL character doesn’t have any designated symbol associated with it and also it is not required consequently.