Code should be written in C Create a library. The library should contain two fun
ID: 3818130 • Letter: C
Question
Code should be written in C
Create a library. The library should contain two functions, Hello() and World(). Calling the Hello() function should print out the string Hello: calling the World() function should print out the string World. The library should use a flag (implemented using a static global variable) that keeps track of which string was last printed. A call to the Hello() function should print out Hello only if World was the last string printed, and vice versa. The flag should be updated at the end of each function call. Each function should return 1 if the string was printed, 0 otherwise. Write a simple program that uses the library and tests for the correct working of the functions.Explanation / Answer
//library..
#ifndef _MYLIBRARY_H_
#define _MYLIBRARY_H_
int flag=0;//flag variable
//function which prints Hello,when previously World is printed
int Hello()
{
if(flag==0 || flag==1)//if previous word print is World
{
printf("Hello ");
flag=2;//setting flag..
return 1;//returning 1 if printed
}
return 0;//returning 0 if not printed
}
//function which prints World,when previously Hello is printed
int World()
{
if(flag==0 || flag==2)//if previous word print is Hello
{
printf("World ");
flag=1;//setting flag
return 1;//returning 1 if printed
}
return 0;//returning 0 if not printed
}
#endif
//testing c file
//testing program
#include<stdio.h>
#include<MYLIBRARY.h>
int main()
{
//testing library function
printf("%d ",Hello());
printf("%d ",World());
printf("%d ",Hello());
printf("%d ",World());
printf("%d ",World());
printf("%d ",Hello());
printf("%d ",Hello());
printf("%d ",World());
printf("%d ",Hello());
printf("%d ",World());
return 0;
}
output:-
Hello
1
World
1
Hello
1
World
1
0
Hello
1
0
World
1
Hello
1
World
1
Process exited with return value 2
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.