C / C ++ Title - basic data types

Title: a float variable assignment is 3.1, why printf output value of 3.0999999? (VS environment at the output of 3.1000000)

【answer】

The computer is used to represent a binary floating-point and integer. In decimal, the 0.1 is a simple, accurate decimal, but binary representation is converted decimals .0001100110011 .... Therefore, the 3.2 can be accurately expressed in decimal, but not in binary. But when the binary to decimal, the value will be on the inconsistency.

topic:

double a,b;
//…
if(a==b)
{//是否相等。}

【answer】

not equal.

solve:

#include<math.h>

if((fabs(a-b)<=epsilon*fabs(a)))

Topic: The following code, what is the output?

bool b=-7;
BOOL B=-7;
cout<<b<<B<<endl;
b=2;
B=2;
if(b==true)
cout<<"满足b==true条件"<<endl;
if(B==TRUE)
cout<<"满足B==true条件"<<endl;

【answer】:

1 -7

Meet b == true condition

Press any key to continue...

【analysis】

the difference:

(1) bool has two values ​​true (1) or false (0). BOOL also has two values ​​TRUE (1) and FALSE (0).

(2) bool only 1 byte, and space BOOL determined by a compiler, and typically four bytes.

(3) bool is a Boolean type, while BOOL is defined in the header file WinDef.h is an integer.

(4) bool value is non-zero (positive or negative integer and floating point including), the result is 1, i.e. true. BOOL type is either the value is TRUE, the or is FALSE. If a certain value is not these two values, the value is not set up under the conditions of TRUE and FALSE.

Topic: What is the actual data for single-precision floating-point -21.375 stored in memory is? Please write a program, when a single-precision floating-point input, print out the actual floating point stored in memory.

【answer】

 

Title: embedded programming, often see u8, u16, u32 and u64 of data types, meaning they represent what I ask? Why you wrote?

【answer】

In the Linux system is generally in the x86 architecture, u8, u16, u32 and u64 of the data type defined as follows:

typedef unsigned char u8;

typedef unsigned short u16;

typedef unsigned int u32;

typedef unsigned long long u64;

For the following reasons:

(1) writing code is more convenient, with a brief character replaces the lengthy character.

(2) increase portability of code, such as their corresponding data types may be different in different platforms. For example, u64, defined in some platform

typedef unsigned long u64;

Title: Integer known (4 bytes) in the contents of memory, printing the actual value of the integer, use C program.

【prompt】

Consider the size endian. For example, the content stored from low to high address in memory is 12 34 ab cd (hexadecimal number), if a little endian CPU, then it corresponds to an integer value: 0xcdab3214; if the CPU is big endian, then it corresponds to an integer value: 0x1234abcd.

[A]

The test questions portability problem size endian. , C programs often perform in embedded development in a number of different CPU architecture, so the size of the problem in different endian CPU is necessary to consider the matter, because otherwise the program will have a major platform compatibility issues and risks. Typically, the data content may be obtained in the memory debugging embedded development process, these data can verify the correctness of the program, so there will be such a case, such contents have been printed out of an integer value from the content, but because endian CPU uncertain, it can not directly determine the correct value of the result, then you can write a function to achieve the correct parse integer value. Reference codes are as follows.

Title: The following code output is how much?

int main()
{
         unsigned char a=-1;
         signed char b=-1;
         char c=-1;
         printf("a=%d,b=%d,c=%d",a,b,c);
         return 0;
}

A: a = 255, b = -1, c = -1

[A]

signed char the range -127--127, unsigned char in the range 0--255, as to char the range depending on the compiler and compile-time options used.

Under VS environment, the same type of field char signed char.

 

Guess you like

Origin blog.csdn.net/chen1083376511/article/details/92001366