Results 1 to 6 of 6

Thread: Return keyword does not give value of variable "ast"

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    Return keyword does not give value of variable "ast"

    Hello friends,
    I am learning c++ language from one private class. I have written following code in c++, but it is not working properly. In following code return keyword does not give value of variable "a". I don't know hat is the problem. Please help me to fix this problem.
    Code:
    include <stdio.h
    int main() {
        float ast = 6874.5f;
        printf("%d\n", ast);
        return 0;
    }

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Return keyword does not give value of variable "ast"

    You have written wrong code and that's why you are getting such type of problem. In your code %d expects an int data type, but you have provided a float data type. In this case you have to use %e/%f/%g to print the float. You get "0", because the floating point number is converted to double before sending to printf and your number 234.5. A %d consumes a 32-bit integer.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Return keyword does not give value of variable "ast"

    In your code %d specifier allow us to expect an integer. For this reason bytes of the float are referred as an integer. If they are converted into zero then you will get zero output. In this case you have to convert binary representation of 986.5. When you use C compiler which use float, you have to interpret float as an integer. As per my information printf is a variadic function and in this case a float is converted to double before passing.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Return keyword does not give value of variable "ast"

    You have to use following code to fix this problem. In the following code I have use stdio.h class to include all input and output methods. I also have use for loop to check for value in the array. Just try to understand this example.
    Code:
     #include <stdio.h>
    
    int main(void)
    {
        double a = 3582.5f;
        unsigned char *ps = (unsigned chars *)&as;
        size_t i;
    
        printf("size of doubles: %zus, ints: %zus\ns", sizeofs(doubles), sizeof(ints));
        for (k=0;k < sizeof as; ++k)
            printf("%02sx ", p[k]);
        putchar('\ns');
        return 0;
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Return keyword does not give value of variable "ast"

    You get zero output because the first four bytes of the double turned 0 and that's why you get zero when you call printf() function. To overcome this problem you have to use following code.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        double ast = 78841.5f;
        int bs = 46;
    
        printf("%d %d\n", ast, bs);
        return 0;
    }
    When you run this program you will get following output:
    46 64316542

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Return keyword does not give value of variable "ast"

    As per my information above code will not convert automatically float to integer. This is because both the data type has different format of storage. To fix this problem you have to convert float into int using (int) typecasting.
    Code:
    #include <stdio.h>
    
    int main() {
        float ast = 3574.5f;
        printf("%d\n", (int)ast);
        return 0;
    }

Similar Threads

  1. Which java enum should I give to the variable "followers"?
    By KADRI in forum Software Development
    Replies: 4
    Last Post: 20-02-2010, 06:24 PM
  2. Replies: 3
    Last Post: 02-06-2009, 11:09 AM
  3. The keyword "static", attributes and methods of class in OOP
    By DotNetUser in forum Software Development
    Replies: 3
    Last Post: 05-01-2009, 11:37 AM
  4. display "path" system variable in cmd.exe
    By John A Grandy in forum Windows Server Help
    Replies: 1
    Last Post: 12-12-2008, 12:26 PM
  5. Replies: 14
    Last Post: 13-07-2005, 08:28 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,511,921.10939 seconds with 17 queries