String to int
I wrote this code to convert number string to int and compare:- #include<stdio.h> int str(char S1[],char S2[]) { int x=0,y=0,i; for(i=0;S1[i]!='\0';i++) { x*=10; int m=S1[i]; x+=m; } for(i=0;S2[i]!='\0';i++) { y*=10; int m=S2[i]; y+=m; } if(x>y) return 1; if(y<x) return -1; if(x==y) return 0; } int main() { char S1[100],S2[100]; gets(S1); gets(S2); int a = str(S1,S2); if(a==1) printf("S1 is greater than S2"); else if(a==-1) printf("S2 is greater than S1"); else printf("Both are equal"); return 0; } but x and y are storing garbage values. why is it so? please help.