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(‘$’) = ?
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(‘$’) = ?
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(‘$’));
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.
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.
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.