C Language Primer is rectification, as for this book, if only for the examination study is good, but if later to engage in programming work and C deal with more, personally think only this book is not enough to get started.
C Primer Plus is a much better introductory book that speaks very well. So this book is also thicker ... Series articles are referenced in this book.
printf () function base expansion:
Presumably everyone's first C language program should be the following on the screen to output Hello World's classic program bar.
#include <stdio.h>int main () { printf ("Hello world! " ); }
This prints out a string, and of course we can use printf to print out other parameters. The printf print variable depends on the type of the parameter, such as using the%d symbol when printing an integer, and using the%c symbol when printing the character. These symbols are called conversion descriptions , and they specify how to convert the data into a form that can be displayed.
Remember to use a conversion description for each item in the argument list that follows the control string, that is, the conversion specification corresponds to parameter one by one , such as
printf ("A is%d, B is%d", a); Error
printf ("A is%d, B is", A, A, a, a); Error
printf ("A is%d, B is%d", a, A, a, a)//correct
Again, remember that the type of conversion description matches the type of the parameter.
#include <stdio.h>
int main ()
{
float N1 = 3.0;
Double n2 = 3.0;
Long n3 = 1234567890;
Long N4 = 2000000000;
printf ("%f%f \ n", n1,n2); Match correctly
printf ("%ld%ld \ n", n3,n4);
printf ("%ld%ld%ld%ld\n", N1,N2,N3,N4); Error matching
}
The result of the above code is as follows (WINDOW7 environment, other platform environment may have different results)
The output of the third matching error shows some strange numbers, why does this result, this printf output mechanism and the parameter passing mechanism about
The parameter passing mechanism differs depending on the implementation, and the following parameter passing is how it works in a Windows system, such as the following function call
printf ("%ld%ld%ld%ld\n", N1,N2,N3,N4);
The call tells the computer to pass the values of the variables N1, N2, N3, and N4 to the computer, and the computer places them in a memory area of the stack . The computer places these values on the stack based on the type of the variable, not the conversion specifier . So N1 occupies 8 bytes in the stack (when it is used as the printf parameter, float is converted to double), N2 is 8 bytes, n3, N4 4 bytes respectively.
The control is then transferred to the printf () function, which reads the values from the stack, but reads them according to the conversion specifier . %ld indicates that 4 bytes should be read, so printf () reads the first 4 bytes in the stack as his initial value, that is, the front half of N1, is interpreted as a long, and the next%ld indicates that 4 bytes are read as the second value, The second part of the N1 is also interpreted as a long type. Similarly, the third and fourth instances of%LD cause the first and second halves of the N2 to be interpreted as a long type, respectively. So although the N3 and N4 specifiers are correct, printf () still reads the wrong bytes.
The return value of printf ():
printf is a return value that returns the number of characters printed (all printed characters, including spaces, non-visible newline characters). If there is an output error, a negative number is returned.
The return value is an incidental feature of the purpose of the printout and is usually not useful. A possible reason to use return is to check for output errors, which are often used to output to a file rather than to the screen .
Reference: C Primer Plus
C Language Foundation Expansion (i.)