|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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.
__________________ Grand Theft Auto 4 PC Video Game |
#4
| |||
| |||
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 |
#5
| |||
| |||
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; } 46 64316542 |
#6
| |||
| |||
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; } |
![]() |
|
Tags: c language, class, file handle, function, main, object |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |