I tryed to make a program to show the output "f" after executing the code. The result was the error E0144 after trying this:
#include <stdio.h>
int main()
{
char first_ch = "f";
printf("%c", first_ch );
return 0;
}
and after trying this the result was 0:
#include <stdio.h>
int main()
{
const char* first_ch = "f";
printf("%c", first_ch );
return 0;
}
Can you explain me why is showing this error at first and after that why in the second case the output is 0 and after all, what should i type to make the program work like i want? Thank you very much!
CodePudding user response:
In C, the char data type uses single quotes ' ' instead of double qoutes " ".
try running
#include <stdio.h>
int main()
{
char first_ch = 'f';
printf("%c", first_ch );
return 0;
}
CodePudding user response:
For the first situation, try using single quotes, such as 'f'. This seems to work. I think in C, there seems to be a rule such as single-quotes for single characters, and double quotes for strings. Not sure why though.
char first_ch = 'f';
For the second situation, you're going to need the library <stdlib.h>. "const char*" is going to create an array of chars via malloc(). They are initially given the value of "0". I'm not sure, but I don't think using the keyword "const" is a good idea at initialization, it results in the default value (0) being unable to be modified (as far as I know).
I'd say, the first situation is simpler and gets the job done.