I wrote this very small program that works fine until I remove the comments for the fprintf function.
Basically the program will prompt the user for a numeric value, when the user guesses right(1234) the program exits. When I remove the comments the program never exits...Does anyone have any idea why? I'm completely lost as to why it would behave like this...maybe its my compiler - gcc 4.4.1
#include <stdio.h>
#include <stdlib.h>
unsigned long testval = 1234;
void* foundit(void)
{
fputs("found it!\n", stdout);
return (void*)0;
}
void* tryit(void)
{
unsigned long val;
fputs("enter a value/guess->", stdout);
fscanf(stdin, "%u", &val);
if (val == testval)
{
return (void*)&foundit;
}
else
{
return (void*)&tryit;
}
}
int main(int argc, char**argv)
{
void *addr = (void*)&tryit;
//fprintf(stdout, "ans->%u\n", 1); //remove comments and program runs
//but fails to exit with correct value
while ((addr = ((void*(*)(void))addr)()))
{}
exit(EXIT_SUCCESS);
}