?
Solved

write a main function

Posted on 2006-10-21
5
Medium Priority
?
206 Views
Last Modified: 2012-05-05
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
Comment
Question by:rmtogether
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17782371
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
 

Author Comment

by:rmtogether
ID: 17782394

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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17782422
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
 

Author Comment

by:rmtogether
ID: 17782454
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 17782464
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question