tags:

views:

285

answers:

4

I'm looking for the following information to be printed out:

sizeof(int) = ?
sizeof(float) = ?
sizeof(double) = ?
sizeof(char) = ?
sizeof (167) = ?
sizeof(3.1415926) = ?
sizeof(‘$’) = ?
A: 

If it's a console application, you could use writeline that takes a string and displays it:

Console.WriteLine(sizeof(int));
Console.WriteLine(sizeof(int));
Console.WriteLine(sizeof(float));
Console.WriteLine(sizeof(double));
Console.WriteLine(sizeof(char));
Console.WriteLine(sizeof (167));
Console.WriteLine(sizeof(3.1415926));
Console.WriteLine(sizeof(‘$’));
MarceloRamires
I'm not sure that sizeof(167) will even compile, but I could be wrong.
SwDevMan81
@Slaks - Yeah there is: http://msdn.microsoft.com/en-us/library/eahchzkf(VS.71).aspx
SwDevMan81
Only with `unsafe`. Also, it only operates on types, not values.
SLaks
@Slaks agreed and agreed :)
SwDevMan81
well, i gues it seems confusing because i did mention, it is meant to ask for the number of bytes for each value in unix system.
Also, you mean `ToString`. Finally, you don't need `\n` or `ToString`.
SLaks
here is the program that i tried to compile:#include <stdio.h>int main (void){ printf("sizeof(int): %d\n",sizeof(int)); printf("sizeof(float): %d\n",sizeof(float)); printf("sizeof(double): %d\n",sizeof(double)); printf("sizeof(char): %d\n",sizeof(char)); printf("sizeof(167): %d\n",sizeof(167)); printf("sizeof(3.1415926): %d\n",sizeof(3.1415926)); printf("sizeof('$') :%d\n",sizeof('$')); return 0; }compiling command used:gcc myweek06.C
Isn't this C#, not C? (Ah, I see, the tag's been changed.)
T.J. Crowder
+3  A: 

Sure. You can use the following code. I'm answering in C since that's what the question asked for, despite the C# tag. If you really want C#, someone else will have to help.

#include <stdio.h>
int main (void) {
    // Use %zu for size_t if your compiler supports it.
    printf("sizeof(int)       = %d\n",sizeof(int));
    printf("sizeof(float)     = %d\n",sizeof(float));
    printf("sizeof(double)    = %d\n",sizeof(double));
    printf("sizeof(char)      = %d\n",sizeof(char));
    printf("sizeof(167)       = %d\n",sizeof(167));
    printf("sizeof(3.1415926) = %d\n",sizeof(3.1415926));
    printf("sizeof('$')       = %d\n",sizeof('$'));
    return 0;
}

This outputs (on my system):

sizeof(int)       = 4
sizeof(float)     = 4
sizeof(double)    = 8
sizeof(char)      = 1
sizeof(167)       = 4
sizeof(3.1415926) = 8
sizeof('$')       = 4

Keep in mind that this gives you the values in terms of bytes which, under the C standard, are not necessarily 8 bits. You should examine CHAR_BITS from limits.h to see how many bits are in a byte.

