Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-03-2010
Member
 
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;
}
Reply With Quote
  #2  
Old 04-03-2010
Member
 
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.
Reply With Quote
  #3  
Old 04-03-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Reply With Quote
  #4  
Old 04-03-2010
Member
 
Join Date: May 2008
Posts: 2,383
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;
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 04-03-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
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
Reply With Quote
  #6  
Old 04-03-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
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;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Return keyword does not give value of variable "ast""
Thread Thread Starter Forum Replies Last Post
Which java enum should I give to the variable "followers"? KADRI Software Development 4 20-02-2010 06:24 PM
Can't get DOS "for" loop to return full filenames with spaces hatred Windows XP Support 3 02-06-2009 11:09 AM
The keyword "static", attributes and methods of class in OOP DotNetUser Software Development 3 05-01-2009 11:37 AM
display "path" system variable in cmd.exe John A Grandy Windows Server Help 1 12-12-2008 12:26 PM
Uninstall "Microsoft Update" and return to "Windows Update" jabarnut Windows Update 14 13-07-2005 08:28 AM


All times are GMT +5.5. The time now is 06:58 AM.