Variables and types code snippet

Submitted by:Jhon Jhon

Date added:20 August, 2011

Category:C++

Source code for C++ program, declaring a number of variables of various types.

Tags: variables in c++

Code Snippet:

#include <iostream>

using namespace std;

int main() {
// int: integer type
// in modern 32-bit C++ compilers can store
// numbers in range: -2147483648..2147483647
cout << "--- int ---" << endl;
cout << "Size of int (bytes): " << sizeof(int) << endl;
cout << "Minimal allowed value: " << INT_MIN << endl;
cout << "Maximal allowed value: " << INT_MAX << endl;
cout << endl;

// int variable
int intVar = 5;
cout << "intVar: " << intVar << endl;
// do some math
intVar *= 2;
intVar += 3;
cout << "intVar (5 * 2 + 3): " << intVar << endl;
cout << endl;

// char: character type
// range: -128..127
cout << "--- char ---" << endl;
cout << "Size of char (bytes): " << sizeof(char) << endl;
cout << "Minimal allowed value: " << CHAR_MIN << endl;
cout << "Maximal allowed value: " << CHAR_MAX << endl;
cout << endl;

// char variable
char charVar = 'A';
cout << "charVar: '" << charVar << "'" << endl;
// do some math
charVar += 'Z' - 'A';
cout << "charVar ('A' + 'Z' - 'A'): '" << charVar << "'" << endl;
cout << endl;

// double: floating-point type
// range: floating-point numbers with double precision
cout << "--- double ---" << endl;
cout << "Size of double (bytes): " << sizeof(double) << endl;
cout << endl;

// double variable (PI)
double doubleVar = 3.141592653;
cout.precision(10);
cout << "doubleVar (PI): " << doubleVar << endl;

return 0;
}
 
 

Comments