4

I like C, I have a C book called Full and Full C and 2 C++, I find these languages fantastic because of their incredible power and performance, but I have to end many of my projects because of these various types.

I say to have std::string, LPCSTR, System::String, TCHAR [], char s [] = "ss", char * s?

This causes tremendous headaches mainly in GUI applications, WinAPI has the problem of LPCSTR not being compatible with char or std::string, and now in CLR applications if it has System::String that gives a lot of headache to convert to std::String or char * s or even char s [].

Why don’t C/C++ have its string type unique like String in Java?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Samuel Ives
  • 302
  • 3
  • 14
  • 3
    C only has (null-terminated) `char` arrays and string literals, no `string` type. And C++ provides some string classes (the most important being `std::string`) as part of its standard library, `LPCSTR` and `System::String` are not part of the language standard – UnholySheep Aug 11 '17 at 11:17
  • 1
    The unique string types as in Java are `std::string` in C++ and `char[]` with an ending `'\0'` in C. Other types than this are either typedefs, structs or own classes and that's not the fault of C or C++. – Andre Kampling Aug 11 '17 at 11:18
  • some of them are strings in the C++ understanding some are tables zero terminated values which are called in the C strings – 0___________ Aug 11 '17 at 11:18
  • 1
    Actually, ``LPCSTR`` is just a ``char *``, as it's also pointed out in the microsoft winapi doc. See also: https://stackoverflow.com/questions/1200188/how-to-convert-stdstring-to-lpcstr – Philipp Ludwig Aug 11 '17 at 11:20
  • 1
    Use `std::string` - *that* is the accepted C++ string type. Even though legacy C strings are available, they're a horror to use. All the others are non-standard. – paxdiablo Aug 11 '17 at 11:22
  • 6
    "_I would like C / C ++ to have its String type unique with String in java._" So why are you comparing 3 **different** languages, and asking why they have different string types, while Java is just a single language? C has `char*`/`char[]` (null-terminated array of characters). C++ has `std::string`, and C++/CLI (yet another language!), has `System::String`. `LPCTSTR` from WinApi is just a typedef to `TCHAR const *`, where TCHAR is either `char`, or `wchar_t`, depending on whether `UNICODE` is defined or not. You can see why it might not be compatible with `std::string`. – Algirdas Preidžius Aug 11 '17 at 11:22
  • You forgot BSTR, bstr_t, CComBSTR and CString etc.. As other people said these are all Windows strings. We use std::string in 95% of the code; rest when needed. – gast128 Aug 11 '17 at 11:38
  • By the way, if you really wanted to you could also create your own `String` class in Java (there's really no point to it, but it is possible) – UnholySheep Aug 11 '17 at 11:42
  • If you ask for C++ string types, why do you add C language tag? Please don't spam with unrelated tags. – Gerhardh Aug 11 '17 at 11:44
  • C++ has single string type - `std::string`. The rest are not strings - it's "array of chars", so Java equivalent would be `char[]` or `byte[]`. BTW. as we speak of duplication, Java has `StringBuilder`, `StringBuffer` (BTW. do you know the difference between the two? ;D)... – el.pescado - нет войне Aug 11 '17 at 11:54
  • Three of those "string" types are Microsoft-isms. Microsoft's software is typically over-designed and bloated. – Pete Becker Aug 11 '17 at 12:08
  • _“I have to end many of my projects because of these various types.”_ No reason for that! These types can be converted. For example, I use `char*`, `std::string`, `std::vector`, `QByteArray` and `QString` in different classes of a single program and convert them when needed without any hassle. – Melebius Aug 11 '17 at 12:11
  • As Java came later, the question should be "Why does Java have a unique string type, unlike C/C++" – chux - Reinstate Monica Aug 11 '17 at 12:31
  • Windows API is older than C++ – M.M Aug 11 '17 at 12:39
  • Is WinAPI not as useful as the new gui libs nowadays? There are Qt and wx but both are many similar to native api, such as Qt which is used by autodesk, adobe, allegorithmic among other famous companies. – Samuel Ives Aug 11 '17 at 13:10

3 Answers3

9

There are no "many types of string in c++". Canonically there is one template std::basic_string, which is basically a container specialized for strings of different character types.

std::string is a convenience typedef onto std::basic_string<char>. There are more such typedefs for different underlying character types.

AFAIK, standard c has also only one officially recognized string standard. It's ANSI-string i.e. a null terminated array of char.

All other you mention are either equivalent of this (e.g. LPCSTR is a long pointer to a constant string i.e. const char*), or some non-standard extensions written by library providers.

Your question is like asking why there are so many GUI libraries. Because there is no standard way to do this, or standard way is lacking in some way, and it was a design decision to provide and support own equivalent type.

Bottom line is, that on the library level, or language level, it's a design decision between different trade-offs. Simplicity, performance, character support, etc. etc. In general, storing text is hard.

luk32
  • 15,812
  • 38
  • 62
  • There is no such thing as "ansi string" in C. There are many different kinds of strings in C including wide strings, char16_t strings and char32_t strings. – Antti Haapala -- Слава Україні Aug 11 '17 at 13:39
  • Could you point me to a in the C standard that would define any string so I can supplement my answer? I have tried searching [a draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) for `char32_t` and found nothing. I was sure null terminated strings were standardized, but I might be wrong. I am not really a C expert. – luk32 Aug 11 '17 at 15:34
  • @luk32: [C 2011 Online Draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), section 7.1.1. – John Bode Aug 11 '17 at 16:02
  • @luk32 wrong version. C11 final draft, aka [n1570](http://port70.net/~nsz/c/c11/n1570.html#6.4.5) – Antti Haapala -- Слава Україні Aug 11 '17 at 18:15
4

Well, first we must answer the question: What is a string?

The C-standard defines it as a contiguous sequence of characters terminated by and including the first null character.1
It also mentions varieties using wchar_t, char16_t, or char32_t instead of char.
It also provides many functions for string-manipulation, and string-literals for notational convenience.

So, a sequence of characters can be a string, a char[] might hold a string, and a char* might point to one.
LPCSTR is a windows typedef for const char* with the added semantics that it should point to a string or be NULL.
TCHAR is one of a number of preprocessor-defines used for transitioning windows code from char to wchar_t. Depending on what TCHAR is, a TCHAR[] might be able to hold a string, or a wide-string.


C++ mixes up things a bit because it adds a data-type for handling strings. To reduce ambiguity, string is only used for the abstract concept, you have to rely on the context to disambiguate or be more explicit.

So the C string corresponds with the C++ null-terminated-byte-string, or NTBS.2
Yes, C++ also knows their wide varieties.
And C++ incorporates the C functions and adds some more.
In addition, C++ has std::basic_string<> for storing all kinds of counted strings, and some convenience-typedefs like std::string.


And now we get to the third language yet, namely C++/CLI.
Which incorporates all I spoke above from C++, and adds the CLI type System::String into the mix.
System::String is an immutable UTF-16 counted-string.


Now to answer the question why C++ does not define one single concrete type to be a string can be answered:

There are different types of string in C++ for interoperability, history, efficiency and convenience. Always use the right tool for the job.
Java and .Net do the same with byte-arrays, char-arrays, string-builders and the like.


Reference 1: C11 final draft, definition of string:

7. Library

7.1 Introduction

7.1.1 Definitions of terms

1 A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.

Reference 2: C++1z draft n4659 NTBS:

20.4.2.1.5.1 Byte strings [byte.strings]

1 A null-terminated byte string, or NTBS, is a character sequence whose highest-addressed element with defined content has the value zero (the terminating null character); no other element in the sequence has the value zero.163
2 The length of an NTBS is the number of elements that precede the terminating null character. An empty ntbs has a length of zero.
3 The value of an NTBS is the sequence of values of the elements up to and including the terminating null character.
4 A static NTBS is an NTBS with static storage duration.164

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

Each string type has his own target, the std::string is for standard library and this is the most common.

As you say C++ has power and performance and these strings allow more flexibility. Char[] and char* you can use for a more generic use of string.

bolov
  • 72,283
  • 15
  • 145
  • 224
Check
  • 130
  • 2
  • 10