Hi, nabhatt. I've written a code that partially does what I think you wanna do. The explanation for it is right after the code, shown right below:
Code:
#include <stdio.h>
int main(void)
{
/* Declaration of variable x: */
int x;
/* Data input: */
printf("Enter the value of x: ");
scanf("%d", &x);
/* [01]: */
printf("%f", (double)x);
/* 2 new lines: */
printf("\n\n");
/* [02]: */
printf("%X", x);
/* 2 new lines: */
printf("\n\n");
/* [03]: */
printf("%p", &x);
return 0;
}
The code starts with the C preprocessor directive
which is necessary in order to use the functions scanf() and printf(). The program starts (and ends) its execution in function main(), which starts with
and ends with
Right after the beginning of function main(), x is declared as an int variable:
Then, printf() asks for the user to input a value for x, which is then read by scanf(), all this happening in the following statements:
Code:
printf("Enter the value of x: ");
scanf("%d", &x);
The first printing of x is as a decimal-point number. x is an int variable, so, in order to print it as decimal number (with %f), you must first assign it to a double variable (say, for example, double y) and print y instead of x, or you can use the type cast to float or double (as is done in the following code) to print x as a number with decimal point. This is a temporary conversion - after this printf statement, x will still maintain its value acquired during the input above.
Code:
printf("%f", (double)x);
In order to accomplish the second printing, which is outputting x in the hexadecimal base, you just use %X instead of %f, and you don't do a type conversion this time:
The third printf() statement means to print the value of &x, where & is the address operator. &x is the address in memory of the variable x; in order to print an address, you can use the %p specifier:
The output is the address of variable of x in hexadecimal format.
If you still need help, please let us know.