Variables and Data Types

// variables.cpp
//
// compile: g++ -Wall -o variables variables.cpp

#include <iostream>

int main()
{
  unsigned char a = 7;  // integer between 0 - 255
  const int b = 45;     // a fixed-value integer (cannot be changed)
  short c = 99;         // a two-byte integer
  float f = 3.497;      // a four-byte floating-point variable
  double d = -0.2345;   // an eight-byte floating-point variable

  // In general, don't combine variables of different types unless you
  // are clear on the possible outcomes.  The following will work but
  // may produce a compiler warning.
  long result = a + b + c;

  std::cout << "result = " << result << std::endl;

  //b = 44;      // won't work ... 'b' is a const
  //a = -4;      // unsigned variable ... but compiler might not complain
  c = 55000;     // what happens here?

  std::cout << "c now = " << c << std::endl;

  a = 0xA4;      // set 'a' to a MIDI "aftertouch" status byte on channel 4

  std::cout << "a = " << (int)a << ", channel (via masking) = " << (int)(a & 0x0F) << std::endl;

  return 0;
}

variables.cpp



McGill ©2003-2020 McGill University. All Rights Reserved.
Maintained by Gary P. Scavone.