Difference Between Null Strings and Empty String in C++ Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, we generally initialize a string that does not store anything at declaration as either a null string or an empty string. They both may seem similar to each other but they are different in terms of what they store or refer to. In this article, we will discuss the key differences between the null strings and empty strings in C++. Null String in C++A null string is a character pointer that points to the NULL. It means that it is not pointing to any memory location and does not refer to a valid string, and if we try to access or manipulate it, it will show undefined behavior. So we need to make proper checks and precautions when working with null pointers. Syntax to Define Null String in C++ char *str = NULL;ExampleThe below example demonstrates that we cannot access a null string. C++ // C++ program to demonstrate initialization of null string #include <iostream> #include <string> using namespace std; int main() { // Initializing null string char* str = NULL; // Attempting to access null string may lead to // undefined behavior cout << "First character: " << str[0] << endl; return 0; } Explanation: The above program leads to undefined behavior as we are trying to access null string which is not possible. Empty String in C++Initializing a string as an empty string initialing it with a string having no characters. In this case, the char pointer points to the valid memory address and stores a single '\0' NULL character. Syntax to Define Empty String in C++string emptyStringName = "";ExampleThe below example demonstrates the initialization of string as empty string. C++ // C++ program to initialize an empty string and print it's // size #include <iostream> #include <string.h> using namespace std; int main() { // Initializing empty string string emptyStr = ""; // or string emptyStr; // Accessing empty string safely cout << "Empty String Size: " << emptyStr.size() << endl; return 0; } OutputEmpty String Size: 0 Difference Between Null String and Empty String in C++The key difference the we can expect when initializing string as null vs empty are given below: Feature Null String Empty String Initialization Pointer initialized to NULL String object created with no characters Validity Not a valid string. Valid string object with zero characters. Accessing data May lead to undefined behavoiur if accessed. Well-defined operations on an empty string. Crash Risk Higher risk due to potential null pointer. Lower risk, due to operations on a valid object Comment More infoAdvertise with us Next Article Difference Between Null Strings and Empty String in C++ chalti Follow Improve Article Tags : C++ Programs C++ cpp-string CPP Examples Practice Tags : CPP Similar Reads Difference Between std::wstring and std::string The std::wstring and std::string are the classes in C++ used to store sequences of characters. While serving similar purposes, they serve different requirements. In this article, we will look at some major differences between the std::wstring and std::string in C++. Wide String in C++The std::wstrin 3 min read Difference Between string and char[] Types in C++ In C++, we can store the sequence of characters i.e. string in two ways: either as a std::string object or char array. In this article, we will discuss some major differences between string and char[] in C++.Character Array Type StringsA character array is simply an array in C++ that contains charac 2 min read Difference between cout and puts() in C++ with Examples Standard Output Stream(cout): The C++ cout statement is the instance of the ostream class. It is used to display output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertio 2 min read Difference Between Array of Characters and std::string in C++ In C++, we have character array and std::string class both of which are used to store the sequence of characters. The character arrays are a part of C-style programming on the other hand std::string is a part of the C++ standard library. In this article, we will discuss what are some major differenc 3 min read What is the Difference Between C++ String == and compare()? In C++ == and compare() both are used to compare strings and find if the given strings are equal or not but they differ in working. In this article, we will learn the key differences between == and compare() of string in C++. "==" Operator in C++The == operator in C++ is used to compare two strings 3 min read How to Check if a String is Empty in C++? In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for a 2 min read std::string::empty() in C++ The std::string::empty() function in C++ is a predefined member function of the std::string class template. This function is used to check if a string is empty, i.e., whether its length is 0 or not, and returns a boolean value true if the string is empty otherwise, it returns false. Syntax of String 2 min read How to Create a Stack of Strings in C++? In C++, Stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to create a stack of strings in C++. Creating a Stack of Strings in C++To crea 1 min read How to Convert a Single Character to String in C++? A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co 4 min read C++ Program to Append a String in an Existing File Here, we will build a C++ program to append a string in an existing file using 2 approaches i.e. Using ofstreamUsing fstreamC++ programming language offers a library called fstream consisting of different kinds of classes to handle the files while working on them. The classes present in fstream are 2 min read Like