The C++ char (string) and wchar_t (wstring) types usage program example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do:  Printing some text using C++ char and wchar_t type in C++ programming

To show: How to use the C++ char and wchar_t types in C++ programming




// the C++ char (string) and wchar_t (wstring) types example

#include <string>

#include <iostream>

using namespace std;

int main(void)


const basic_string <char> str1("TEST");

// uses the typedef for string word synonym to basic_string <char>

string str2("TEST");


// simple comparison between two objects of type basic_string

cout<<"String str1: "<<str1<<"\nString str2: "<<str2<<endl;

cout<<"Operation: (str1 == str2)?"<<endl;

if(str1 == str2)

cout<<"Strings str1 & str2 are equal."<<endl;


cout<<"Strings str1 & str2 are not equal."<<endl;


// L - literal qualifier for long

const basic_string <wchar_t> str3(L"TESTING");

// uses the typedef for wstring word synonym to basic_string <wchar_t>

wstring str4(L"JUMPING");


// simple comparison between two objects of type basic_string <wchar_t>

cout<<"\nString str3: TESTING \nString str4: JUMPING"<<endl;

cout<<"Operation: (str3 == str4)?"<<endl;

if(str3 == str4)

cout<<"Strings str3 & str4 are equal."<<endl;


cout<<"Strings str3 & str4 are not equal."<<endl;

return 0;



Output example:


String str1: TEST

String str2: TEST

Operation: (str1 == str2)?

Strings str1 & str2 are equal.


String str3: TESTING

String str4: JUMPING

Operation: (str3 == str4)?

Strings str3 & str4 are not equal.

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index