Promotion


Program Listing

Promotion.cpp

/************************************************************************
*                                                                       *
*   Program:    Promotion                                               *
*                                                                       *
*   Purpose:    This program will demonstrate the effects of promotion  *
*               and demotion                                            *
*                                                                       *
************************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main() {

//  Integers

    char ch = 5;                    // Five
    short sh = 4000;                // Four thousand
    int in = 3000000;               // Three million
    long lg = 2000000000;           // Two billion
    long long ll = 1000000000000;   // One trillion
    
    cout << "Initial values" << endl << endl;
    cout << "ch = " << (int)ch << endl;
    cout << "sh = " << sh << endl;
    cout << "in = " << in << endl;
    cout << "lg = " << lg << endl;
    cout << "ll = " << ll << endl;

//  Promote char to short, short to int, int to long, long to long long

    sh = sh + ch;
    in = in + sh;
    lg = lg + in;
    ll = ll + lg;
    
    cout << endl << "After promotion" << endl << endl;
    cout << "ch = " << (int)ch << endl;
    cout << "sh = " << sh << endl;
    cout << "in = " << in << endl;
    cout << "lg = " << lg << endl;
    cout << "ll = " << ll << endl;
    
//  Demote long long to long, long to int, int to short, short to char

    lg = lg + ll;
    in = in + lg;
    sh = sh + in;
    ch = ch + sh;
        
    cout << endl << "After demotion" << endl << endl;
    cout << "ch = " << (int)ch << endl;
    cout << "sh = " << sh << endl;
    cout << "in = " << in << endl;
    cout << "lg = " << lg << endl;
    cout << "ll = " << ll << endl;

    return 0;
}

Sample Run

Initial values

ch = 5
sh = 4000
in = 3000000
lg = 2000000000
ll = 1000000000000

After promotion

ch = 5
sh = 4005
in = 3004005
lg = 2003004005
ll = 1002003004005

After demotion

ch = -39
sh = -13612
in = -1013335249
lg = -1016339254
ll = 1002003004005



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:25 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/Promotion.html

Return to Coms 261 home page