"C language" a line of printf code--{a[0]? 0[a]}

Source: Internet
Author: User

This is a previous time to do http://fun.coolshell.cn/in a question, very interesting, involved in fact is the basis of C, but the first time I saw this line of code really confused me:

printf (&unix["\021%six\012\0"], (Unix) ["has""  Fun"0x60);

At that time on the Internet a search, there is a guy gave a full sentence explanation: http://blog.itpub.net/12443821/viewspace-671745/

Here, I use my understanding to explain again, at least more in line with my understanding of the idea ~ I am the first time to see this writing, C language predecessors please skip ~

This line of code is parsed into the following sections:

What do you mean, \021 \012?

\ABC means the ASCII code represented by octal, so \021 is 17 corresponding ASCII code (2^8+1=17), \012 is 10,\0 is 0, so the code is about equal to the following representation, The ASCII 17 character is a bit strange, instead of using @:

printf (&unix["@%six\n"], (Unix) ["has""  fun"0x60);

Second, what is UNIX

This is really not easy to know, if you run this line of code on Windows, is to error, because there is no definition of UNIX, this is the legend of the compiler built-in macros, maybe the GCC built-in bar, did not check, anyway, equivalent to:

#define Unix 1

Three, (0[a] = = A[0])? True:false

Here's a look (Unix) ["have"] what is the thing, then I was confused by this, even if I know that UNIX represents 1, then 1["have" what is it?

Char " qwe " ;p rintf ("%c", b[1]);

To see what the output of these two lines is, it is obvious that the second term "w" of the output character array B is known to everyone, and it is clear to all that this is actually:

printf ("%c", * (b +1));

This principle is obvious, b[1] equivalent to * (B+1) equivalent to * (1+B), and 1[b] does not represent this address well ~

When this is all clear, the code above becomes:

Char "  have " ; Char "  Fun " ;p rintf (&unix["@%six\n"], s1[10x60);

Four, address arithmetic

s1[1]+s2-0x60, this corresponds to the previous%s, is a string, it is obvious that s1[1] is a char (here is the letter a), or a number (a ASCII code 97, or a hexadecimal representation of 0x61).

S2 is the first character address of the string "fun", 0x60 is the number, so s1[1]-0x60 first operation, get 1, so the problem becomes:

Char "  Fun " ;p rintf (&unix["@%six\n"1);

S2+1 is un, that is, the previous%s, so the world suddenly clear:

printf (&unix["@unix \ n"]);

Wu, Steady

First look at the unix["@unix \ n"], as before, said (directly deleted from the front is not very reasonable, with the variable s to declare, this is automatically added):

Char " @unix \ n " ;p rintf (&s[1]);

S[1] means that the letter U,&s[1] represents the address of u, and it becomes the output "unix\n".

So the end result is UNIX.

"C language" a line of printf code--{a[0]? 0[a]}

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.