Hello, Sathish! > > Can you explain why the following program prints the output twice? > > I read that atexit() is one of the way to determine whether > the program terminates normally or abnormally. And it will > be calling its registered functions in reverse order. > Overriding atexit() is not a good idea. In fact, you should never override any of the library functions, unless you know what are pros-and-cons of doing so. Your assumption about process termination is true. If the function registered by atexit() is called, that means the process is about to terminate normally. An ideal way of using atexit() is shown at the end of this mail. Best, Vijay Zanvar > Code > > sum1(); > main( ) > { > int a=5, b=1; > printf("Value from main %d\n", a); > } > > sum1() > { > int a=3; > return 5; > } > atexit() > { > printf("Value from sum %d\n", sum1()); > } > > > Output > > Value from sum 5 > Value from sum 5 > Value from main 5 > > > Normally atexit() will be called when main returns or when we > say exit(). > > In the above program when atexit() will be called? > > I cant understand why the output prints twice? > #include <stdio.h> #include <stdlib.h> void sum1() { int a=3; printf("Value from sum %d\n", a); } #if 0 // Never override atexit() int atexit() { printf("Value from sum %d\n", sum1()); return 0; } #endif int main() { int a=5, b=1; atexit(sum1); printf("Value from main %d\n", a); return 0; } /* Output: Value from main 5 Value from sum 3 */ > > Thanks & Regards > Sathish Kumar > subject = Survey Results > REMOTE_HOST: 216.119.215.193 >