Write a C code as described below I am trying to print out a statement \"Good Mo
ID: 3869726 • Letter: W
Question
Write a C code as described below I am trying to print out a statement "Good Morning!" from function 1 (func1) and "Good Bye!" from function 2 (func2) in the program. However, due to the infinite loop that I made by mistake in the program, func1, I can not see the second statement"Good Bye!". The interrupt handler that I added to deal with the signal (control + to kill the foreground process stops the infinite loop but couldn't see "Good Bye!". The program and the output after executing the program are as follows. Program #include #include "tlpi, hdr.h" void intHandler)- printf("This is an interrupt handler n"); exit(EXIT_SUCCESS); void func1O printf( Good Morning! "); while(1) infinite loop made by mistake */ printf(" Looping ...n"); sleep(1);Explanation / Answer
You have a very simple mistake throughout the program.
You are using while loop and you are using it as while(1) so it's always true and won't come out of the loop.
So you can remove while loop and use your func1() as given below -
void func1()
{
int n=10;//you can give any value based on how many time you want your loop in running status
printf("Good Morning! ");
while(n>10)//loop termination condition
{
printf("Looping ");
n=n-1; //reduce n by 1 for further loop
//this loop is going to be executed 10 times
}
/*loop is ended*/
sleep(1);//it will wait for one seconds then comes out from the function
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.