03 November 2011

Type range in c++ by example

//
// main.cpp
// type_ranges
//
// Created by Ahmad Ibrahim on 11/3/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include <iomanip>
using namespace std;
void wrong_calc();
void correct_calc();
int main (int argc, const char * argv[])
{
wrong_calc();
correct_calc();
return 0;
}
void wrong_calc()
{
int num = INT_MAX;
num = num * 10 / 10;
cout <<"Wrong Value " << setw(20) << num << endl;
}
void correct_calc()
{
int num = INT_MAX;
num = static_cast<double>(num) * 10 / 10;
cout<< "Correct Value" << setw(20) << num << endl;
}
view raw gistfile1.cpp hosted with ❤ by GitHub
Output:
 Wrong Value -1
Correct Value 2147483647

No comments: