0

I found out a difference between char a = 'hi' and char a = "hi" while printf. From my experience in python, it works the same.

#include <stdio.h>

int main(void)
{
    char a = 'h';
    printf("%c",a);
    return 0;
}   

it prints a character properly.

but below "h" does not print well.. Why?

#include <stdio.h>

int main(void)
{
    char a = "h";
    printf("%c",a);
    return 0;
}   
JGPARK
  • 57
  • 6
  • 2
    What does your compiler tell about that code? If it does not show a warning, you need to increase warning level. For GCC use `-Wall -Wextra -pedantic`. `"h"` is a string literal which decays to a pointer to its first character. You assign a pointer (32 or 64 bits) to a `char` which only holds 8 bits. The chances to get some printable character from this are low. – Gerhardh May 01 '22 at 11:13
  • 2
    *I found out a difference* Your main question should be why you assumed they might be the same... – Gerhardh May 01 '22 at 11:14

0 Answers0