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 | Linux Links | Bling | About BLU

BLU Discuss list archive


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

errors in C



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




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