6 perverted C-language Hello World Programs-Better understanding C (2, 3)

Source: Internet
Author: User

Next article, 6 perverted C language Hello World program--Better understand C, this article brings you sick Hello World program 2, 3

Hello2.c

#include <stdio.h>    Main () {      int x=0,y[14],*z=&y;* (z++) =0x48;* (z++) =y[x++]+0x1d;      * (z++) =y[x++]+0x07;* (z++) =y[x++]+0x00;* (z++) =y[x++]+0x03;      * (z++) =y[x++]-0x43;* (z++) =y[x++]-0x0c;* (z++) =y[x++]+0x57;      * (z++) =y[x++]-0x08;* (z++) =y[x++]+0x03;* (z++) =y[x++]-0x06;      * (z++) =y[x++]-0x08;* (z++) =y[x++]-0x43;* (z++) =y[x]-0x21;      x=* (--z); while (Y[x]!=null) Putchar (y[x++]);    }

This one look is more insipid. The main concern is the pointer and the ASCII code problem. Assigns the first address of an integer array to the int * Type pointer z, via * (z++) =Y[X++]+ASCII code,in turn, the elements in the array are assigned a value of ' H ', ' e ', ' l ', ' l ', ' o ', ', ', ' ', ' w ', ' O ', ' r ', ' L ', ' d ', '! ',' + '。assigns a value of "\" to "y[13" as the exit condition for the while loop.

We mainly analyze the relationship between pointers and arrays:

We have to be clear that arrays are actually different from pointers.

1) The pointer holds the address, the first to obtain the contents of the pointer, as the address from the address to obtain data, is an indirect way of asking; the array is the direct access to the data, A[i] is to take a+i as the content of the address. For the compiler, one data is the address, and one pointer is the address of the address.

2) When defining pointers, the compiler simply allocates space for the pointer itself, unless the assignment is a string constant. The string created when the pointer is initialized in ASCI C is defined as read-only. An undefined behavior occurs when you try to modify the value of a string by using a pointer.

#include <stdio.h>int main (void) {char *p= "hello";//printf ("%c\n", P[1]),//p[1]= ' F ';p rintf (", p[1] changed to%c" , P[1]); Char a[]= "World";p rintf ("%c", A[1]), a[1]= ' U ';p rintf (", a[1] changed to%c", a[1]);} If the comment is canceled, the program will be hung out. </span>
3) The array name represents a fixed piece of address in memory and cannot be used as the lvalue of an assignment expression; The pointer is assigned its own address when defined, and can be used as the lvalue of an assignment expression to point to a different area in memory.

4) When you access an array in the form A[i], it is always interpreted by the compiler as a pointer access like * (A+i).

5) In the case of function arguments, arrays are equivalent to pointers, and arrays of parameters are always modified by the compiler to a pointer to the first element of the array.

6) In other cases, definitions and declarations must match. If you define an array, you must declare the array when you declare it in other files, as well as the pointer. The declaration simply tells the compiler the name of an object created elsewhere and allocates memory for the object when it is defined.


Hello3.c

int n[]={0x48,    0x65,0x6c,0x6c,    0x6f,0x2c,0x20,    0x77,0x6f,0x72,    0x6c,0x64,0x21,    0x0a,0x00},* M=n;    Main (n) {Putchar    (*m)! = ' + '? Main    (m++): Exit (n++);}

This is actually more similar to the one above, and even simpler is the conversion of a character to an ASCII code.

First, declare a global array of integers, containing the ASCII code values. and assigns its first address to an integer pointer m.

Main (n) {Putchar (*m)! = ' + '? Main (m++): Exit (n++);} This place uses a conditional expression: if Putchar (*m)! = ' \ "is true, continue to main (m++), recursively call yourself, otherwise exit (n++) exits the function. Exit is the end of a process that deletes the memory space used by the process and returns the error message to the parent process, and return returns the function value and exits the function, which is the language level, which represents the return of the call stack. exit itself is not a system call, but a function of the C standard library, in Stdlib, the system call is the exit internal implementation to complete.




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.