CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2012
    Posts
    4

    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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Jul 2012
    Location
    Ohio
    Posts
    13

    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.

  5. #5
    Join Date
    Apr 2012
    Posts
    29

    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
  •  





Click Here to Expand Forum to Full Width

Featured