string literals in c++

#include <iostream>
#include <string>
#include <stdlib.h>// using c standard library
int main()
{

	"Caleb";//String literal is a series of characters between two double quotes
	//It is a const char array and array is  just a ptr to a begining of block of memory
	//String literals are stored in read only section of memory

	const char name[8] = "Rhe\0eno";// \0 is null termination character
	std::cout << strlen(name) << std::endl;//c function for size of string ,output => 3 due to null terminating char \0
	std::cout << name << std::endl;// output => Rhe because null termination char \0

	const char* name1 = u8"Caleb";//1 byte per char
	const wchar_t* name2 = L"Caleb";//is 2 or 4 bytes per character dependent on compile on windows it's usually 2bytes but on linux it is 4 bytes per char
	const char16_t* name3 = u"Caleb";//2 byte per char
	const char32_t* name4 = U"Caleb";//4 byte per char


	using namespace std::string_literals;
	// this name space give   number of functions for convenience

	//std::string name0 = "Caleb" + "hello";//error because can't add ptr to ptr
	std::string name0 = "Caleb"s + "hello";//s is an operator that returns std::string this will work because now adding a ptr to actual string
	//various versions of strings
	std::wstring namee = L"Caleb"s + L"hello";//wide string: 2 or 4 bytes depend on compiler
	std::u32string namee32 = U"Caleb"s + U"hello";//u 32 string :4 bytes per char
	std::u16string namee16 = u"Caleb"s + u"hello";//u16 string :2bytes per char
	std::string namee8 = u8"Caleb";//1 byte per character
	const char* example = R"(Line1
Line 2
Line 3 
Line 4 
)";//R:Raw  for writing on different lines it prints escape characters 
	std::cin.get();

};

4.33
6
Ray Zhang 105 points

                                    string literal

4.33 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
C++ is string literal c++ text literals literal in cpp c++ literals operator string literals c++14 c++ what are literals c++ #define string literal template string literal c++ how to identify literals in c++ code find all the literals in c++ string literals in cpp what are literals c++ template literals in c++ Literals in c++ cerate string literal in c++ cpp literals what is the character literal in c++ c++ user defined string literals examples of string literals in c++ define literals in c++ literals c++ What is the definition of a literal in c++ c++ What kind of literals are there literal keyword C++ c++ string literal manipulation what is mean by Literals in c++ c++ which string literal should i use how to define a string literal in c++ string object and string literal in c++ c++ custom string literal what are literals in c++ c++ literals literal &quot;&quot;s for string c++ c++ literal string r string literal U(&quot;&quot;) c++ string literal U() c++ character literals in c++ what is the point of a Literals in c++ define a literal character C++ define string literal C++ What are literals in C++? literals in c++ valide integer c++ string literal format r srting c++ c++ declare wstring literal string literal declaration cppreference string literal c++ normal string c++ r before string literal what is literals in c++ c unicode string literal literals vs data types in c++ c++ 11 string literal tow string literal cpp string literal cpp raw string literals in c++ c++ r string prefix cpp string literal c++ string literals c++ R string l string and rstring c string prefixes c++ char \r c static string prefix utf16 c utf 8 string literal c++ r in front of string c++ string &quot;/r&quot; c++ string r prefix c++ string R&quot; c++ 11 raw string literal u8 to string c++ c++ string literal c++ raw string literal c++ raw literal string c++ raw literal c++ literal string c++ string litteral raw string c++ literal string c++ string literal in c++ string literal c++ string literals in c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source