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
Related
I'm working on a code to create my own printf. The use of printf, sprintf, and the like are prohibited. I need to convert a double type variable to a char type variable in order to use it in a puts() function with prefixes and other things added. How do I turn a double into a string?
I found this earlier, but it only converts the memory of the variable to a string:
#include <string.h>
double a=2.132;
char arr[sizeof(a)];
memcpy(arr,&a,sizeof(a));
What can I do to make this a variable? So far, the snippet of my code where I integrated that looks like this:
double num = 10.99;
char arr[sizeof(num)];
memcpy(arr, &num, sizeof(num));
puts(arr);
//Expected output: 10.99
//Actual output:
doesn't work and will return nothing to the console. If anyone could point me in the right direction, that would be much appreciated!
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;
}
I'm very new to C. I want to construct a string using 3 integers and "/".
Eg.
int a=01;
int b=10;
int c=2012;
char date = "a/b/c";
Can you please help and let me know what is the correct way to do this.
Thanks in advance
You should allocate enough buffer and use sprintf().
int a=01; /* this is octal value */
int b=10;
int c=2012;
char date[40]; /* 32-bit long integer will be at most 11 digits including sign in decimal */
sprintf(date, "%d/%d/%d", a, b, c);
Try this:
#include <stdio.h>
int main()
{
int a=1;
int b=10;
int c=2012;
char date[11];
sprintf(date, "%d/%d/%d", a, b, c);
printf("%s\n", date);
sprintf(date, "%02d/%02d/%04d", a, b, c);
printf("%s\n", date);
return 0;
}
This prints the date in two formats. The second zero-pads while the first does not. Here's the output:
1/10/2012
01/10/2012
Use sprintf, which will write to a string, as the name suggests: string print function:
sprintf(date, "%d/%d/%d", a, b, c);
and include the header stdio.h.
Also, doing
char date;
makes date a character, but you want it to be a string. So allocate memory in it:
char date [10];
which makes it a string or an array of characters with 10 elements. But you will be able to store only 9 characters in it, as you have to keep one element for the null-terminator or \0.
How does sprintf work?
If you're confused what sprintf is doing, basically the first argument is where sprintf is printing, the second argument is what to print, and the third, fourth, etc.. arguments are variables which will be substituted by a %d, %s, etc.
For a better explanation, see this:
The C library function sprintf () is used to store formatted data as a string. You can also say the sprintf () function is used to create strings as output using formatted data. The syntax of the sprintf () function is as follows:
int sprintf (char *string, const char *form, … );
You can also use itoa, but it is not standard.
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>
#include<stdio.h>
#include<string.h>
struct s {
char ch[20];
float a;
};
int main()
{
struct s p[10];
int i;
for(i=0;i<10;i++)
{
scanf("%s%f",p[i].ch,p[i].a);
}
}
What is wrong with this code?
Its giving runtime error.
What's the problem?
Most of the errors come from this line.
scanf("%s%f",p[i].ch,p[i].a);
You should use the address of p[i].a, and also restrict the numbers of chars to write in p[i].ch.
scanf( "%19s%f", p[i].ch, &p[i].a );
I haven't touched C code for a while but shouldn't it be something like
scanf("%s%f",p[i].ch,&(p[i].a));
(You have to give the memory address of the variables to the scanf function.)
At the line:
scanf("%s%f", p[i].ch, p[i].a);
You are using p[i].a as a float* (pointer), while it's a float. You're invoking undefined behavior. You probably wanted to do it like this:
scanf("%s%f", p[i].ch, &p[i].a);
Change your code like this:
#include <stdio.h>
#include <string.h>
struct s {
char ch[20];
float a;
};
int main(){
struct s p[10];
int i;
for(i=0;i<10;i++){
scanf("%s%f",p[i].ch, &p[i].a);
}
}
Note that variable a is a float type; you need to pass its memory address when using scanf.
I think the problem is in the p[i].a parameter;
use &p[i].a instead.