control character whose bits are all 0

The null character is a control character with the value zero. Many character sets include a code point for a null character โ€“ including Unicode (Universal Coded Character Set), ASCII (ISO/IEC 646), โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null_character
Null character - Wikipedia
1 week ago - Since the null character is not a printable character, representing it requires special notation in source code. In a string literal, the null character is often represented as the escape sequence \0 (for example, "abc\0def"). Similar notation is often used for a character literal (i.e. '\0') ...
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-tutorials-null-character
NULL Character in C - Sanfoundry
April 18, 2025 - C strings are arrays of characters ending with the NULL character โ€˜\0โ€˜. It tells the compiler and various standard library functions where the string ends. ... That โ€˜\0โ€˜ at the end helps functions like printf(), strlen(), strcpy() know where to stop reading the string.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null-terminated_string
Null-terminated string - Wikipedia
March 25, 2025 - UTF-16 uses 2-byte integers and as either byte may be zero (and in fact every other byte is, when representing ASCII text), cannot be stored in a null-terminated byte string. However, some languages implement a string of 16-bit UTF-16 characters, terminated by a 16-bit NUL (0x0000). Many attempts to make C string handling less error prone have been made.
๐ŸŒ
Tutorial and Example
tutorialandexample.com โ€บ null-character-in-c
Null character in C - TAE
When this piece of code is executed, an array of size 10 is created with string โ€œcomputerโ€ inside it which looks something like this; Here, the โ€˜\0โ€™ is used to indicate the end of a string. Note: The NULL character must not be overwritten with something else because it may lead to a condition where the end of the string cannot be determined.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 124426-how-write-null-character-file.html
how to write a null character to a file
When I opened the created file in vim editor,it showed me the character as ^@. You execute it. ... #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int main ( int argc, char *argv[] ) { int fd; if((fd=open("Temp",O_WRONLY,0777))<0) { perror("File"); exit(1); } printf ( "No of Characters Written:%d\n", write(fd,"\0",1) ); return 0; }
๐ŸŒ
YouTube
youtube.com โ€บ caleb curry
C Programming Tutorial 86 - Intro to Strings and Null Character - YouTube
Subscribe (Itโ€™s FREE!) - http://calcur.tech/subscribe (FREE) My C Programming Crash Course - http://calcur.tech/c-crash-course (FREE TRIAL) The C Programming...
Published ย  August 12, 2019
Views ย  7K
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.)

Find elsewhere
๐ŸŒ
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.
๐ŸŒ
OSDev Wiki
wiki.osdev.org โ€บ Null_Character
Null Character - OSDev Wiki
Some assemblers accept the tag ".asciiz" followed by an string between double quotation marks to create (unsually in the data section) a string, that then you can treat as a normal C-like string. Example under 64 bit Intel assembly, with NASM: bits 64 global main extern printf section .data str: db "Hello, OSDev!" ; NON-NULL-TERMINATED STRING strnll: db ", and goodbye!", 0 ; NULL-TERMINATED STRING section .text main: lea rdi, [str] xor rax, rax call printf ยท As printf will start reading the characters to print from address "str" until a NULL, the output of the program will be:
๐ŸŒ
ScienceDirect
sciencedirect.com โ€บ topics โ€บ computer-science โ€บ null-character
Null Character - an overview | ScienceDirect Topics
In the C programming language, a string is defined as an array of characters of type char, ending with the null character '\0'. 4 The null character (0x00) serves as a sentinel value that marks the end of a string, allowing functions to determine where the string terminates.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_strings.htm
Strings in C
Let us create a string "Hello". It comprises five char values. In C, the literal representation of a char type uses single quote symbols such as 'H'. These five alphabets put inside single quotes, followed by a null character represented by '\0' are assigned to an array of char types.
๐ŸŒ
University of Kent
cs.kent.edu โ€บ ~durand โ€บ CS2 โ€บ Notes โ€บ 01_Intro โ€บ c2_stringCstyle.html
C-Style Strings
There are two ways to keep track of the the number of items in an array: ... A C-style string is a null (denoted by \0) terminated char array. The null occurs after the last character of the string. For an initialization using double quotes, "...", the compiler will insert the null.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ null-character-c
How to Use NULL Character in C with Examples โ€“ Linux Hint
The constant โ€˜0โ€˜ has different meanings in different contexts. It can either be used to compare a pointer to 0, also called null pointer constant, or can be used as a NULL character โ€˜\0โ€™ to mark the end of an array or string in C programming.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ difference-between-null-pointer-null-character-0-and-0-in-c-with-examples
Difference between NULL pointer, Null character ('\0') and '0' in C with Examples - GeeksforGeeks
July 15, 2025 - The macro NULL is provided in the header file "stddef.h". Below are the ways to check for a NULL pointer: NULL is defined to compare equal to a null pointer as: ... if(!pointer) Null Characters('\0'): '\0' is defined to be a null character.
๐ŸŒ
University of Texas
farside.ph.utexas.edu โ€บ teaching โ€บ 329 โ€บ lectures โ€บ node21.html
Character strings
Here, 'f' represents the character ``f'', etc., and '\0' represents the so-called null character (ASCII code 0), which is used in C to signal the termination of a character string. The null character is automatically added to the end of any character string enclosed in double quotes.
๐ŸŒ
PrepBytes
prepbytes.com โ€บ home โ€บ c programming โ€บ null character in c
Null Character in C
August 3, 2023 - In C, strings are represented as arrays of characters terminated by a Null character. The Null Character in C at the end of the string ensures that functions and libraries designed to work with C strings can correctly identify the stringโ€™s length and avoid buffer overflows.
๐ŸŒ
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

```