Basic expansion of C language (1) and basic expansion of C Language

Source: Internet
Author: User

Basic expansion of C language (1) and basic expansion of C Language

Tan haoqiang is a beginner in C language. As for this book, it would be good to take the test, but if he wants to engage in programming in the future and deal with C more often, I personally think it is not enough to get started with this book.

C primer plus is a better entry-level book with a comprehensive presentation. Therefore, this book is also thick. The series of articles are referred to in this book.

  

Basic expansion of printf () functions:

Presumably everyone's first C language program should be the following classic program that outputs hello world on the screen.

    

#include<stdio.h>int main(){   printf("hello world!");      }

The output here is a string. Of course we can use printf to print other parameters. The printf print variable depends on the parameter type. For example, the % d symbol is used when the integer is printed, and the % c symbol is used when the characters are printed. These symbols are calledConversion descriptionThey specify how to convert the data into a printable form.

Remember to use a conversion description for each project in the parameter list after the control string, that isThe conversion description must correspond to the parameters one by one., Such

  

Printf ("a is % d, B is % d", a); // Error

Printf ("a is % d, B is", a, B); // Error
Printf ("a is % d, B is % d", a, B) // correct

RememberThe type of the conversion description must match 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 \ n", n1, n2); // correct match
Printf ("% ld \ n", n3, n4 );
Printf ("% ld \ n", n1, n2, n3, n4); // error matching
}

The running result of the above Code is as follows (in Windows 7, different results may occur in other platforms)

        

The output statement with the Third Matching Error shows some strange numbers. Why is this result? The Output Mechanism of printf is related to the parameter transfer mechanism.

The parameter transfer mechanism varies with the implementation. The following parameter transfer is the working principle in windows, and the following function calls

printf("%ld %ld %ld %ld\n",n1,n2,n3,n4); 

This call tells the computer to pass the values of the n1, n2, n3, and n4 variables to the computer.Place on Stack. AccordingThe type of the variable rather than the conversion specifier.Put these values in the stack. Therefore, n1 occupies 8 bytes in the stack (float is converted to double when it is used as the printf parameter), n2 occupies 8 bytes, and n3 and n4 occupy 4 bytes respectively.

Then the control is transferred to the printf () function, which reads these values from the stack,Read the data according to the conversion instructions.Read. % Ld indicates that four bytes should be read, so printf () reads the first four bytes in the stack as its first value, that is, the first half of n1, it is interpreted as a long type. The next % ld indicates that four bytes are read as the second value. Similarly, the second half of n1 is also interpreted as a long type. Similarly, the third and fourth instances of % ld make the first half and the last half of n2 be interpreted as long. Therefore, although the n3 and n4 specifiers are correct, printf () still reads the wrong bytes.

The return value of printf:

Printf returnsNumber of printed characters(All printed characters, including spaces and invisible line breaks ). If an output error occurs, a negative number is returned.

The returned value is a function attached to the purpose of printing the output, which is usually not very useful. One possible reason for using the returned value is that the output is incorrect,It is often used to output to a file rather than to the screen..

 

 

 

Reference: C primer plus

 

 

      

 

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.