Question

Am I right in saying that the difference between 'A' and "A" in C is:

'A' - Means a character 1 byte (8 bits) long containing A.

"A" - Means a string which is 2 bytes (16 bits) long which holds an A and a NULL character.

Are my explanations correct?

Thanks.

Was it helpful?

Solution

You are absolutely correct.But you got to bear in mind that when you use those as rvalues they are different.One has the type char, and can be used as an int as it is implicitly converted to the ASCII value, while the other has type char*.

Let me illustrate my point with a code if that helps:

 int num='A';  //Valid, assigns 65 to num
 char test=65;  //Valid, as test will be 'A' after this
 char *ptr="A"  //Valid, assigns  address of string "A" to pointer ptr
 printf("%c,%d",'A','A'); // Output will be   A,65
 printf("%p",(void*)"A"); //Will print address where string "A" is
 printf("%c","A"); ///WRONG
 printf("%s","A"); //Works

Edit For the finer nuances, if you feel your understanding is up to that mark yet,refer to Mat's comment.Else read it after a few weeks when you have advanced further in your study of C.

OTHER TIPS

Basically yes. The main nuance is byte vs. char. Where you say byte, you should say char. On most systems a char is one byte. There are a few that use larger objects to store a char.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top