/*implementation of strcmp*/
#include<stdio.h>
#include<string.h>
/*length of the string*/
static const int MAX_LENGTH = 4;
/*gets string comparison's return value i.e. the difference between the first unmatched characters*/
int getStrCmpReturn(char[], char[]);
/*gets the maximum of the two integers*/
int max(int, int);
int main()
{
char string1[MAX_LENGTH];
char string2[MAX_LENGTH];
gets(string1);
gets(string2);
printf("\n%d", getStrCmpReturn(string1, string2));
return 0;
}
int getStrCmpReturn(char string1[], char string2[])
{
//int test = 50;
int strCmpReturn = 0;
int i;
for(i = 0; (i < max((int)strlen(string1), (int)strlen(string2))); i++)
{
if(string1[i] != string2[i])
{
strCmpReturn = string1[i] - string2[i];
break;
}
}
return strCmpReturn; //not required, why?
}
int max(int string1Length, int string2Length)
{
if(string1Length >= string2Length)
{
return string1Length;
}
else
{
return string2Length;
}
}
Look at the definition of the function getStrCmpReturn(). It is seen that if the the return statement is removed or commented, the function still returns the value stored in the variable strCmpReturn. Even if an extra variable is added to it, like "int test = 5;" (shown in comments), the function still returns the value stored in the variable strCmpReturn.
How is the compiler able to guess that the value in "strCmpReturn" is to be returned, and not the ones stored in other variables like "test" or "i"?