user8626782
user8626782

Reputation:

Is a c-style string containing only one char considered a string?

Is a c-style string containing only one char considered a string or would you call that construct a char?

Upvotes: 0

Views: 1193

Answers (3)

Pravar Jawalekar
Pravar Jawalekar

Reputation: 605

Is a c-style string containing only one char considered a string or would you call that construct a char?

Indeed a C-Style string means a string i.e. it is quite different from a char data type. Since in C language, You don't have a dedicated built-in type to manipulate and represent string type like in C++ we have std::string hence once has to use character arrays (essentially null terminated) i.e. char str[SIZE] = "something" to represent character string type. On the other hand a single character is stored in char which is altogether different from char []. These two things are not same!

Example,

char str[] = "a"; // sizeof(str) will give 2 because presence of extra NULL character
char c = 'a'; // simply a single character

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234695

Zero or more characters followed by a NUL-terminator is a C-style string. You can use the double quotation character notation to define a literal.

In C, an int that can fit into a char, such as '3' is a char.

Something like '34' is multicharacter literal.

Upvotes: 1

A one element buffer is still technically a buffer. Forming a pointer to the start of it is not at all affected by how many items are in it.

So no, it's not a char. Furthermore, even the type system would differentiate char[1] from char.

It's also worth nothing that you may be surprised by what is a 1 character string. Because this one "a" has two characters in the buffer, not one. The only one character buffer that is a valid C-string is the empty string.

Upvotes: 1

Related Questions