I hope this isn't a stupid question, but say I want to create a char* in C called "Hello World", using only a double. I want to do this by abusing type confusion, so that in one file I might have something like this:
char test[12];
int main() {
printf("%s\n", test);
}
and in the other file I'd have this:
double test = some_random_double;
So my issue is therefore in picking a double that converts into the Ascii string "Hello World".
Are there any suggestions? I thought I would go backwards and convert the ascii string into hex or binary, and then try converting said hex/binary into a float format, but I can't figure out how to do so.
You would need an array of 2 doubles for this purpose.
You can determine what values with this code:
#include <stdio.h>
#include <string.h>
int main(void) {
char msg[sizeof(double) * 2] = "Hello World";
double d[2];
memcpy(d, msg, sizeof(d));
printf("double test[2] = { %.20lg, %.20lg };\n", d[0], d[1]);
return 0;
}
Executing it on my laptop produces:
double test[2] = { 2.1914441197069634153e+228, 3.2516248670450380385e-317 };
Related
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
I have problems with Atof function. I am trying to convert string to float but it is not giving any error when I try in Coocox software in Debug section, Output is not showing anything. I tried two functions Atoi and Atof. When I use Atoi there is no output.When I use Atof The program starting restart. I put stdlib.h definition for atof in here.But it is atoff for float value in here.I tried same code in Dev C++ in C it is working very well. Other things I use without working Atof but this time again the program is restarting. This is working on Dev C. But not in Coocox. How can I solve the problem? There is only difference atoff! What can it be related? I used stdlib.h and there is no error in compilation!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
float c;
int b;
char *array[1] = {"52.43525"};
b=(atoi(array[0])/100);
c=((atof(array[0]))/100.0)*100.0;
c/=60;
c+=b;
printf ("%f\n", c);
return 0;
}
-----stdlib.h----
double _EXFUN(atof,(const char *__nptr));
#if __MISC_VISIBLE
float _EXFUN(atoff,(const char *__nptr));
#endif
int _EXFUN(atoi,(const char *__nptr));
int _EXFUN(_atoi_r,(struct _reent *, const char *__nptr));
long _EXFUN(atol,(const char *__nptr));
long _EXFUN(_atol_r,(struct _reent *, const char *__nptr));
------------------------------------
after correcting all the compiler warnings, this was the resulting code:
Note: since the array feature was not used, I changed it to a simple pointer. This made no difference int the output.
#include <stdio.h> // printf()
//#include <string.h> -- contents not used
#include <stdlib.h> // atoi(), atof()
int main ()
{
float c;
int b;
char *array = {"52.43525"};
b = atoi(array);
c = ( (float)( atof(array) ) / 100.0f ) * 100.0f;
c /= 60.0f;
c += (float)b;
printf ("%f\n", c);
return 0;
}
running the program resulted in:
52.873920
So if your compiler is not finding atof() it is a problem with the compiler.
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.
I'm trying to printf an integer that was passed by command line but the console print a long random values.
I put this into RUN "C:\Users\pc\Documents\Visual Studio 2013\Projects\Lab3\Debug\Lab3.exe randomString 4"
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]){
printf("%s\n", argv[0]); // Working
printf("%s\n", (argv[1])); // working
printf("%d\n", (int)(argv[2])); // NOT Working
printf("%d\n", argc); // Working
system("pause");
return 0;
}
You can't just "cast" a char* pointer to an int and expect it to be magically converted. That is just printing the address of the first element of the array. You need to convert it to an int with a runtime function such as atoi(argv[2]) See function details here
The argv[2] variable is a string pointer (char* to be precise). So casting it to int will just give you the numerical value of this pointer (or part of it, depending on the size of the addresses on your system). And this is exactly your "random long number" that you are seeing. In order to convert the string to a number you can use functions like atoi.
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>