C / C ++ data types insight

Note: Should the test did not say it was a DEV5.11 test
are their own test summary Source, skills-oriented, with reference to a number of blog
articles written just afraid to forget their own knowledge but also test; the wrong place look to correct me

Predefined data types

int : 4 bytes, a byte 8, 8 * 4 = 32; so in the range: -2 31 is --2 31 is -1 = -2,147,483,648--2,147,483,647;
char; Double; BOOL; a float; void ; pointer type;

Type modifiers :

unsigned,signed, short, long

Total (dev 5.11 Test):

{ short int (2字节) = short; 
  long int (4字节) = long = int;
  long long int (8字节) = long long;double (8字节); 
  long double(16字节);(这些都有 unsigned和 signed 的区别) };
  float (4字节);char (1字节);

long >= int >= short 1

float and double are used the number of symbols, the mantissa, exponent symbol, index four parts expressed (Example: -3.4 * 10 (-38); number operator: a first negative sign; mantissa: 3.4; symbol index: Negative numbers in brackets ; index 38);
float, Double, Double Long, the range online, but I could not test, so I am not sure, this is not in the list.

test:

#include<stdio.h>
#include <iomanip>
#include<iostream>
using namespace std;
int main()
{     
double D=1234567890.1234567890;      
long double LD=12345.1234567890098;      
float F=1.23456789;      
cout<<"D:"<<setprecision(18)<<D<<endl;      
cout<<"LD:"<<setprecision(18)<<LD<<endl;      
cout<<"F:"<<setprecision(9)<<F<<endl;
return 0;
}

/*
DEV 5.11显示:
D:1234567890.12345672
LD:12345.1234567890097
F:1.23456788

VS2015显示:
D:1234567890.12345672
LD:12345.1234567890097
F:1.23456788
*/

All the last random random number seed and it seems all the same, so the decimal precision are double: 17 Wei; long double: 17 Wei; float: 9 place;
if you do the test with the maximum number of decimal 9 only in use is the most reliable, so the test:

#include<stdio.h>
#include<time.h>
#include <iomanip>
#include<iostream>
using namespace std;
int main()
{      
double D=-9999999999.999999;      
long double LD=-99999.99999999999;      
float F=-9.999999;      
cout<<"D:"<<setprecision(18)<<D<<endl;      
cout<<"LD:"<<setprecision(18)<<LD<<endl;      
cout<<"F:"<<setprecision(9)<<F<<endl;
return 0;
}

/*
DEV 5.11显示:
D:-9999999999.99999809
LD:-99999.9999999999854
F:-9.99999905

Observed digits are accurate to: Double: 15 place; the same long double; float: 7 bit ; it is used in this range of the corresponding data type is ok.

Special is: long double-byte total memory 16; Double 8 bytes total memory; has the same accuracy.

Placeholder Summary:

	%d:int     
	%ld:long     
	%f:float    
	%lf:double
	%p:输出变量地址
	%x/%X:输出十六进制数    
	%s:输出字符串
	%o:无符号八进制整数
	%u:无符号十进制整数    
	%e/%E:用科学记数法输出浮点数

Custom Data Types

Array; structure (struct); class (class)
Commonwealth (Union) : Commonwealth has some similarities with the structure. But there are two fundamentally different. In the structure, each member has its own memory space, the total length of the structure of a variable length and are the members. In the Commonwealth, some members of the shared memory space, variable combined length equal to a length of the longest members.

It should be noted that this does not mean the so-called multiple members share the same time in an inner joint variable, but to the joint variables can be given to any member of the value, but you can only assign one kind of value.

Commonwealth stated :

   1.union perdata
   {
      int class;
      char officae[10];
   };
   perdata a,b; 
   2.union perdata
   {
   int class;
   char office[10];
   }a,b;
    3.union
    {
    int class;
    char office[10];
    }a,b

Example test:

int main()
{
       union ch{
              int A;
              char Z;
       }a,b;
       a.A='A';
       b.Z='Z';
       cout<<"a.A:"<<a.A<<endl;
       cout<<"b.Z:"<<b.Z<<endl;
       cout<<"a.Z:"<<a.Z<<endl;
       cout<<"b.A:"<<b.A<<endl;
       return 0;
}

/*
输出结果: 
a.A:65                   整型 A 
b.Z:Z                     char型 Z 
a.Z:A                     char型 A 
b.A:90                   整型 Z 
说明:赋值的时候已经确定值了,在使用同一个对象的不同的数据类型的时候,数据值不变,但还会改变数据类型。 
*/

Consortium equivalent to a variable data type.

Enumeration (enum) : C / C ++ language can be used to create a symbolic constants and const #define, enum and use the tools not only to create symbolic constants, but also to define new data types, but must be carried out according to certain rules.
example:

enum enumType {Monday, Tuesday, Wednesday, Thursday, Friday,Saturday, Sunday};

The default value is undefined amount of enumeration is incremented from 0, if the definition

enum enumType {Monday = 1, Tuesday, Wednesday = 1, Thursday, Friday,Saturday, Sunday};

则Monday = 1, Tuesday = 2, Wednesday =1, Thursday = 2, Friday = 3, Saturday = 4, Sunday = 5

test:

int main()
{
       enum enumType { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
       enumType Weekday;
       cout<<Monday<<Tuesday<< Wednesday<< Thursday<< Friday<<Saturday<< Sunday<<endl;
//     Monday = 1;         错误,不能赋值,    所以 Monday++ 也不可以了; 
       cout<<enumType(0)<<enumType(1)<<enumType(2)<<enumType(3)<<enumType(4)<<enumType(5)<<enumType(6)<<endl; 
       int a = Monday + 1;
       if (Monday == 0)
       cout<<"Monday:"<<Monday<<endl;
//     Monday = Wednesday;       错误,不能赋值; 
       cout<<"a:"<<a<<endl;
//     enum Week{Monday = 1, Tuesday, Wednesday = 1, Thursday, Friday, Saturday, Sunday};            错误,不能重复定义里面的每一元 
       return 0;
}

/*
输出结果: 
0123456
0123456
Monday: 0
a: 1
*/

  1. C standards, and it does not specify the specific implementation may vary according to the compiler ↩︎

Released three original articles · won praise 3 · Views 152

Guess you like

Origin blog.csdn.net/weixin_44223946/article/details/104181557