C Language Puzzle Record

Source: Internet
Author: User
Tags printf

After reading the C language puzzle, a lot of harvest, a further understanding of C language, from which each of the examples listed can learn many previously overlooked knowledge points.

Here are some good case histories.

What does the following program output?

#include <stdio.h>
int main ()
{
    float a = 12.5;
    printf ("%d\n", a);
    printf ("%d\n", (int) a);
    printf ("%d\n", * (int *) &a);
    return 0;
}

Reference Answer:

The output of the program is shown below,

0

12

1095237632

The reason is: the floating-point number is 4 bytes, 12.5f is converted to binary is: 01000001010010000000000000000000, hexadecimal is: 0x41480000, decimal is: 1095237632. So, the second and third outputs I'm sure you know why. And for the first one, why output 0, we need to know the memory layout of float and double, as follows:

Float:1 bit sign bit (s), 8-bit exponent (e), 23-bit mantissa (m, total 32 digits)

Double:1 bit sign bit (s), 11-bit exponent (e), 52-bit mantissa (m, total 64 digits)

And then we need to know that printf does not match the type, so it turns float directly into a double, noting that the 12.5 float and double memory binaries are completely different. Don't forget to use the reverse byte sequence under the x86 chip, the high byte and the low bit are reversed. So:

float: 0x41480000 (in memory: 00 00 48 41)

Double: 0x4029000000000000 (in memory: 00 00 00 00 00 00 29 40)

And our%d requirement is a 4 byte int, for the double memory layout, we can see the first four bytes is 00, so the output is naturally 0.

This example shows us that printf is not type-safe, and that's why C + + is cited as cout.

ATTENTION:

1, "Hello" [2] = = 2["Hello"] = ' l '

In 2,c/c++, numbers beginning with 0 are octal.

3,sizeof is not a function, is an operator, its i++ type of size, which can be in the program before the run (compile) The complete thing, so, sizeof (i++) directly by 4 to replace, in the runtime will not have i++ this expression.

The variable initialization statement in the 4,switch-case body is not executed.

The 5,printf return value is the number of characters in the output.

6,stdout and stderr are not the same device descriptors. StdOut is a piece of equipment, stderr is not. For block devices, only if the following conditions are entered, 1 encounters a carriage return, 2 buffer full, and 3 flush is invoked. and stderr is not.

This article is from the "About:blank h4cking" blog, please be sure to keep this source http://pnig0s1992.blog.51cto.com/393390/814005

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/C/

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.