Reputation: 2857
I have written this code:
#include "stdio.h"
static int static_int;
static char static_char;
static float static_float;
static char *static_pointer;
static double static_double;
static int static_end;
int main()
{
printf("static int =%d\nstatic char = %c\nstatic float = %f\nstatic pointer =0x%x\nstatic doub le =%f\n",
static_int,static_char,static_float,static_pointer,static_double);
printf("\n\n");
printf("static int =0x%x\nstatic char =0x%x\nstatic float =0x%x\nstatic pointer =0x%x\nstatic_doub le =0x%x\nstatic end =0x%x\n",
&static_int,&static_char,&static_float,&static_pointer,&static_double,&static_end);
return 0;
}
And I get this result:
static int =0
static char =
static float = 0.000000
static pointer =0x0
static double =0.000000
static int =0x804a030
static char =0x804a034
static float =0x804a038
static pointer =0x804a03c
static_double =0x804a040
static end =0x804a048
I am confused.
First, why does a char hold 4 byte memory (should it only take one?)?
And why does a float only take 4 byte memory? I think it will transform to double automatically. And a double takes 8 bytes.
PS: I use SUSE and GCC.
Upvotes: 2
Views: 1198
Reputation: 113
I am too confused by your question. So, I am answering assuming that you really meant byte when you meant bit in the above questions.
First.Why char hold 4 bit memory(should it only take one?)?
Nothing in the result shows that char is holding a 4 bytes in the memory. What you actually printed are the addresses at which the variables are stored. Not the sizes. The compiler assigning a 4-byte word to for char doesn't necessarily mean that char is occupying 4 bytes. Char only occupies 1 byte of the assigned 4 byte word and the remaining 3 bytes are padded with zeroes. If you are talking about the char pointer, then as you are aware, the pointer stores the address of the char variable it is pointing to. And this address is a unsigned integer which is 4 bytes in size.
To get the size of the variables use sizeof operator
And . Why float only take 4 bit memory? I think it will transform to double auto ! Ant double take 8 bit!
The size of Float is 4-bytes (on 32-bit systems). So the compiler assigned 4 bytes. The size of double is always twice that of float. So the compiler assigned a 8 byte address space to store the value. I don't understand why you think that float will be transformed to double. The program doesn't require any such auto conversion.
The compiler assigns addresses in 4-byte words because the computer accesses memory one word at a time for efficiency. The general rule is that this 'access length' (which is 4 bytes in this case) must be at least the size of the lowest size primitive data type (which is char - 1 byte length)
Upvotes: 0
Reputation: 1350
Upvotes: 2
Reputation:
Why char hold 4 bit memory(should it only take one?)?
It doesn't. char
always is exactly one byte wide, as specified by the C standard. (What makes you think it's 4 bytes long?)
And . Why float only take 4 bit memory?
I think you mean 4 bytes? No conforming C implementation has 4-bit types (the smallest type is char
which is at least 8 bits wide). Again, on most modern systems, float
is a 32-bit (and 4-byte, assuming an 8-bit byte) single precision IEEE-754 floating-point number, and double
is a 64-bit (8-byte) double-precision IEEE-754 floating-point number.
So float
is typically 4 bytes long - and the size of a float
object doesn't change, even if it's implicitly converted to a double
when passed to a variadic function. In C, function arguments are passed by value, so conversion to double
essentially means that a copy of type double
of your float
variable is made (and passed to the function).
(But anyway, where did you get the information about the sizes? I don't see you using the sizeof
operator anywhere...)
Upvotes: 2