|
-
July 9th, 2012, 04:45 PM
#1
Declaring the size of int, double, floats
Code:
#include <iostream>
using namespace std;
int main()
{
int i;
float x, y, z;
double d;
i = 15;
d = 40;
x = 25;
y = 125;
z = y/x;
return 0;
}
Question to all .. I need to declare the size of my int, float(s), and the double listed above. How would I got about doing so?
For instance, my string: string str ("str");
I did it like: cout << "The size of my str is: " << str.size() << " characters." << endl;
Please help!
Last edited by Marc G; July 12th, 2012 at 01:56 AM.
Reason: Added code tags
-
July 9th, 2012, 05:58 PM
#2
Re: Declaring the size of int, double, floats
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "The size of int is " << sizeof(int) << endl;
cout << "The size of double is " << sizeof(double) << endl;
cout << "The size of float is " << sizeof(float) << endl;
}
You cannot change the sizes of these types -- the sizes are set at compile time. The sizeof() is a compile-time command that returns the size in bytes of these types.
Regards,
Paul McKenzie
-
July 12th, 2012, 01:57 AM
#3
Re: Declaring the size of int, double, floats
Why do you want to change the size of these types?
As Paul said, you can't, they are defined by the compiler.
Tell us your exact problem. There is most likely another solution to your problem.
-
July 12th, 2012, 03:53 PM
#4
Re: Declaring the size of int, double, floats
persianmess,
I may be wrong but I think you want to know the string::length aka string::size of each of this. You may have to cast them all to strings but a method should be something like string mystring = "THE"; mystring.length; to cast or change an into to a string to count you'll do a int number = 1; mystring = (string)number; or maybe (char) you'd have to check. This sounds like homework covered in chapter 2 maybe three of your C++ book. You don't have to read everything (though i recommend it) just read the chart that should be there containing the methods available and what they do.
-
July 12th, 2012, 08:45 PM
#5
Re: Declaring the size of int, double, floats
Yes, it is easier for the system to handle the currently defined sizes of each type than others you may be interested in trying to re-implement. People don't tend to define an ASCII character of size 6 or 7 instead of 8 bits.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|