-5

Please explain what is going on. I am unable to make sense of the output.

#include <stdio.h>
int main()
{
    char c="c";
    printf("%c",c);
    return 0;
}

Also what will be the output when c="false", "e",'false' etc

I wish to understand whats going on. I know that char are to be used like: 'a'.

PS The code runs perfectly fine and was asked in my undergraduate course exam for Progamming Introduction.

2 Answers2

1

Doing

char c = "c"

is wrong, "c" is an array which contains one element and a null termination, the first element is 99, also called 'c'. For eg.

char ab[2] = "ab"

Here, "ab" is equivalent to {'a', 'b', '\0'} The '\0' at the end is explained below. "c" is equivalent to {'c', '\0'} and arrays generally point to the first element of the array. So, in your code c actually points to an array, whose first element is c. So the pointer to that array also points to 'c'.

char c = *"c"

actually works but is bad. You could also change the printf to printf("%c\n", *c) if c were a pointer, deferencing c will give you the first element of the array and c is the first and element.

You should do

char c = 'c'

And the null termination of strings is because:

"c" or any other string is actually {'c', '\0'} with a null termination at the end. It's need so that we can know when a string ends. For eg.

In "abcdefgh", we would have no idea when the string ended, the user would have to know how long each string is plus how long the buffer is, so it is terminated by a '\0' at he end and the string is stopped to be parsed when '\0' is reached. So, "abcdefgh" is actually {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '\0'}.

Shambhav
  • 813
  • 7
  • 20
  • `"c"` is an array that contains *two* elements and is not equivalent to `{'c'}` – Eugene Sh. May 07 '21 at 14:02
  • @EugeneSh. I just realized I forgot to say that I have not counted the null termination and I was editing my answers when you commented this. I'm just surprised at how fast people comment. – Shambhav May 07 '21 at 14:04
  • Will happily revert my vote once fixed. – Eugene Sh. May 07 '21 at 14:05
  • You cannot do `printf("%c\n", *c)` with `char c`. – MikeCAT May 07 '21 at 14:07
  • @EugeneSh. Am I missing anything else, I really think I am given how many mistakes I already have. – Shambhav May 07 '21 at 14:14
  • @MikeCAT I just assumed everyone would just use logic to figure out that `c` would be a pointer. Made that clear as well. – Shambhav May 07 '21 at 14:16
  • The first part still contains wrong claims which I pointed to. You have added second part which is correct, but it is conflicting with what you said in the first one. Better fix the first part. I will revert my vote note trusting it will be fixed – Eugene Sh. May 07 '21 at 14:17
  • @EugeneSh. Now, have I messed up anything? – Shambhav May 07 '21 at 14:33
0

To assign a single character value to a char object, use single quotes:

char c = 'c';

Double quotes signify a string, which is a sequence of characters including a zero-valued terminator - the string "c" is actually the sequence {'c', 0} and its type is "2-element array of char", which in this instance "decays" to type "pointer to char".

Handy mnemonic - single characters use single quotes.

EDIT

Actually, looking back at this, I've oversimplified to the point of it being wrong.

'c' is an example of a character constant, which can be one more more characters delimited by single quotes. The type of a character constant in C is int, not char.

Some character constants use escape sequences to represent non-printable characters, such as

'\n' - newline
'\t' - tab
'\b' - backspace

You can also have escape sequences with octal or hex digits:

'\033` - ESC using octal digits
'\x1b' - ESC using hex digits

Some implementations allow multibyte character constants like 'ABCD' - the value and meaning of those constants is implementation-defined.

John Bode
  • 119,563
  • 19
  • 122
  • 198