-1

The following code prints (compiled with clang)

Output

[A][?][?]

Code

#include <stdio.h>

int main(){
    char a = "A";
    printf("[A]");
    printf("[%c]", a);
    printf("[%c]", "A");
}

Gives a warning (clang does)

test.c:4:10: warning: incompatible pointer to integer conversion initializing
      'char' with an expression of type 'char [2]' [-Wint-conversion]
    char a = "A";
         ^   ~~~
test.c:7:20: warning: format specifies type 'int' but the argument has type
      'char *' [-Wformat]
    printf("[%c]", "A");
             ~~    ^~~
             %s

However

Output

[A][z][z]Characters: a A 

Code

int main(){
    char a = "A";
    printf("[A]");
    printf("[%c]", a);
    printf("[%c]", "A");
    printf ("Characters: %c %c \n", 'a', 65);

}

I'm thinking it has something to do with the memory and integers (both because of the warning and because the "A" that is rendered as a "?" goes to a "z", that is "A"--).

Zimm3r
  • 3,369
  • 5
  • 35
  • 53
  • 1
    turn on your compiler warnings, and read a beginners' tutorial about characters and strings in C. – The Paramagnetic Croissant Feb 26 '15 at 16:32
  • The compiler's message perfectly explains what is the problem with your code. I don't know how to explain it any better than this. – 5gon12eder Feb 26 '15 at 16:35
  • 1
    @5gon12eder I disagree is is completely in obvious (and stupid) to assume a differentiation between '' and "" when it comes to most languages that don't differentiate and I have never seen any mention of '' and "" being different from any tutorials or classes. – Zimm3r Feb 26 '15 at 16:43

1 Answers1

4

That's because "A" is a const char[2] string of 'A' and '\0'. Use:

char a = 'A';

to get what you appear to be after.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54