Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Printf & scanf function

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

1. Printf function

This is a function declared in stdio.h (printf and scanf), so you must add # include before using it

1. Usage

1 > printf (string)

Printf ("Hello, World!")

2 > printf (string, format parameter)

1 / / use constant as parameter 2 printf ("My age is% d\ n", 26); 3 4 / / you can also use the variable 5 int age = 17 int age 6 printf ("My age is% d", age)

Summary: the number of formatters in the left string must be the same as the number of formatter parameters on the right; the type of formatter determines the type of formatter parameters, such as using% d, indicating that the corresponding formatter parameters must be integer

two。 Commonly used format characters and their meanings

3. Fine format control of format characters

1 > output width

If I change% d to% 4d:

Printf ("The price is% 4d.", 14)

Output: that is, there are 2 more widths in front of "14".

If the actual numerical width is relatively large, such as using% 4d to output "142434" with a width of 6, it will be output according to the actual numerical width of 6.

If you change it to%-4d

Printf ("The price is%-4d.", 14)

Output result:

2 > the number of decimal places of a floating point number

Printf ("My height is f", 179.95f)

Output: 6 decimal places are output by default.

If you only want to output 2 decimal places, change% f to% .2f.

Printf ("My height is% .2f", 179.95f)

Output result:

You can set both the output width and the number of decimal places

Printf ("My height is 8.1f", 179.95f)

Output result:

2. Scanf function int age;scanf ("% d", & age)

Note:

1. When the scanf function is executed, it will wait for the user's keyboard input and will not execute the code later.

The second argument to 2.scanf does not pass the age variable, but the address of the age variable & age,& is an address operator in C that can be used to get the address of the variable.

3. When the input is finished, hit the enter key to tell the scanf function that we have finished typing, and the scanf function will assign the input 8 to the age variable

Scanf () enter notes:

For example, the following examples:

/ / comma, scanf (& a, & b, & c); / / input format: 10 d#%d#%d 14, & b, & c); / / input format: 10x14x20:% d#%d#%d, & a, & b, & c); / / input format:% dx%dx%d, & a, & b, & c); / / input format:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report