Solved

write a main function

Posted on 2006-10-21
5
198 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
  • 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 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now