Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is "L" macro(?) defined?

Tags:

c

winapi

static TCHAR szWindowClass[] = L"foo";

L is "glued" to the string "foo". How come? A function or a macro as I am used to is something like L("foo");

Can anyone explain me how come L be glued to the string?

like image 783
Fabricio Avatar asked Nov 14 '12 13:11

Fabricio


People also ask

How are macros defined #define?

noun. plural macros. Definition of macro (Entry 2 of 3) : a single computer instruction that stands for a sequence of operations.

How is a macro defined in C language?

The macro in C language is known as the piece of code which can be replaced by the macro value. The macro is defined with the help of #define preprocessor directive and the macro doesn't end with a semicolon(;).

Can I #define in a macro?

(1) You can define a macro of a macro as in a macro containing another macro. (2) However, you cannot define a macro of a macro like #define INCLUDE #define STDH include <stdio.


2 Answers

L is not a macro, it's just the standard prefix for wide (wchar_t, "Unicode") string literals; the concept is similar to the L suffix for long int literals, f suffix for float literals and so on1.

By the way, if you are using TCHARs you shouldn't be using L directly; instead, you should use the _T() or TEXT() macro, that adds L at the beginning of the literal if the application is compiled "for Unicode" (i.e. TCHAR is defined as WCHAR), or adds nothing if the compilation target is "ANSI" (TCHAR defined as CHAR).


  1. Although in my opinion this is confusing - why suffixes for arithmetic types and a prefix for wide string literals?
like image 80
Matteo Italia Avatar answered Oct 12 '22 11:10

Matteo Italia


It's not a macro, it's a token that the compiler recognizes as a prefix that has some special meaning.

The same happens with e.g. l as a suffix that produces long literals, as in long x = 42l;.

like image 41
Jon Avatar answered Oct 12 '22 11:10

Jon