you need to write a C function named start. The prototype is: int start(int argc
ID: 3594098 • Letter: Y
Question
you need to write a C function named start. The prototype is:
int start(int argc, char **argv);
The function accepts an argc and argv that are passed to main.
The return value is as follows:
if argc<>2, return -1
if argc==2, then return the following:
if argv[1] is not an integer, return -2
if argv[1] is an integer but is not positive, return -3
otherwise argv[1] is a positive integer: return its value
You must use sscanf (as usual). Be sure to check the return value from sscanf
to determine whether or not its argument represents an integer.
Write your own main test function to test your start function.
If your main function is called main.c compile as follows:
gcc -o main main.c start.c
Explanation / Answer
float start(int argc, char **argv)
{
int a;
if( argc == 2 ) {
if(!sscanf(argv[1], "%d%*c", &a) == 1)
return -2;
if(a < 0) return -3;
else if(a >=0) return a;
}
else
{
return -1;
}
}
int main( int argc, char **argv )
{
cout << start(argc, argv);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.