Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to put an L before the string and do I create this variable correctly?

I am sort of new to C++ and I got troubles understanding the usage of the data types fully.

I have these variables to be applied to createwindow parameters and the class with it. That takes an LPCWSTR data type.

LPCWSTR szTitle = L"Hello";
LPCWSTR szWindowClass = L"There";

Therefore I did that, although, I don't understand why I have to include the L before the string (the debugger put it there to be honest). I have also not too often seen strings be defined as the direct types (instead I often see WCHAR,char, etc). If you would make these variables, how would you write them? I don't believe I should be using LPCWSTR. Again sorry, I am fairly new and I can't find exactly what I'm looking for online.

like image 668
JohnSilver Avatar asked Mar 15 '17 17:03

JohnSilver


People also ask

What does L in front of string mean?

L is a prefix used for wide strings. Each character uses several bytes (depending on the size of wchar_t ). The encoding used is independent from this prefix.

Why do we use L before string in C++?

L is the prefix for wide character literals and wide-character string literals which tells the compiler that the char or string is of type wide-char. w is prefixed in operations like scanning (wcin) or printing (wcout) while operating wide-char type.

What is the L prefix?

The L prefix denotes a wide character/string literal; i.e., it is of type wchar_t instead of char. Unicode based programs typically use wide strings, while ANSI/ASCII based programs typically do not.

What is the proper syntax to create a string?

To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial. For example, you can assign a character 'a' to a variable single_quote_character .


1 Answers

C++ has several different character types, and the ones at issue here are char and wchar_t, with wchar_t being a wide character of some implementation-defined type. A string literal in C++ is treated like an array of characters, so you can write

const char* rawString = "I'm a regular old string!";

Because char and wchar_t aren't necessarily the same type, you can't write

const wchar_t* rawString = "I'm a regular old string!"; // Error!

because there's a type mismatch: you've got an array of chars on the right-hand side and a pointer of type const wchar_t* on the left. As a result, C++ lets you define wide string literals by prefixing a string literal with an L. the resulting string is then an array of elements of type wchar_t, so this will compile:

const wchar_t* rawString = L"I'm a wide string!"; // Totally fine!

Microsoft's alias LPCWSTR is essentially a const wchar_t*, which is why you need the L prefix.

like image 200
templatetypedef Avatar answered Oct 10 '22 14:10

templatetypedef