
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is a null-terminated string in C/C++?
In C the strings are basically array of characters. In C++ the std::string is an advancement of that array. There are some additional features with the traditional character array. The null terminated strings are basically a sequence of characters, and the last element is one null character (denoted by ‘\0’). When we write some string using double quotes (“…”), then it is converted into null terminated strings by the compiler.
The size of the string may smaller than the array size, but if there are some null character inside that array, that will be treated as the end of that string.
See the following example. Here we have defined one string using std::string, then we will provide the same string, but there will be one \0 inside it.
Example
#include<iostream> using namespace std; main() { string my_string = "This is a sample text"; cout << my_string << endl; my_string = "This is a sam\0ple text"; //check the \0 cout << my_string; }
Output
This is a sample text This is a sam
- Related Articles
- What is terminated decimal?
- What is a string literal in C++?
- How to check if a variable is NULL in C/C++?
- C# Program to check a string for whitespace characters or null
- How to Convert a zero terminated byte array to string in Golang?
- What is @ in front of a string in C#?
- Null List in C#
- NULL pointer in C
- What is String Title case in C#?
- What is the type of string literals in C/ C++?
- What is the difference between String and string in C#?
- What is the type of string literals in C and C++?
- Check if a String is empty ("") or null in Java
- Null Pointer Exception in C#
- Why is address zero used for the null pointer in C/C++?

Advertisements