Thread: Need help in C

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Fresno, California, United States
    Posts
    6

    Need help in C

    1. write a program declaring a variable x as the type int, and use the scanf function to input an integer to initialize the value of x. Then, your program will print the following values with specified formats using the printf library function:


    • [01] x (format: decimal)
    • [02] x (format: hexadecimal)
    • [03] &x (format: hexadecimal)
    • [04] (int) (*((int *) x)) (format: decimal)
    • [05] *((double *) (&x)) (format: double)
    • [06] ((double *) (&x)) + 1 (format: hexadecimal)
    • [07] ((int *) (&x)) + 1 (format: hexadecimal)
    • [08] ((void *) (&x)) + 1 (format: hexadecimal)
    • [09] ((char *) (&x)) + 1 (format: hexadecimal)
    • [10] ((char **) (&x)) + 1 (format: hexadecimal)
    • [11] ((int **) (&x)) + 1 (format: hexadecimal)

    it suppose to crash at some point.


    Sorry but I am completely new to programming.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    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

    Code:
    #include <stdio.h>
    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

    Code:
    int main(void)
    {
    and ends with

    Code:
    return 0;
    }
    Right after the beginning of function main(), x is declared as an int variable:

    Code:
    int x;
    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:

    Code:
    printf("%X", x);
    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:

    Code:
    printf("%p", &x);
    The output is the address of variable of x in hexadecimal format.

    If you still need help, please let us know.
    Last edited by stdq; 02-03-2012 at 07:52 PM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Fresno, California, United States
    Posts
    6
    Thanks you so much

Popular pages Recent additions subscribe to a feed