An interesting C program

xiaoxiao2021-03-06  54

An interesting C program

Recently I found a simple C pro program in a forum, which contains many skills, here to share with you.

The original procedure is as follows:

#include

Main (_) {char * x = "* b # ** 000 ** i # ******* 2 * 0 *** # -. **** 5. * - # -. **** 54. # ******* 2 ** 6 # **** 00 ** 0. # "; While (_ = * x / 4) _- = 8, Printf (" / n% * s " !! _, _ _, "_ / _ / _ /" * x % 4 * 2);

The surface looks a very strange code, I put it in the VC 6 below, I can't pass, prompt "_" not declare, slightly modify the above code, as follows:

#include

Main (int _) {char * x = "* b # ** 000 ** i # ******* 2 * 0 *** # -. **** 5. * - # -. *** * 54. # ****** 2 ** 6 # **** 00 ** 0. # "; While (_ = * x / 4) _- = 8, printf (" / n% * s " !! _, _ _," _ / _ / _ / " * x % 4 * 2);

The careful people have seen it, just in front of the parameter "_" of the main function, the type of variable is added: int. At this point, the above code can be properly compiled and connected in VC6. The results of operation are as follows:

_ / _ /

_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /

_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /

_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /

_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /

_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /

_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /

It is a word "knocker" consisting of "_ /".

Let's analyze this small program, but I will change this program for it.

#include

Main (int _) {

Char * x = "* b # ** 000 ** i # ******* 2 * 0 *** # -. **** 5. * - # -. **** 54. # ** ***** 2 ** 6 # **** 00 ** 0. # ";

While (_ = * x / 4)

{

_- = 8;

Printf ("/ n% * s" !! _, _ _, "_ / _ / _ /" * x % 4 * 2);

}

}

First take a look at the main function parameters, the name of this parameter is quite "_", we look at some inconsistency, but it is indeed legitimate variable name. In addition, our common main function is generally without parameters. If there is a parameter because the program wishes to handle the parameters required to run this program under the command line format, this is usually: main (int Argc, char ** argv), The first parameter argc represents the number of parameters in the parameter table, and the Argc is a CHAR type 2D array that saves the parameter string. For example, if we enter a command at the command: DIR -SC: / At this point, the value of Argc is 3 (including the command itself), argv [0] points to the string: Dir, arg [1] points to the string: -s, arg [2] points to string C: /. So since the parameter of the main function is usually not, or if it is two, and this program only has one, do you do legal? The answer is yes! I mentioned in the "Variables in the C language", the parameters in the function are saved in the stack, so this involves a problem with which to balance the stack is the caller or called by the caller. When using the VC6 compiler, if the function does not have a special statement, the caller is the caller cleanup stack by default. In other words, when the runtime is called, only one parameter is passed to the main function, this point it knows, after the main function call ends, it is only clearing one when the stack is in the balance stack. Functions, therefore do not have any problems.

Let's look down and define a CHAR type pointer variable x, pointing to a string. Next is the While statement, while (_ = (* x) / 4), but the value of expression _ = (* x) / 4 is not 0, executes the While cycler, however, expression _ = (* X) / 4 is an assignment statement, what is its value? In this case, the value of the expression in the right side of the usual assignment number is the value of the entire expression. When the statement is performed for the first time, the x point to the character '*', the corresponding value is 42, and in this type, when X = 0, the cycle ends when the string is completed.

Next in the original program is: _- = 8, Printf ("/ n% * s" !! _, _ _, "_ / _ / _ /" * x % 4 * 2); here The comma effect is a similar semicolon, but the comma is a statement, and the semicolon is two statements.

We carefully see this statement, its format string is: "/ n% * s", where% * s is rare, many people don't know what format this is, but we can find such a description in MSDN. :

If the width specification is an asseterisk (*), An int Argument from the argument list support. The value of the value being formatted in the argument list.

It means that if the width is specified by the asterisk (*), an INT type parameter should be provided in the parameter list as a value of the width. This is clear, the original PrintF statement is the use of expressions _ _ to control strings "% * S" width, which is replaced (*).

Look carefully, the original format string has not yet finished! Complete should be: "/ n% * s" !! _. This is strange, how is the character string and!! _ Add? In fact, it is not surprising, here!! _ Is a "non-" operation of the variable _, the result should be 0 or 1. To be clear, the string is passed to the function as the parameter, just pass the first address of the string to the function, so the first address of the string "/ n% * s" is the address of the character '/ n', when When this address is added, the format string passes to the Printf function becomes "% * s", so this !! _ of the mystery is to control the wrap! Is it very creative? Of course, the following code is also idea, the string parameter of the PrintF statement to be printed: "_ / _ / _ /" * x % 4 * 2. After the analysis, we are not difficult to understand "_ / _ / _ /" plus the intention of the expression of the back, and the number of control output characters is a "_ /", two, or three. Then the key is the back of this expression: * x % 4 * 2. In fact, slowly analyzing is not difficult, first execute * x% 4 * 2, then perform X . So, when executed when the While statement is executed, X will point to the next character!

In summary, this applet uses a string to control the output, which uses many techniques. For beginners need a good understanding, it is very helpful for future programming!

转载请注明原文地址:https://www.9cbs.com/read-114319.html

New Post(0)