Stroustrup Chapter 4, Exercise 3 - displaying the sizes of char, int etc.

Discussion in 'C++' started by arnuld, Mar 5, 2007.

  1. arnuld

    arnuld Guest

    this programme runs without any trouble. it took 45 minutes of typing.
    i posted it here so that people can save their precious time:


    // Stroustrup special edition
    // chapter 4 exercise 2

    // printing the sizes of different types


    #include <iostream>

    int main() {


    bool b;

    char c;
    signed char sc;
    unsigned char uc;
    wchar_t wc;

    int i;
    unsigned int ui;
    short s;
    unsigned short us;
    long l;
    unsigned long ul;

    float f;
    double d;
    long double ld;


    bool* bp;

    char* cp;
    signed char* scp;
    unsigned char* ucp;

    int* ip;
    unsigned int* uip;
    short* sp;
    unsigned short* usp;
    long* lp;
    unsigned long* ulp;

    float* fp;
    double* dp;
    long double* ldp;

    std::cout << "bool: \t" << sizeof(b)
    << "\n"

    << "char: \t" << sizeof(c) << "\n"
    << "signed char: \t" << sizeof(sc) << "\n"
    << "unsigned char: \t" << sizeof(uc) << "\n"
    << "wchar_t: \t" << sizeof(wc) << "\n"

    << "int: \t" << sizeof(i) << "\n"
    << "unsigned int: \t" << sizeof(ui) << "\n"
    << "short: \t" << sizeof(s) << "\n"
    << "unsigned short: \t" << sizeof(us) << "\n"
    << "long: \t" << sizeof(l) << "\n"
    << "unsigned long: \t" << sizeof(ul) << "\n"

    << "float: \t" << sizeof(f) << "\n"
    << "double: \t" << sizeof(d) << "\n"
    << "long double: \t" << sizeof(ld) << "\n"

    << "bool*: \t" << sizeof(bp) <<
    "\n"
    << "char*: \t" << sizeof(cp) <<
    "\n"
    << "unsigned char*: \t" << sizeof(ucp) <<
    "\n"

    << "int*: \t" << sizeof(ip) << "\n"
    << "signed char*: \t" << sizeof(scp) << "\n"
    << "unsigned int*: \t" << sizeof(uip) << "\n"
    << "short*: \t" << sizeof(sp) << "\n"
    << "unsigned short*: \t" << sizeof(usp) << "\n"
    << "long*: \t" << sizeof(lp) << "\n"
    << "unsigned long*: \t" << sizeof(ulp) << "\n"

    << "float*: \t" << sizeof(fp) << "\n"
    << "double* \t" << sizeof(dp) << "\n"
    << "long double*: \t " << sizeof(ldp) << "\n"

    << "\n";
    }

    // created with Emacs, the true text editor
     
    arnuld, Mar 5, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  2. arnuld
    Replies:
    5
    Views:
    344
    Alf P. Steinbach
    Nov 8, 2006
  3. arnuld
    Replies:
    0
    Views:
    412
    arnuld
    Mar 5, 2007
  4. arnuld
    Replies:
    4
    Views:
    525
    mlimber
    Mar 5, 2007
  5. arnuld
    Replies:
    13
    Views:
    632
Loading...

Share This Page