C printf and scanf usage

Source: Internet
Author: User
Tags control characters

 

I have never learned C language and only learned C ++ before, so I learned C language. In fact, I personally think the difference between C and C ++ is very small, basically, the difference between the output and input of printf and scanf is that the header file to be included is different. For example, in C ++, it is # include <iostream>. In C, it is # include <stdio. h>.

When calling printf and scanf, you must include # include <stdio. h>

Printf is used: printf ("format control string", output parameter 1, output parameter 2 );

The format control string contains: Format control instructions, common characters

The format control description mainly refers to the output of data in the specified format, including the format control characters starting with %. Different types of data use different formats to control characters (INT type uses % d, % F for float and double)

Common characters are the characters that are output as they are, for example, Fahr = % d, Celsius = % d \ n in "Fahr = % d, Celsius = % d \ n ".

Scanf is used: scanf ("format control string", input parameter 1, input parameter 2 );

The format control string contains: Format control instructions, common characters

The format control string represents the input format (% d for int type, % F for float, and % lf for double type)

Common character: the usage of printf is the same as that mentioned above.

The input parameter is the address of the variable, so you must add &

1. printf ("Fahr = % d, Celsius = % d \ n", Fahr, Celsius );

2. printf ("Enter x (x> = 0): \ n ");

 

3. printf ("Y = f (% F) = %. 2f \ n", x, y );

% F indicates that floating point data is output in decimal form, with 6 decimal places retained, while %. 2f indicates that two decimal places are retained during output.

 

4. scanf ("% lf", & X); // read the input

Call the input data of the scanf () function. Before the variable name X, add &. % L in LF is the first letter of long, the input parameters of the scanf function must correspond to the format control description in the format control string,

And their types, numbers, and locations must correspond one by one.

Scanf ("% d % lf", & X, & Y, & Z) indicates that the input x is of the int type, and Y is of the int type, Z is of the double type. This is a one-to-one correspondence.

 

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.