Connecting Tech Pros Worldwide Forums | Help | Site Map

Examine the following!!

Newbie
 
Join Date: Nov 2009
Posts: 2
#1: 3 Weeks Ago
#include <stdio.h>
#include <string.h>
int main()
{
printf("%s\n", strerror(2));
return 0;
/*indicates successful termination */
}/*end main*/

What is the above demonsrating?

A, a function takes an error number and creates an error message string
B,A function takes a string as an argument and returns the number of characters in the string.
C,A function copies the value of the byte and includes it as an error in the printf.
D,None above

I got this question wrong and I like to corrrect myself plz help.

Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#2: 3 Weeks Ago

re: Examine the following!!


Quote:

Originally Posted by gasteven View Post

I got this question wrong and I like to corrrect myself plz help.

What [wrong] answer did you give before?
Newbie
 
Join Date: Nov 2009
Posts: 2
#3: 3 Weeks Ago

re: Examine the following!!


I put A and it was wrong! I like to know the right answer.
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 385
#4: 3 Weeks Ago

re: Examine the following!!


The correct answer still is A). It demonstrates usage of strerror() that "takes an error number and creates an error message string" which you can check in any C reference. ( just google for strerror). No part of this code "returns the number of characters in the string" or "function copies the value of the byte and includes it as an error" ( e.g. because int as a strerror's argument is not a byte)
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#5: 3 Weeks Ago

re: Examine the following!!


Quote:

Originally Posted by gasteven View Post

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     printf("%s\n", strerror(2));
  6.     return 0;          /*indicates successful termination */
  7. } /*end main*/
What is the above demonstrating?

Quote:

Originally Posted by gasteven View Post

I put A and it was wrong! I like to know the right answer.

I assume the point of the question is to describe what main does.

You need to familiarize yourself with standard library function strerror. Your program creates the error message string corresponding to error number 2; and then prints that error message string to stdout.

Consider answer A: a function takes an error number and creates an error message string. This is pretty close. Your program is hard coded for error number 2. Your program prints the error message string after creating it.

Consider answer B: a function takes a string as an argument and returns the number of characters in the string. This can't be right because your function doesn't have any arguments.

Consider answer C: a function copies the value of the byte and includes it as an error in the printf. This can't be right because the error number value ("2") is not present in the printed text.

Consider answer D: none of the above. This is a possibility. It depends on whether the instructor considers the exceptions noted above for answer A to be significant.
Reply