Numerical limitations------C + + programming principles and Practices (advanced)

Source: Internet
Author: User

Tag: A sign Eric indicates the value with document Oat Sentinel

Each C + + implementation identifies properties of built-in types in <limits>, <climits>, <limits.h>, and <float.h>, so programmers can use these properties to check for numerical limits, Set Sentinel mechanism and so on. They are very important for developing the underlying program. If you feel the need for these attribute values, it is likely that your work is closer to the hardware. But there are other uses for these properties, for example, it's normal to be curious about the language implementation details: "How big is an int?" "," Char is signed? And so on It is difficult to find the correct answers to these questions from the system documentation, and the C + + standard does not specifically define these issues. A better approach is to write a short applet to get answers to these questions:

#include <limits>

cout<< "number of bytes in an int:" <<sizeof (int) << ' \ n ';cout<< "largest int:" <<int_max << ' \ n ';cout<< "smallest int value:" <<numeric_limits<int>::min () << ' \ n '; if (numeric_ limits<char>::is_signed) cout<< "Char is signed\n", Else cout<< "char is unsigned\n"; Char Ch=numeric_ Limits<char>::min ();cout<< "The char with the smallest positive value:" <<ch<< ' \ n ';cout< < "The int value of the char with the smallest positive value:" <<int (ch) << ' \ n ';

If you are writing a program that will be used on a variety of hardware platforms, it is valuable to get the information in the program. Another approach is to hard code this information into the program, but this is disastrous for maintenance personnel.

These property values are also useful for overflow detection.

Principles and Practice of C + + programming (Advanced article)

Numerical limitations------C + + programming principles and Practices (advanced)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.