I have this snippet of the code
char *str = “123”;
if(str[0] == 1) printf("Hello\n");
why I can’t receive my Hello thanks in advance!
how exactly compiler does this comparison if(str[0] == 1)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You want to do this:
The difference is that you are comparing
str[0]to the number 1, while my code above is comparingstr[0]to the character'1'(which has ASCII value 49). Not all programming languages treat characters and numbers interchangeably in this way, but C does.Check out ASCII for more information about how computers map numbers to characters.