[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

write a main function

Posted on 2006-10-21
5
Medium Priority
?
215 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 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

Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

612 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