Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

errors in C



Doug,
There are 2 functions that will give you the error text:
1. perror: void perror(const char *s);
This will print whatever you pass in as the argument followed by the
text value of the errno. 
2. strerror. You must include strings.h
char *strerror(int errnum);
In Linux this is not reentrant, so it you are doing threads, use the _r
function. Note that some Unix/Linux functions return the error value
rather than set errno. Specifically POSIX threads. 
  fprintf (stderr, "test: %f\nerror with number: %i:%s\n", test, 
	errno,
	strerror(errno));
Also, I would not print out the value of test. The reason is that in
most of the math library, the double return value is set to HUGE_VAL on
failure, and errno is set appropriately. If no error is raised, then the
functions return the appropriate result and do not change the value of
errno.  The atof() function is only a wrapper around the strtod(), and
can also return HUGE_VAL. In any case, if test contains an
unrepresentable value, it could cause fprintf to crash. 
Here are the changes I would make to your code:
1. No need to set errno to 0. You don't care.
2. Include <string.h>
3.  replace:
   test = atof (argv[1]);
fprintf (stderr, "test: %f\nerror with number: %i\n", test, errno); 

with:
	test = strtod(argv[1], NULL);
	if (test == HUGE_VAL) {
		fprintf (stderr, "Error %d with %s: %s\n",
		errno, argv[1], strerror(errno)); 
		/* take corrective action */
	} else {
		fprintf (stderr, "test: %f\n", test);
	} 
Testing for a specific error might look like this:
switch(errno) {
	case EDOM:
		/* actions you take for a domain error */
	break
	case ERANGE:
		/* Actions you take for a range error */
	break
	... other cases
	default: /* the default case */
} /* end of switch */

In many cases, not checking for errors is not very bad, but in case,
espespecially with floats and doubles, you can cause your program to
crash. 


-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
 	
	

On Wed, 29 Jan 2003 11:47:42 -0500
"Doug Sweetser" <doug at theworld.com> wrote:

> Hello:
> 
> I am trying to "do the right thing", like checking for errors.  I
> don't use C very much, using Perl much more often.  Still, sometimes I
> like to compile things.
> 
> I read the documentaiton on atof which is suppose to take an argument
> and turn it into a floating point number.  Should it fail, it should
> generate a non-zero errno.  Here is my C code to just take in one
> argument and print it to the screen with the errno:
> 
> 
> /*      error.c
> 
> Task:	try to understand input errors.
> 
> Compiled with: gcc -Wall -o error error.c
> */
> 
> /* Includes */
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> 
> int main (int argc, char *argv[])
> {
>   /* Variables */
>   double test;
> 
>   /* Set this global variable to zero. */
>   errno = 0;
> 
>   /* Get 1 argument from the command line */
>   test = atof (argv[1]);
> 
>   /* Print results. */
>   fprintf (stderr, "test: %f\nerror with number: %i\n", test, errno); 
>    
> 
>   return 0;
> }
> 
> 
> > error 4
> test: 4.000000
> error with number: 0
> > error rtw
> test: 0.000000
> error with number: 0
> 
> 
> In stdlib.h, it reads
> /* Convert a string to a floating-point number.  */
> extern double atof (__const char *__nptr) __THROW __attribute_pure__;
> 
> 
> I often don't get what this means or how to figure out what it means.
> I read that errno was suppose to be set differently.  Any advice on
> how to do this right?
> 
> doug
> _______________________________________________
> Discuss mailing list
> Discuss at blu.org
> http://www.blu.org/mailman/listinfo/discuss
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.blu.org/pipermail/discuss/attachments/20030129/9cc5adc4/attachment.sig>



BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org