[C ++ primer plus] [Chapter 3 processes data] -- 1. Simple Variables

Source: Internet
Author: User

C ++ primer plus

Chapter 3 processes data


1 simple variable

1.1 integer limit

// limits.cpp -- some integer limits#include <iostream>#include <climits>int main (void){  using namespace std;  int n_int = INT_MAX;  short n_short = SHRT_MAX;  long n_long = LONG_MAX;  // output integer limits  cout << "INT_MAX = " << INT_MAX << endl;  cout << "SHRT_MAX = " << SHRT_MAX << endl;  cout << "LONG_MAX = " << LONG_MAX << endl;  cout << "int is " << sizeof (int) << " bytes\n";  cout << "short is " << sizeof (n_short) << " bytes\n";  cout << "long is " << sizeof n_long << " bytes\n";  //  cout << "long is " << sizeof long << " bytes\n";  //  limits.cpp:22: error: expected primary-expression before ‘long’  return 0;}

1.1.1 climits

Climits files store the maximum and minimum limits of various types. Find this file and view

#pragma GCC system_header#include <limits.h>#ifndef _GLIBCXX_CLIMITS#define _GLIBCXX_CLIMITS 1#ifndef LLONG_MIN#define LLONG_MIN (-__LONG_LONG_MAX__ - 1)#endif#ifndef LLONG_MAX#define LLONG_MAX __LONG_LONG_MAX__#endif#ifndef ULLONG_MAX#define ULLONG_MAX (__LONG_LONG_MAX__ * 2ULL + 1)#endif

Without the int_max definition, continue to search in limits. h file and find such definitions.

/* Minimum and maximum values a `signed int' can hold.  */#  define INT_MIN(-INT_MAX - 1)#  define INT_MAX2147483647



1.1.2 sizeof

Sizeof (long), sizeof (n_long), sizeof n_long can be expressed in all three ways, but sizeof long compilation fails, as shown in limits. cpp.


1.1.3 long

The long definition is introduced in C ++. The restriction description of limits. H is

/* Minimum and maximum values a `signed long long int' can hold.  */#   define LLONG_MAX9223372036854775807LL#   define LLONG_MIN(-LLONG_MAX - 1LL)/* Maximum value an `unsigned long long int' can hold.  (Minimum is 0.)  */#   define ULLONG_MAX18446744073709551615ULL


1.2 represents and outputs other hexadecimal Integers

// hexoct.cpp -- show hex and octal constants#include <iostream>int main (void){  using namespace std;  int a = 42;  int b = 042;  int c = 0x42;  cout << "a = " << a << endl;  cout << "b = " << b << endl;  cout << "c = " << c << endl;    cout << "After cout << oct" << endl;  cout << oct;  cout << "b = " << b << endl;  cout << "After cout << hex" << endl;  cout << hex;  cout << "c = " << c << endl;  cout << "a = " << a << endl;    return 0;}  

1.2.1 indicates octal and hexadecimal

042 indicates 42 of octal

0x42 indicates the hexadecimal 42


1.2.2 output octal and hexadecimal

Cout <Oct;

The output integers are all octal integers.

Cout

The following output integers are all hexadecimal, so the output result of the above Code is:

a = 42b = 34c = 66After cout << octb = 42After cout << hexc = 42a = 2a

1.3 determine the constant type

Why is 123 stored in code cout <123 <Endl?

First look at the suffix, such as 123l will be stored as long

If there is no suffix,

In decimal format, the minimum type that can be stored in int, long, and unsigned long is used.

For octal or hexadecimal values, the minimum types can be stored in int, unsigned int, long, and unsigned long.


1.4 general characters

For some special characters, C ++ is represented in a similar way as escape characters, for example, \ u00e2. â

Cout <"g \ u00e2teau ";

Output: gâ teau


1.5 wchar_t

When the character set to be processed by the program cannot be represented by an 8-bit byte, wchar_t and wcout are used for processing. The L prefix can be used to indicate the wide character set and string.

// testWchar_t.cpp -- test wchar_t and wcout#include <iostream>int main (void){  using namespace std;  wchar_t a = L'p';  wcout << L"tall " << a << endl;    return 0;}


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.