0

I can't get my function to print characters i have initialised and declared.

#include <stdio.h>
int main() {

  char letter1 ="i";
  char letter2 ="n";
  char letter3 ="C";

  printf ("Programming %c%c %c\n", letter1, letter2, letter3);


  return 0;
}

I want it to display

     "Programming in C"

using

    printf ("Programming %c%c %c\n", letter1, letter2, letter3);

I get the following error

main.c:4:8: warning: incompatible pointer to integer conversion 
initializing 'char' with an expression of type 'char [2]' [-Wint- 
conversion]
                    char letter1 ="i";
                                             ^     

I haven't learn about pointers or anything yet, I'm used to simpler languages that just work. I am trying to go through an edX course but I find the quality to be poor and the pacing tedious.

Would be happy if you could help me out here and recommend me better resources for learning C

thanks

Mr_Bodger
  • 47
  • 3
  • 3
    `"i"` is a *string* literal, you want *character* literals, which use a single quote `'i'`: https://en.cppreference.com/w/c/language/character_constant – UnholySheep Mar 24 '19 at 20:23
  • I didn't even know there was a difference, thanks it works now <3 – Mr_Bodger Mar 24 '19 at 20:24
  • Or change `char` to `char *` and then do `printf ("Programming %c%c %c\n", *letter1, *letter2, *letter3);` (otherwise character literals use ***single-quotes*** while string literals use ***double-quotes***) – David C. Rankin Mar 24 '19 at 20:47

1 Answers1

0
 char letter1 ="i";

Unfortunately, "i" is a string. Use 'i' for characters or "i"[0] if you really want to.

If your compiler didn't give you a warning, it's either a terrible compiler or you don't have appropriate warnings turned on. If you got a warning and you ignored it ...

David Schwartz
  • 179,497
  • 17
  • 214
  • 278