double and int in C

Updated on technology 2024-02-09
9 answers
  1. Anonymous users2024-02-06

    #include

    int main(void)

    double d;

    printf("enter a single digit: ");

    scanf("%lf", &d);

    printf("the digit entered is %i", (signed int)d);

    return 0;

    If you add a cast to the output, the output will be the correct result, probably because the original data type is a double precision floating-point, but it is formatted as %i (a signed integer), and the result is unpredictable during the conversion.

    To put it more bluntly, the picture above is good.

    In the figure, you are using gdb to debug your **, when the input is 4, we look at the stack with this floating-point number 6 storage method (x 8bf followed by the address is the value of the memory address displayed by bytes, showing 8 bytes), the double type occupies 8 bytes, you can see that the first 6 of these 8 bytes are 0, then it is easy to understand, when you format according to %i (signed integer), whether it is signed or unsigned inter number, it will be processed according to the int type in the expression ( Automatic type escalation), that is, on a machine with a general int type of 32, the 8-byte double number will be truncated and the first 4 bytes will be taken.

    Isn't that clear? The landlord can do another experiment and you enter a floating-point number to see (e.g., how much will the result show?). Definitely not 0

    It's a very large number, because the first 4 digits of the floating-point number are not 0 and are very large, about the specific storage method of double-precision floating-point numbers in memory, as well as the internal mechanism of the prinf function, the landlord can pay attention to it.

  2. Anonymous users2024-02-05

    What does i mean?

    What kind of output format do you want?

    If you want to output floating-point type, use %lf A, use %d for shaping, and use %g for the extra 0 after the decimal point of the floating-point number

    I haven't seen it!

  3. Anonymous users2024-02-04

    What is i? Haven't heard of it.

  4. Anonymous users2024-02-03

    double is a keyword in the C language, which representsDouble-precision floating-point type, which occupies 8 bytes of memory space, the value range is ", the significant digit of double precision is fully guaranteed is 15 digits, and 16 bits is only part of the value***.

    In C, float and double are both floating-point numbers.

    The difference is that the range represented by double is larger than that of float, and the range of decimal parts is also higher than that of float.

    Case in point: pi.

    This number, if expressed in float, can only be accurate to 6 decimal places at most. The double can be accurate to about 15 decimal places. Exactly how many digits are used, depending on the compiler used.

    Yes, but there is not much difference between the compilers of each machine.

    As for the integer part, the range of the integer part represented by the float is already large enough, and it can be represented to the trillion level, which is already boundless. The range of integers represented by double is even larger. In actual development, except for a few high-precision fields, basically, it is enough to judge the use of floats.

  5. Anonymous users2024-02-02

    It represents an integer data type, which is usually 4 bytes long.

  6. Anonymous users2024-02-01

    The double type occupies 8 bytes in memory, and the int type occupies 4 bytes in memory. The value of double is greater than int. It can be understood that the double is an 8-liter kettle, and the int type is a 4-liter kettle, and the water from the large kettle cannot be poured directly into the small kettle.

    If you want the value to be successfully assigned, you need to cast the int type to the double type.

    Forced type conversion: Convert a data type with a large value range to a data type with a small value range, which is performed automatically, while the coercion needs to be performed manually.

    Conversion Format: Data Type Variable Name = Data Type) The data value to be transformed.

    Assign value to int type, modify to int i = int); The double type is converted to the int type, and the decimal point is directly removed.

    Strong note: Converting floating-point numbers to integers and removing the decimal point directly will result in a loss of precision.

    int is forcibly converted to short and chops off two bytes, which may cause data loss.

  7. Anonymous users2024-01-31

    Double is a type of data used by computers. Compared to a single-precision float, a double uses 64 bits (8 bytes) to store a floating-point number.

    It can represent 15 or 16 significant digits in the decimal system, with negative values ranging from to and positive values ranging from to .

    For example, declare a variable of type double:

    double num;

    Initialize a variable of type double:

    num = ;

  8. Anonymous users2024-01-30

    Float and double are both floating-point data types in C++, and the difference between the three is:

    1. The accuracy is different.

    The floating-point type is a single-precision floating-point number, and the double-type is a double-precision floating-point number.

    2. Allocate storage space.

    The C++ compiler allocates 4 bytes for floating-point types and 8 bytes for two-type.

    3. The number of significant bits is different.

    float can provide 6 significant digits, and double can provide 15 significant digits.

  9. Anonymous users2024-01-29

    The definition of double is floating-point.

    Double data is more accurate and takes up more space than float. Double is twice as accurate as float, so more accurate calculations are often used.

    Double-precision floating-point values can be converted to other types of integers or floating-point numbers, and vice versa. The last character of a double-precision floating-point constant can be marked with "d" or "d". The storage format is similar to that of floating-point, with the highest bit being the sign bit, the next 11 bits being exponential, and the next 52 bits being mantissa bits.

    Compared with the numerical type, it can provide higher numerical precision, using a fixed storage length of floating-point number, and each double-precision floating-point type of data occupies 8 bytes. Unlike numeric data, its decimal point position is determined by the data value entered and can only be used for fields in a table.

    Floating-point data:

    1. Float type.

    The compilation system allocates 4 bytes to each float-type variable, and the values are stored in the memory unit in the form of normalized binary number exponents.

    2. Double type.

    In order to expand the range of values that can be represented, a double-type data is stored with 8 bytes, and 15 significant digits can be obtained.

    3. Long double type.

    Different compilation systems handle the long double type differently, and Visual C++ handles the long double type in the same way as the double type, allocating 8 bytes.

Related questions
15 answers2024-02-09

ints need to be expressed according to the size of the compiler. >>>More

9 answers2024-02-09

Scope. You static char *chh;

static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More

4 answers2024-02-09

It's not very hard that the tail node next points to null. >>>More

7 answers2024-02-09

Haha, if you're just learning SQL programming, you'll be fine with any of them, but I'd recommend Oracle because it's closer to the common usage and the essence of SQL. One more sentence: What weight is not weight, and it is impossible to learn the heavyweight directly? >>>More

6 answers2024-02-09

Understand the following rules: 1) Overloading an operator does not change the priority of the operator. >>>More