paxdiablo
it was giving me an error when i compile it:Undefined first referenced symbol in file__gxx_personality_v0 /var/tmp//ccwLqbRS.old: fatal: Symbol referencing errors. No output written to a.outcollect2: ld returned 1 exit status
1) How are you compiling it? 2) What compiler are you using? 3) What OS are you using?
paxdiablo
i was using gcc compiler and used the exact copy from the above program.
I'm noticing that the quotes around $ are 'smart quotes', and not plain ones. Is that a typo in the original program? Because that's not going to fly at all with any compiler (except MS Word perhaps)
Adriano Varoli Piazza
You need to name the file with a .c extension. Or compile with g++ instead of gcc.
indiv
Actually it worked fine under Ubuntu 9.10, Adriano, but I've changed it anyway :-) although the smart quotes were just inside the string, not part of the zizeof. @user292489, that personality problem is usually a C/C++ mismatch. Make sure your program is called something.c and use "gcc -o something something.c" to compile it.
paxdiablo
I'm going to be pedantic here and point out that `sizeof` returns an `unsigned` type, while `%d` implies a signed integer. On a 64-bit big-endian computer, this is very likely to print 0 for all cases. Over fifteen years ago, a student of mine got that result on a Macintosh with a 32-bit `size_t` and 16-bit `int`.
David Thornley
i tried but it keeps giving me the same error
@user292489: the error you are getting is a linker error and means that you have a variable or function that is not defined in any of the object files or libraries. You should edit your question and outline the exact steps you are taking.
Lucas
@user292489: (1) What computer? (2) What OS? (3) What is your compile command? Please answer these questions before trying anything else.
paxdiablo
@user292489@: to answer paxdiablo's question precisely, just copy what the command "gcc -v" or "gcc --version" gives you
Lucas
here is the program that i tried to compile: #include <stdio.h> int main (void) { printf("sizeof(int): %d\n",sizeof(int)); printf("sizeof(float): %d\n",sizeof(float)); printf("sizeof(double): %d\n",sizeof(double)); printf("sizeof(char): %d\n",sizeof(char)); printf("sizeof(167): %d\n",sizeof(167)); printf("sizeof(3.1415926): %d\n",sizeof(3.1415926)); printf("sizeof('$') :%d\n",sizeof('$')); return 0; } compiling command used: gcc myweek06.C
@user292489: change the filename. the capitalized "C" from "myweek06.C" to "myweek06.c". And then compile with "gcc myweek06.c"
Lucas
i really apprtiate you all, it works when i try to compile using g++, instead of using gcc myweek06.c, but am still not quite sure why it did not compile it using gcc.
@user292489: that is because gcc accepts the extension ".C" only for c++. To compile c you have to use ".c"
Lucas
well, never knew that would make a difference, that worked perfetclly,another Q: how can we identify the implicit and explicit cast for the following expression in c;int a = 2, b = 3;float f = 2.5;double d = -1.2;int int_result;float real_result;
@user292489: That should be a separate StackOverflow question. :-)
Platinum Azure
well, never knew that would make a difference, that worked perfetclly, another Q: how can we identify the implicit and explicit cast for the following expression in c; 1) int a = 2, b = 3;(2) float f = 2.5; (3) double d = -1.2; (4) int int_result; (5) float real_result;
@user292489: That should be a different question. In fact, there's one like it, and it suffers from the problem that there is no such thing as an implicit cast. A cast is, by definition, explicit.
David Thornley
@paxdiablo Hi paxdiablo!your answer is right but I think there should be %ul instead of %d because the sizeof() function returns number of bytes require to store certain datatype and size in memory is unsigned so use of proper format specifier is good. Even %d is also ok.
Param-Ganak
@Param, it *should* be %zu as I note in the code comment but this is a C99 feature from memory and not available in all compilers that I'm forced to cater for. If you have a look in my comment to David Thornley's answer, not even %ul is *fully* safe since it's possible for a size_t to have more bits than an unsigned long.
paxdiablo
+1  A: 

Something like

#include <stdio.h>

int main()
{
    printf("sizeof(int) = %ul\n", (unsigned long) sizeof(int));
}

with a lot of similar lines will do. Save, compile, and run.

One common mistake is printf("sizeof(int) = %d", sizeof(int));, but this is a mismatch. The result of sizeof() is size_t, which is an unsigned integral type that's big enough to hold any possible object size. The %d specifier asks for an int, which is a signed integral type that's convenient for calculation. It's not all that rare for size_t to be bigger than int, and then you're passing arguments of a size that the function doesn't expect, and that can be bad.

David Thornley
Actually, @David, I don't think that will cut it either. The standard states "The types used for size_t and ptrdiff_t should not have an integer conversion rank greater than that of signed long int *unless the implementation supports objects large enough to make this necessary*." (my italics) - that means the size_t is allowed to be greater than an unsigned long in a conforming implementation. The %zu format pattern is the only *guaranteed* way to ensure there won't be problems.
paxdiablo
@paxdiablo: You're correct in the general case. I was oversimplifying because it actually didn't matter in the OP's case, and I didn't make that clear.
David Thornley
A: 

Hi, I am not working on C from last three years. But following is the answer of your question. You can print size of different data types as follows:

printf("Size of integer: %ul",sizeof(int));

printf("Size of float: %ul",sizeof(float));

printf("Size of double: %ul",sizeof(double));

printf("Size of char: %ul",sizeof(char));

printf("Size of 167: %ul",sizeof (167));

printf("Size of 3.1415926: %ul",sizeof(3.1415926));

printf("Size of integer: %ul",sizeof(‘$’));

Just paste above printf lines in your program. You will get the size of the datatypes.

Here sizeof() is a function which returns the total memory required to store that datatype or literal.

Param-Ganak