Please tell me what I am doing wrong
//I'm trying to get the number of the month by sending its name.
#include <stdio.h>
My function
int monthstr2num (char month[]){
if (month == "September")
return 8;
}
int main (){
char month []={"September"};
int num;
num = monthstr2num (month);//func call
displays a wrong output like 37814040
printf ("%d", num);
return 0;
}
Your problem lies in two places.
First is where you are using
==to compare a string, that isn’t possible in C (It’s undefined behavior, it compiles but won’t do what you want). You must use a function in the C library calledstrcmp. It’s located instring.hand can be used like so:Also, when that if statement returns false, you must have another return outside the if statement, such as
return 0;