I was just trying to animate some strings before terminating the application. I called attached piece of code to do so. but it keeps on printing garbage till core dumped.
Here is that code..
int terminate_msg(int quit_flag)
{
char server_msg[] = "\nApplication Not Alive";
char exit_msg[] = "\nTerminating Application...\n";
char *ch;
int i = 0;
if(quit_flag == 1) {
printf("%s%s", server_msg, exit_msg);
while((ch = server_msg + i) != NULL) {
printf("%c",*ch);
fflush(stdout);
usleep(100000);
i++;
}
i = 0;
sleep(2);
while((ch = exit_msg + i) != NULL) {
printf("%c",*ch);
fflush(stdout);
usleep(100000);
i++;
}
sleep(2);
}
return 0;
}
Output:
Application Not Alive
Terminating Application...
Application Not Alive
Terminating Application...
���І�JS�dX��lX���p�dX��X��$���P
l�UZ��D~�]P�up��IS�@q�P�q�M�dX��І@��!p�\X��^C
Though first printf()
prints correctly, don't know why garbage prints are coming at the end. If there is any problem, garbage should come after first while loop also.
Note that, when comparing *ch != '\0'
, everything is working fine.
What is wrong with this piece of code? I'm using gcc
in linux environment.