Variables that can store non-numerical values that are longer than one single character are known as strings.
The C++ language library provides support for strings through the standard string class. This is not a
fundamental type, but it behaves in a similar way as fundamental types do in its most basic usage.
A first difference with fundamental data types is that in order to declare and use objects (variables) of this type we
need to include an additional header file in our source code: <string> and have access to the std namespace
(which we already had in all our previous programs thanks to the using namespace statement).
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring;
return 0;
}
This is a string
As you may see in the previous example, strings can be initialized with any valid string literal just like numerical
type variables can be initialized to any valid numerical literal. Both initialization formats are valid with strings:
string mystring = "This is a string";
string mystring ("This is a string");
Strings can also perform all the other basic operations that fundamental data types can, like being declared without
an initial value and being assigned values during execution:
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
This is the initial string content
This is a different string content
The C++ language library provides support for strings through the standard string class. This is not a
fundamental type, but it behaves in a similar way as fundamental types do in its most basic usage.
A first difference with fundamental data types is that in order to declare and use objects (variables) of this type we
need to include an additional header file in our source code: <string> and have access to the std namespace
(which we already had in all our previous programs thanks to the using namespace statement).
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring;
return 0;
}
This is a string
As you may see in the previous example, strings can be initialized with any valid string literal just like numerical
type variables can be initialized to any valid numerical literal. Both initialization formats are valid with strings:
string mystring = "This is a string";
string mystring ("This is a string");
Strings can also perform all the other basic operations that fundamental data types can, like being declared without
an initial value and being assigned values during execution:
// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
This is the initial string content
This is a different string content
Learn More :
Programming
- stddef.h Definitions for common types, NULL, and errno
- stdarg.h Definitions for accessing parameters in functions that accept a variable number of arguments
- stat.h Definitions used for file status functions
- signal.h Definitions for ANSI defined signaling capability
- setjmp.h Defines typedef and functions for setjmp/longjmp.
- MCOMMAND Programming in C
- MCINPUT Programming in C
- MCDISPLY Programming in C
- MCALC Turbo C Programming
- using namespace std; in C++
- cout << "Hello World!"; in C++
- int main() in C++ Language
- return 0; in C++
- Constants in C++
- Calculate Cirrcumfrence Program in C++
- Structure of a program in C++ Learn
- alloc.h Memory Management Functions and Variables
- Defines structs, unions, macros, and functions for dealing with MSDOS and the Intel iAPX86 microprocessor family
- How do I create a configuration file?
- What is a configuration file? in C
- What is the difference between TC.EXE and TCC.EXE?
- How do I run Turbo C?
- How do I install Turbo C?