The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 11-19-2007, 12:40 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
String L or without L

What is the difference between

Code:
L"Hello World"
and

Code:
"Hello World"
...??? thx

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 11-30-2007, 04:10 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 112
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
the L character indicates that the string literal is a "wide string literal". In other words, an array of wchar_t rather than just plain old char.

the 'wide char' is usually used for internationalisation, where you might want to use unicode instead of standard 1-byte characters.
(Note - the size of wchar_t is not 'set in stone', though on most systems I'm aware of, it usually supports at least UTF-16)

the C++ library has a whole bunch of wide character tools, all of which are similar to their 'narrow character' equivalents, but prefixed with a w. eg, wcout, wstring, wcin, etc.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (11-30-2007)
  #3 (permalink)  
Old 11-30-2007, 08:14 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
so, is it a good practice to keep L in tag every time we're writing a string? If not, then what's the point of having one kind over the other? I personally think that internationalization is an important part of codes...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 12-01-2007, 03:56 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 112
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Not really - wide character strings aren't just a simple modification of adding L's to your program. you need to use the wide character string type and related I/O streams too.

There's no point in storing simple strings (eg, "hello") in wide character format - since not only does it complicate things (wchar_t has no standard defined size, and its representations vary), it usually uses more bytes than necessary too.

Internationalisation extends beyond wide characters into areas such as number representation, date representation, etc. The usual approach is to write the user interface portion separately, then only the UI will be concerned with these issues, allowing the rest of the program to continue internally without having to worry about how it looks to the outside world.

If you want to make a habit of using wchar_t for everything, then feel free. although the more common approach is a conversion function to widen or narrow strings as you need to. eg,
Code:
#include <iostream>
#include <string>
#include <locale>

std::wstring widen_string(std::string str)
{
    std::wstring result;
    std::locale loc;
    for(int i(0); i < str.size(); ++i)
    {
        result += std::use_facet<std::ctype<wchar_t> >(loc).widen(str[i]);
    }
    return result;
}

int main()
{
    std::string str = "hello, world";

    std::wcout << widen_string(str);
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 12-01-2007 at 04:00 AM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (12-01-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 08:21 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50