To answer the question, we need a little historical tour.The end of the 1960s. It is time to develop a large number of programming languages that are subsequently popular.A little sooner than C, Pascal's tongue appeared. The display in Pascal is done by procedure WriteLn:var
age: Integer;
name: String;
begin
WriteLn('Hello, my name is ', name, ', my age is ', age, '!');
end.
Challenge WriteLn Looks like a challenge to the procedure, but it's actually a language design, because normal procedures can't have a variable number of parameters. So that's a call. WriteLn Worked, the language compiler will recognize this design and process it in a special way.Among other things, you may indicate the form of the conclusion, for example, write age:8which means the number age We need to fill the gaps so that it takes eight positions. Of course, we don't have any formats when we call normal procedures.From a certain point of view, such an approach complicates both language and compiler. You have normal procedures and functions, and there are special ones that look like ordinary, but in fact others. On the other hand, in this case the compiler really knows the type of variables and uses that knowledge to correctly extract and format them.The creators of S.'s language were on a different path. They agreed that there would be no specific design in the language and that the whole introduction and conclusion would be made by normal functions. They had to come up with two things:How to transfer variables to the function and to process them.How to change formatting.The answer to the first question is quite complex. It is common practice to transfer parameters through glass. The resulting procedure shall record the parameters in the glass and transmit the control of the procedure. She knows exactly the number and type of his parameters, so she can contact them. She knows where they are relative to the top of the glass. At the end of the work, the procedure removes its parameters from the grid, because it knows how many lights have to be removed.But as soon as you're trying to get a variable number of parameters into the glass, you're in trouble. You don't know where the first parameter is, because you might have 10 in the glass, maybe 30. You don't know how many bikes you have to remove from the grid.In order to challenge these functions, Cre's creators had to go to some of the predators. First, in C, the variables are recorded backwards, from the last to the first. If you call the top of the line, it'll be the first parameter.Second, the causing function knows how many parameters it put in the glass, so it removes them from the grid.The main question remains: how exactly is the function called to deal with what is in the glass behind the first parameter? She has no information on the type and size of the parameters. The answer here is a simple technical trick.The type and number of parameters we point in the formatting line. The address of the formatting line (first parameter) is known because it was caught in the glass by the last and is at a fixed displacement relative to the top of the track. The size of the index is also known, so we can figure out where the second parameter begins.Magic goes further.Function printf Running. on the formatting line and removes any symbol until the percentage symbol is met. Then she looks at him and understands the type and size of the next parameter. She's pulling him off the grid and moving the index in the glass.%d means a whole number, usually 32 bats or 4 bayths. The function extracts four bayths from the grid, takes them out in 10 shape, moving the indicator in the glass to 4 bayt.%f- number with a floating point (double) 8 byte. The compiler converts float into double, as noted in the commentary. The function removes 8 Byte as a number with a floating point and moves the index to 8 Byte.%s Means that the parameter is the starting point of the line, which is 4 bayta in the 32-bit annex and 8 bytes in 64-bit. The function interprets the meaning of the address and removes the line at the address and then moves the indicator in the glass to 4 or 8 Byte.You may be mistaken by transferring one type and size parameters and recording something in the format line. The consequences may be different, from not very dangerous incorrect withdrawal to the edge of the annex, because it is trying to show the line at the wrong address.Such mistakes are unpleasant, and the most offense, the compiler can't find them, at least until he starts specializing in the handling of the challenges. printf and scanf♪Programme alignment is more important, so the compilers are now monitoring the consistency of the formatting line.