0

what is the difference between

char name='chiheb';

char name="chiheb";

I'm also confused about how char can allow many characters .what is the difference then between string and char;

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Does this answer your question? [Single quotes vs. double quotes in C or C++](https://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c-or-c) – Sorenp Jun 26 '21 at 12:47
  • Single quote is for char. Double quotes are fir character pointers (pointers to strings of characters) – L. Scott Johnson Jun 26 '21 at 12:47
  • A **string** is an *array* of `char` with a NUL terminator. Some C libraries do define a `string` type (which isn't an array but a pointer), and that is often considered to be unhelpful. – Weather Vane Jun 26 '21 at 12:51

1 Answers1

1

In the first declaration on object of the type char is initialized by a multi-byte integer character constant the value of which is implementation defined.

For the second declaration the compiler will issue a message that you are trying to convert a pointer to an integer because the string literal used as an initializer is converted to pointer to its first element of the type char *.

A valid declaration will look like for example

char name = "chiheb"[0]; // or some other used valid index

or

char name = *"chiheb";

or

char *name = "chiheb";
halfer
  • 19,824
  • 17
  • 99
  • 186
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • i didnt understand this part : In the first declaration on object of the type char is initialized by a multibyte integer character constant the value of which is implementation defined. – chiheb nouri Jun 26 '21 at 14:35
  • @chihebnouri The value of this multibyte integer character constant 'chiheb' is implementation defined. So you can not say apriore how the variable name will be initialized. – Vlad from Moscow Jun 26 '21 at 21:09