Integer data limits checking – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#2122 siteicon   siteicon   siteicon  

Integer data limits checking

A program to check and understand integer data type data limits

Learning Objectives

  • Understanding the 4 byte integer data type maximum and minimum value.

Source Code

TC++ #2122

 

Source Code

Run Output

Integer max should be 2147483647
Integer min should be -2147483648

Attempted to print 2147483647
Actually printed :2147483647
Attempted to print -2147483648
Actually printed :-2147483648
Attempted to print 2147483648
Actually printed :-2147483648
Attempted to print -2147483649
Actually printed :2147483647

Code Understanding

int imax=2147483647; int imin=-2147483648;
These two declaration and initialisation are for correct maximum and minimum integer data limits for 32 bit (4 Bytes) integer data type. A simple int on most modern computers if of 4 byte size while on old DOS based compilers it is of 2 bytes size.

int imaxe=2147483648; int imine=-2147483649;
these declarations are for erroneous data value. Notice that last number is 1 more for the positive side of integer and 1 less for the negative side integer limits.

cout<<“Integer max should be 2147483647″<<endl;
cout<<“Integer min should be -2147483648″<<endl<<endl;
These two lines have been given to display the allowed values, so that they are available on the display screen for comparison purposes.

cout<<“Attempted to print 2147483647″<<endl;
cout<<“Actually printed :”<<imax<<endl;
cout<<“Attempted to print -2147483648″<<endl;
cout<<“Actually printed :”<<imin<<endl;
With these instructions correct values are printed as the values given are in the range.

cout<<“Attempted to print 2147483648″<<endl;
cout<<“Actually printed :”<<imaxe<<endl;
cout<<“Attempted to print -2147483649″<<endl;
cout<<“Actually printed :”<<imine<<endl;
These instructions print the incorrect results as the values are out of the range.

Notes

  • The maximum and minimum int size in terms of powers of 2 is  (2^31-1) and (2^31). This is because half the range is on positive side and half on negative side. For unsigned integer the entire positive side of values are available from 2^31-1 as the maximum value.

Common Errors

  • Since large figurative symbols are there in the program, it can go wrong with very small human mistakes. One has to be extra careful in writing figures.


Suggested Filename(s): int-data-limits.cpp



Share

sunmitra| Created: 2-Jan-2018 | Updated: 28-Aug-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back