I tried to call a self-written function without handling its return value. gcc told me at the line of the function call that this would be a statement with no effect.
To check whether my function gets called at all, I added some printf statement, but didn’t get any output from the program.
Is it possible that gcc simply ignores the function call? As far as I know, I never had any problems with such statements.
So here is the code:
unsigned strlen(char *string)
{
printf("ignored by gcc");
unsigned count = 0;
for(; *string++; count++);
return count;
}
int main()
{
char string[] = "something";
strlen(string);
return 0;
}
Thanks in advance.
It is perfectly legal to ignore the return value of a function. You probably do it more than 99% of the times you invoke
printf()(which returns anint).However, as several persons have said in the comments, you called your function
strlen()after a standard library function. This is illegal according to C99’s 7.1.3:2:Here the compiler warns unexpectedly, and either calls the standard function instead of yours, or calls no function at all (since it knows its
strlen()is supposed to be without side-effects). This is one of the things undefined behavior can do.