6 perverted C-language Hello World Programs-better understanding of C (4, 5, 6)

Source: Internet
Author: User

Hello4.c

[CPP]View Plaincopyprint?
  1. Main () {int i,n[]={((1<<1) << (1<<1) << (1<<
  2. 1) << (1<< (1>>1)) + ((1<<1) << (1<<1))), ((1
  3. <<1) << (1<<1) << (1<<1) << (1<<1))-((1<<1) << (
  4. 1<<1) << (1<<1) + ((1<<1) << (1<< (1>>1))) + (1
  5. << (1>>1)), ((1<<1) << (1<<1) << (1<<1) << (1
  6. <<1)-((1<<1) << (1<<1) << (1<< (1>>1)))-((1
  7. <<1) << (1<< (1>>1))), ((1<<1) << (1<<1) << (1
  8. <<1) << (1<<1))-((1<<1) << (1<<1) << (1<< (1>>1
  9. ))-((1<<1) << (1<< (1>>1))), ((1<<1) << (1
  10. <<1) << (1<<1) << (1<<1))-((1<<1) << (1<<1) << (
  11. 1<< (1>>1))-(1<< (1>>1))), ((1<<1) << (1<<1
  12. ) << (1<<1) + ((1<<1) << (1<<1) << (1<< (1>>1)))
  13. -((1<<1) << (1<< (1>>1))), ((1<<1) << (1<<1)
  14. << (1<<1), ((1<<1) << (1<<1) << (1<<1) << (1<<
  15. 1))-((1<<1) << (1<<1))-(1<< (1>>1))), (((1<<
  16. 1) << (1<<1) << (1<<1) << (1<<1))-((1<<1) << (1
  17. <<1) << (1<< (1>>1))-(1<< (1>>1))), ((((1<<1
  18. ) << (1<<1) << (1<<1) << (1<<1))-((1<<1) << (1
  19. <<1) << (1<< (1>>1)) + (1<<1)), (((1<<1) << (
  20. 1<<1) << (1<<1) << (1<<1))-((1<<1) << (1<<1)
  21. << (1<< (1>>1)))-((1<<1) << (1<< (1>>1))),
  22. ((1<<1) << (1<<1) << (1<<1) << (1<<1))-((1
  23. <<1) << (1<<1) << (1<<1) + ((1<<1) << (1<< (1>>
  24. 1))), (((1<<1) << (1<<1) << (1<<1)) + (1<< (1
  25. >>1)), (((1<<1) << (1<<1)) + ((1<<1) << (1<< (
  26. 1>>1)) + (1<< (1>>1))}; for (i= (1>>1);
  27. < ((1<<1) << (1<<1) + ((1 <<1) << (1<< (1>>1
  28. ))) + (1<<1));  i++) printf ("%c", N[i]);}

This code looks relatively long and chaotic, in fact, the main concern is the C language bit operation in the displacement operation:

<<--left SHIFT, >>--right. Generally, the left side of the delegate is multiplied by 2, the left shift 2 is multiplied by 4, the right shift is equivalent to 2, the right Shift 2 represents the other than 4;

The above code, mainly through the displacement, in turn, the integer array int n[] assignment, respectively, corresponding to the "Hello, world!" ASCII code value for each character in the This can be verified by self-calculation. Then format the output with%c to convert the integer type to the corresponding character in the ASCII code.

Bit logic operations are also involved in the C-bit operation:

1. Single-Mesh operator: ~ (inverse)

2. Binocular Operation:& (Bitwise AND), | (bitwise OR), ^ (bitwise XOR)


Hello5.c

[CPP]View Plaincopyprint?
  1. #include <stdio.h>   
  2. #define __ (a) goto A;   
  3. #define ___ (a) Putchar (a);   
  4. #define _ (a) ___ (a) __ (b);   
  5. Main ()
  6. {_:__ (t) a:_ (' R ', g) b:_ (' $ ', p)
  7. C:_ ('l ', f) d:_ (' A ',s) e:_ (' a ', s)
  8. F:_ (' o ', Q) g:_ ('l ', h) h:_ (' d ', N)
  9. I:_ (' e ', W) j:_ ('e ',x) k:_ (' \ n ', z)
  10. L:_ ('H ', L) m:_ ('X ', i) n:_ ('! ' ) , K)
  11. O:_ (' z ', Q) p:_ ('Q ',b) q:_ (', ', D)
  12. R:_ (' i ', L) s:_ ('w ',v) t:_ (' H ',j)
  13. U:_ (' A ', a) v:_ ('o ',a) w:_ (') ', K)
  14. X:_ (' l ', c) y:_ (' \ t ', g) z:___ (0x0)}

This code looks neat, but the syntax is rather strange. Take a closer look, and you'll find that there are two main things involved: the use of a goto statement and the use of a macro definition.

All colons to the left are statement labels. As the first sentence in the Mian function translates to _:goto t; After executing this statement, jump to the statement labeled T, T: (' H ', j), complete all the macro substitutions, and the code translates to:

[CPP]View Plaincopyprint?
  1. Main ()
  2. {
  3. _:goto t;
  4. A:putchar (' R '); Goto  G;
  5. B:putchar (' $ '); Goto  p;
  6. C:putchar (' l '); Goto  F;
  7. D:putchar ("); Goto  s;
  8. E:putchar (' a '); Goto  s;
  9. F:putchar (' o '); Goto  Q;
  10. G:putchar (' l '); Goto  h;
  11. H:putchar (' d '); Goto  N;
  12. I:putchar (' e '); Goto  W;
  13. J:putchar (' e '); Goto  x;
  14. K:putchar (' \ n '); Goto  Z;
  15. L:putchar (' H '); Goto  l;
  16. M:putchar (' X '); Goto  i;
  17. N:putchar ('! ' ) );   goto k;
  18. O:putchar (' z '); Goto  Q;
  19. P:putchar (' q '); Goto  b;
  20. Q:putchar (', '); Goto  D;
  21. R:putchar (' i '); Goto  l;
  22. S:putchar (' W '); Goto  v;
  23. T:putchar (' H '); Goto  J;
  24. U:putchar (' a '); Goto  A;
  25. V:putchar (' o '); Goto   A;
  26. W:putchar (') ');   goto k;
  27. X:putchar (' l '); Goto  C;
  28. Y:putchar (' \ t '); Goto  G;
  29. Z:putchar (0x0);
  30. }
The order of jumps between statements is:

_ >>t>>j>>x>>c>>f>>q>>d>>s>>v>>a>>g>>h >>n>>k>>z


Hello6.cpp (Note that this program can only be compiled by the C + + compiler)

#include <stdio.h> #define _ (_) Putchar (_); int main (void) {int i = 0;_ (++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ +++++++++++++++++++++++++++ + ++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ ++++i) _ (++++++++++++++++++++ +++++++++++++++++++++ +++++++ ++++++++++i) _ (++++++++++++++ i) _ (--++i) _ (++++++i) _ (------------------------------------------------- -------------------------------------------------------------------------------------i) _ (----------------- -------i) _ (++++++++ ++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ + + +    +++++++++++++++++++++++++ ++++++++++++++++++++++++++++ ++++++++++++++++++++++++++i) _ (----------------i) _ (++++++ i) _ (------------i) _ (----------------i) _ (------------------------------------------------------------------- -------------------------------------------------------------------i) _ (----------------------------------------------i) return i;} 
This code looks pretty cool, in fact, is very boring, through the self-increment, the decrement operator is I assignment, and these values are "Hello, world!" The ASCII value of the corresponding character in the string, output with Putchar (c).

written here, six perverted C-language Hello World programs are all finished. At one glance it does not seem to make sense, and in the case I do not specify, do you eng what the program is going to output? I think it's a little difficult. Of course, we do not advocate the C language confusion code, write some people can not touch the mind courtesy of the code, but through the analysis of these chaotic code, understand the reason, through analysis, layer to solve the confusion code after the mysterious veil, I think this should also be a learning C language of fun. It's a really cool thing to write this messy code!

6 perverted C-language Hello World Programs-better understanding of C (4, 5, 6)

Related Article

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.