char a = "a";
The problem is that "a"
is a C string, a pointer to null-terminated array of char
. This means that you end up assigning some part of an address rather than the ordinal value of a character.
You need to assign a char
like this:
char a = 'a';
If you compiled with warnings enabled then the compiler would have told you about the mistake. For instance, my GCC compiler, with -Wall
says:
main.c: In function 'main':
main.c:3:14: warning: initialization makes integer from pointer without a cast [enabled by default]
char a = "a"; // "a" ascii code is 97
^
You must also never write:
printf(buffer);
If buffer
contains any format strings, then this will lead to printf
attempting to read parameters that you did not supply.
Instead write
printf("%s", buffer);