Reputation: 420
I want to print
"Size of (type) is (size of type) Byte"
for example
"Size of int is 4Byte"
by the code below.But it says that
"Size of int is 8Byte"
I think this code showed the size of string(int) but the type int.
How can I solve this problem?
code
#include <stdio.h>
char *variabletype[] = {"char", "unsigned char", "signed char", "int", "unsigned int", "short", "unsigned short", "long", "unsigned long", "long long", "unsigned long long"};
int main() {
for (int i = 0; i < 11;++i) {
printf("Size of %s is %u\n",variabletype[i], (unsigned int)(sizeof(variabletype[i])));
}
return 0;
}
return
Size of char is 8Byte
Size of unsigned char is 8Byte
Size of signed char is 8Byte
Size of int is 8Byte
Size of unsigned int is 8Byte
Size of short is 8Byte
Size of unsigned short is 8Byte
Size of long is 8Byte
Size of unsigned long is 8Byte
Size of long long is 8Byte
Size of unsigned long long is 8Byte
Upvotes: 4
Views: 2850
Reputation: 1
sizeof
is an operator computed at compile time. It returns a compile time constant (except for VLAs). In your code, you are always printing sizeof(char*)
(which happens to be 8 on your and on my computers). Remember that an array is not the same as a pointer, even if arrays decay into pointers.
You want:
printf ("sizeof int is %zu\n", sizeof(int));
printf ("sizeof long is %zu\n", sizeof(long));
and you might use preprocessor tricks (stringizing with #
)
#define PRINTSIZE(Type) printf("sizeof " #Type " is %zu\n", sizeof(Type))
PRINTSIZE(int);
PRINTSIZE(long);
The C language separates translation-time and run-time. Practically speaking, you are using a compiler (and sometimes even a cross-compiler). There is no eval operator in standard C.
On many operating systems, you can run another compilation process (perhaps using system or popen(3)), but how to run the compiler is system specific (on Linux, you'll often use gcc
; on Windows, it probably is something else).
So in principle you could, on Linux, write a program which generates a temporary C file then uses system(3) on some computed string twice: once to run the compilation, another time to run the produced executable. But that is complex (and not portable), so is not worth your time.
Notice that, in C (and C++), types, statements, expressions, variables names, etc... are only known to the compiler and don't exist at runtime (when running you only have memory locations). The C11 standard (read n1570) mentions translation units.
Maybe you dream of some homoiconic programming language. Then don't use C, but languages like Common Lisp. Try SBCL, that Common Lisp implementation is transforming (i.e. compiling) at each REPL interaction your expression into machine code.
Upvotes: 6
Reputation:
I tried your code and found out that if you are using a 64-bit system it shows 8 byte and on a 32-bit system it will show 4 Byte
If you use Visual studios just change the debug settings(i think?) from x64 to x86(32-bit) that should give you your result.
NOTE: i am a really bloody beginner on C and thought this could help... although i don't really know why it is double the size on a 64-bit system^^".
Upvotes: 0