• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

write a main function

hi,

I want to write a main() function to use the atof function. this what I did so far. please help me re write the main() function.



  #include <stdio.h>
  #include <stdlib.h>
  #include <ctype.h>

  double atof(char s[]);

 main()
   {
      double a;
      char s[3];
     
      s[0]="a";
      s[1]="b";
      s[2]="c";
     
      a= atof(s);
      printf("%d", a);      
   }



   /* atof:  convert string s to double */
   double atof(char s[])
   {
       double val, power;
       int i, sign;

       for (i = 0; isspace(s[i]); i++)  /* skip white space */
           ;
       sign = (s[i] == '-') ? -1 : 1;
       if (s[i] == '+' || s[i] == '-')
           i++;
       for (val = 0.0; isdigit(s[i]); i++)
           val = 10.0 * val + (s[i] - '0');
       if (s[i] == '.')
           i++;
       for (power = 1.0; isdigit(s[i]); i++) {
           val = 10.0 * val + (s[i] - '0');
           power *= 10;
       }
       return sign * val / power;
   }
0
rmtogether
Asked:
rmtogether
  • 3
  • 2
1 Solution
 
sunnycoderCommented:
Hi rmtogether,

1. Print doubles using %lf flag
 printf("%lf", a);  

2. atof is defined in C library
just #include stdlib.h

3. strtod would be much more robust that atof ... depending on your system, you may also have strtof and strtold avaiable to you

Cheers!
sunnycoder
0
 
rmtogetherAuthor Commented:

this is for my exam study. I need fully understuand the atof funciton. So I wnat to write main function to test atof. could you please help me this?
0
 
sunnycoderCommented:
1.
      s[0]="a";
      s[1]="b";
      s[2]="c";

should have been

      s[0]='a';
      s[1]='b';
      s[2]='c';

"a" is same as string a\0 while on LHS you have a single character!!! Using single quotes gives you a character, using double quotes makes it a NULL terminated string.

2.
>      char s[3];
You are using three indexes and a string needs to be \0 terminated ... So infact you need 4. Also, always intitalize the array or explicitly add a string terminator so that values outside your array are not seen by your function

      char s[4] = { 0 };

3. As I said,
printf("%lf", a);

4. Currently you would get 0 as output because you are passing "abc" for conversion
0
 
rmtogetherAuthor Commented:
hi, sunnycoder

i am very glad you are here!!

could you please tell me what isdigit() use for and
what is the meaning of val = 10.0 * val + (s[i] - '0');
0
 
sunnycoderCommented:
isdigit()  checks if the character argument you supplied it is a digit (character in the range of 0 to 9).

>val = 10.0 * val + (s[i] - '0');

s[i] - '0' gets the numerical value of your digit ... e.g. if your digit was 7 ... As part of string, it would have been character '7' .. Note that 7 is not the same as '7' ... '7' is a character with ASCII value 55. Internally stored as integer 55!!!

To get value 7 from character 7, smiply subtract character '0' ... This would give you the "distance" of current character from '0'. Since current characer is a digit, we will get the integer value for that digit as a result of that subtraction

Rest is self explanatory ... simply multiply previous value of val by 10 and add current digit ... Simple place value number system (units, tens, hundreds ... each increasing place get multipled by 10)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now