Null character
Updated: 07/31/2022 by Computer Hope
A null character is programming code representing a character with no value, a missing value, or used to denote the end of a character string. In databases and spreadsheets, a null character can also be used as padding. An example of a null character is when a software developer declares a variable without specifying a value, or sets it equal to nothing.
Null character examples in coding
Below are examples of null characters used in the C and JavaScript programming languages.
Example code in C
#include <stdio.h> #include <string.h> int main () { char str[] = "computerhope"; printf("As a string:\n"); printf("%s\n",str); printf("As an array of characters (character, decimal value of character):\n"); printf("'%c' %d\n",str[0],str[0]); printf("'%c' %d\n",str[1],str[1]); printf("'%c' %d\n",str[2],str[2]); printf("'%c' %d\n",str[3],str[3]); printf("'%c' %d\n",str[4],str[4]); printf("'%c' %d\n",str[5],str[5]); printf("'%c' %d\n",str[6],str[6]); printf("'%c' %d\n",str[7],str[7]); printf("'%c' %d\n",str[8],str[8]); printf("'%c' %d\n",str[9],str[9]); printf("'%c' %d\n",str[10],str[10]); printf("'%c' %d\n",str[11],str[11]); printf("'%c' %d\n",str[12],str[12]); return 0; }
Example output from C
Example code in JavaScript
<!DOCTYPE html> <html> <head> <title>Null Character Example</title> </head> <body> <h3>Null Character Example</h3> <script> let c; document.write("<p>'" + c + "'</p>"); // undefined c = "\0"; document.write("<p>'" + c + "' " + c.length + "</p>"); // null c = "computerhope"; document.write("<p>'" + c + "' " + c.length + "</p>"); // computerhope without a null character c = "computerhope\0"; document.write("<p>'" + c + "' " + c.length + "</p>"); // computerhope with a null character </script> </body> </html>
Example output from JavaScript
Blank, Empty, Nbsp, Non-printing character, Null, Null pointer, Programming terms, Undefined