Need to confirm.
Does float has 8 bytes in a 32 bit machine?
That means:
char - 1 byte
int - 4 bytes
float - 8 bytes
double - 16 bytes
Am I correct?
Thank you.
Need to confirm.
Does float has 8 bytes in a 32 bit machine?
That means:
char - 1 byte
int - 4 bytes
float - 8 bytes
double - 16 bytes
Am I correct?
Thank you.
Just use sizeof( int ), sizeof( float ), etc.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
have to throw this in here
#include <limits.h>
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
How to use this function header? I never use b4 but see b4?Originally posted by chrismiceli
have to throw this in here
#include <limits.h>
same as any other header.
It allows you to use things like INT_MAX and UINT_MAX directly.
#include <stdio.h>
int main(void)
{
float b;
b = 65.0;
printf("The value of b is %d\n", b);
return 0;
}
OUTPUT: The value of b is 0.
Why the output is not 65 with no decimal place?
Pls help.
Thanks
1. use %f (not %d)
Some compilers will warn you of such format mis-matches
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Yup, I know. I want to know why using %d will cause value to be 0 and not 65?Originally posted by Salem
1. use %f (not %d)
Some compilers will warn you of such format mis-matches
What's the difference b/w exit(1) and exit(0)? When do we use them?
Use exit( 0 ) if the program is exiting normally, without any problems, otherwise, you can use exit( 1 ) if there was a problem.
When you pass %d to printf, it attempts to read the variable as an integer. But when you pass a float to it, integers and floats are stored differently in memory, so it won't output the proper value.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
I thought using %d the compiler will convert 65.0 to 65 which is an integer right? But why the value turn out to be 0? I still dun understand?Originally posted by XSquared
Use exit( 0 ) if the program is exiting normally, without any problems, otherwise, you can use exit( 1 ) if there was a problem.
When you pass %d to printf, it attempts to read the variable as an integer. But when you pass a float to it, integers and floats are stored differently in memory, so it won't output the proper value.
Let's say if I use exit(1) if program exit normally while using exit(0) if there was a problem. What would happen? Will the program still function normally or abnormally? How abnormal?
> I thought using %d the compiler will convert 65.0 to 65 which is an integer right?
Wrong.
Any function which expects a variable number of arguments (like printf) cannot perform any data type conversions on the variable arguments, since there is no parameter type information available.
The "%d" in printf() is part of the "language" of printf, not part of the language of C.
There's nothing to stop you writing
which uses "!d" to format a decimal integer.Code:void my_print ( const char *fmt, ... );
> But why the value turn out to be 0?
Luck?
As XSquared explained, the data formats are different, so what you get is anyones guess.
> Will the program still function normally or abnormally? How abnormal?
The program will be fine - however, anything trying to use the exit status will be in a sorry state.
You should really use EXIT_SUCCESS and EXIT_FAILURE (in stdlib.h) for maximum portability
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
It's undefined behaviour. Your compiler could do pretty much anything, and there's no guarantee that any other compiler will do the same thing. The argument type should match the format specifier.Originally posted by jnwk888hwq
I thought using %d the compiler will convert 65.0 to 65 which is an integer right? But why the value turn out to be 0? I still dun understand?
Like others have said, this is undefined behaviour, what you need to do is this:
printf("The value of b is %d\n", (int)b);
Which will give you the result you require.Code:
Originally posted by jnwk888hwq
#include <stdio.h>
int main(void)
{
float b;
b = 65.0;
printf("The value of b is %d\n", b);
return 0;
}
OUTPUT: The value of b is 0.
Why the output is not 65 with no decimal place?
Pls help.
Thanks