Is there any difference in the way atof and strtod work? - c

I know that strtod() and atof() functions are used for conversion from string to double.
But I can't figure out the difference between these two functions.
Is there any difference between these two functions, if yes then please let me know...
Thanks in Advance.

From the man page on double atof(const char *nptr):
The atof() function converts the initial portion of the string pointed to by nptr to double. The behavior is the same as
strtod(nptr, NULL);
except that atof() does not detect errors.
Why can't it detect errors? Well, because that second argument of double strtod(const char *nptr, char **endptr) is used to point to the last character that couldn't be converted, so you can handle the situation accordingly. If the string has been successfully converted, endptr will point to \0. With atof, that's set to NULL, so there's no error handling.
An example of error handling with strtod:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *str = "1234.56";
char *err_ptr;
double d = strtod(str, &err_ptr);
if (*err_ptr == '\0')
printf("%lf\n", d);
else
puts("`str' is not a full number!");
return 0;
}

Related

Putting value of a char array into a float variable

I want to put the value of an array into a float integer.
main(){
float a;
char array[4]="12.1";
a=atoi(array);
printf("%f",a);
}
When I uses this program, it gives 12.000000 as output but I want 12.100000 as output. Thanks in advance.
Use of this :
atof() — Convert Character String to Float :
#include <stdlib.h>
double atof(const char *string);
This link explains about that.
Summarizing the answers and comments, your program should look like:
int main(void) {
float a;
char array[]="12.1";
a=atof(array);
printf("%f\n",a);
}
instead of atoi () which converts character array to integer, use atof() read here

How to convert char pointer to float in C?

I've looked around everywhere and tried pretty much everything suggested and can't get anything to work.
this is my code:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
float a;
char *nums[3];
char str[5];
printf("Please enter a,b,c:");
scanf("%s",str);
int i=0;
char *p;
p = strtok (str,",");
while (p != NULL)
{
nums[i++] = p;
p = strtok (NULL, ",");
}
a=atof(nums[0]);
printf("%s\n",nums[0]);
printf("%f\n",a);
return 0;
}
the math.h is for something later on after I figure this out. So if I entered "1,2,3" into this program, my print statements would show me "1" and then "0.000", this is obviously just there for me to test things out but why the does my value just disappear after trying to convert to a float? I need that value of 1 to do math with later in my program but I can't get that char pointer value no matter what I try, I can only seem to print it, but it screws up as soon as I try to convert it into a type I can use.
Two issues:
You nums and str arrays are too short. nums should have a size of at least 3, and str should be at least 6 ("1,2,3" plus null byte), probably more for larger numbers.
So change those to:
char *nums[3];
char str[20];
Second, you don't #include <stdlib.h>, which contains the declaration of atof. Without a declaration, it is assumed to return an int.
Fix the array sizes, and #include <stdlib.h>, and it should work.

C: Conversion from String to Double gives strange results

I am tring to convert a string into a floating-point value. Take a look at my small program:
#include <stdio.h>
int main() {
char string[3] = "42";
double value = atof(string);
printf("Floating-point value: %f\n", value);
return 0;
}
When I run it, I get this:
Floating-point value: 327680.000000
Why? The conversion from string to integer using atoi has worked very well!
If you have any idea why this is, please share your wisdom. :)
char string[2] = "42";
should be
char string[3] = "42";
the size of "42" array is 3 bytes as you have to count the trailing null character. If you want use char string[2] for the declaration, your string will not be null terminated.
Then you also have to include stdlib.h file for atof declaration:
#incude <stdlib.h>

What's the correct way to convert an argument passed to a program to a long double variable?

I'm having trouble with this...It compiles, but segfaults immediately when run...Using GDB, I determined that it segfaults while trying to initiate the long doubles. I feel like atoi might be the wrong function to use, but I tried other similar functions and still got a segfault.
int main(int argc, char *argv[]) {
long double x = atoi(argv[1]);
char oper = argv[2][0];
long double y = atoi(argv[3]);
atoi() stands for "ascii to integer", which isn't what you want. You should use an appropriate function — strtold(3) is most appropriate, but you can probably use sscanf(3), too.
Use strtold:
#include <stdlib.h>
#include <errno.h>
char * e;
errno = 0;
long double d = strtold(argv[1], &e);
if (*e != 0 || errno != 0) { /* Error! Do not consume the result. */ }
// result now in "d"
You can use the value of *e to figure out whether any part of the string was consumed; see the manual for details. My example just checks whether the entire string could be parsed.
I'm actually not sure if there's a thread-safe solution... (Oh, I think it is, because errno is thread-local.)
The correct way to convert a string to a long double is
long double strtold(const char *nptr, char **endptr);

pointers in c (beginner)

I just started to look at C, coming from a java background. I'm having a difficult time wrapping my head around pointers. In theory I feel like I get it but as soon as I try to use them or follow a program that's using them I get lost pretty quickly. I was trying to follow a string concat exercise but it wasnt working so I stripped it down to some basic pointer practice. It complies with a warning conflicting types for strcat function and when I run it, crashes completly.
Thanks for any help
#include <stdio.h>
#include <stdlib.h>
/* strcat: concatenate t to end of s; s must be big enough */
void strcat(char *string, char *attach);
int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
strcat(s,t);
}
void strcat(char *s, char *t) {
printf("%s",*s);
}
Your printf() should look like this:
printf("%s",s);
The asterisk is unnecessary. The %s format argument means that the argument should be a char*, which is what s is. Prefixing s with * does an extra invalid indirection.
You get the warning about conflicting types because strchr is a standard library routine, which should have this signature:
char * strcat ( char * destination, const char * source );
Yours has a different return type. You should probably rename yours to mystrchr or something else to avoid the conflict with the standard library (you may get linker errors if you use the same name).
Change
printf("%s",*s);
to
printf("%s",s);
The reason for this is printf is expecting a replacement for %s to be a pointer. It will dereference it internally to get the value.
Since you declared s as a char pointer (char *s), the type of s in your function will be just that, a pointer to a char. So you can just pass that pointer directly into printf.
In C, when you dereference a pointer, you get the value pointed to by the pointer. In this case, you get the first character pointed to by s. The correct usage should be:
printf( "%s", s );
BTW, strcat is a standard function that returns a pointer to a character array. Why make your own?
Replacing *s with s won't append strings yet, here is fully working code :
Pay attention to function urstrcat
#include <stdio.h>
#include <stdlib.h>
/* urstrcat: concatenate t to end of s; s must be big enough */
void urstrcat(char *string, char *attach);
int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
urstrcat(s,t);
return 0;
}
void urstrcat(char *s, char *t) {
printf("%s%s",s,t);
}
pointers are variable which points to address of a variable.
#include "stdio.h"
void main(){
int a,*b;
a=10;
b=&a;
printf("%d",b);
}
in the follwing code you will see a int 'a' and a pointer 'b'.
here b is taken as pointer of an integer and declared by giving'' before it.'' declare that 'b' is an pointer.then you will see "b=&a".this means b is taking address of integer "a" which is keeping value 10 in that particular memory and printf is printing that value.

Resources