C language-two library functions, C language library functions
Two library functions
--------------------------
-- 1 -- printf function-- 2 -- scanf Function
--------------------------
[Written at the beginning :]
『
Function: it is a mathematical term from the name. It was used by laveniz in 1694 to describe a volume related to the curve, such as the slope of the curve or a certain point on the curve.
In the process-oriented C language, it is used to complete some specific functions.
Some object-oriented languages such as OC and Java are called methods. Indicates the behavior of an object.
Method or function. First of all, you need to understand what he is doing?
Simply put --> C functions are used to complete program functions
{
C language not only provides rich library functions, but also allows users to define their own functions. Each function is a reusable module that calls each other to implement complex functions in an orderly manner. It can be said that all the work of the C program is done by a variety of functions, so C language is also called a functional language. It can be said that all the work of the C program is done by a variety of functions, so the C language is also called a functional language.
Standard C language (ansi c) defines a total of 15 header files, known as the "C standard library", all compilers must support, how to use these standard libraries correctly and skillfully, it can reflect the level of a programmer.
- Qualified programmers: <stdio. h>, <ctype. h>, <stdlib. h>, and <string. h>
- Skilled programmers: <assert. h>, <limits. h>, <stddef. h>, and <time. h>
- Excellent programmer: <float. h>, <math. h>, <error. h>, <locale. h>, <setjmp. h>, <signal. h>, <stdarg. h>
There are not only a large number of functions, but also hardware knowledge.
It should also be pointed out that in C language, all function definitions, including the main function, are parallel. That is to say, in a function body, you cannot define another function, that is, you cannot define nested functions. However, mutual calls and nested calls are allowed between functions. Traditionally, the caller is called the main function, and the caller is called the called function. A function can also be called as a recursive call by itself.
The main function is the main function. It can call other functions, but cannot be called by other functions. Therefore, the execution of the C program always starts from the main function. After calling other functions, it returns to the main function and ends the entire program by the main function. A c source program must have and can only have one main function. (Reference from http://c.biancheng.net/cpp/html/55.html here)
}
』
-- 1 -- Introduction to printf function 1.1 printf function
Printf is a standard output library function that outputs the computation results of a program in a precise format.
Format: printf ("format control string", output item list (Variable list ));
Output item list: It must be exactly the same as the format controller in terms of type and quantity. When multiple output items exist, each output item is separated by commas.
1.2 format control string:
The format of the specified data input, which consists of a format controller and a common character, and is used together with % to describe the data type (format character) of the input data ).
The variable address for receiving data. The input items correspond to the format control string in terms of type and quantity. When there are multiple input items, the address names are separated by commas, the input format must be consistent with the variable type.
Instructions for use:
% D output a 10-digit integer
% F outputs a real number type data (integer data cannot be output). The default output is 6 decimal places.
% C Outputs a character
% S output a string
% O outputs an eight-digit format
% X outputs a hexadecimal format
% P output memory address
% Ld: input and output values of the double (real) Type
% U: Enter and output unsigned (unsigned) type value
1.3% f output Accuracy Problem float f1 = 3.1415926f; float a = 1111.111111; float B = 2222.22222; printf ("f1 = % f \ n", f1 ); // --> 3.1415923 by default, the output number is 6 digits after the decimal point/* Note: After % f is printed, the precision is 6 digits after the decimal point, the valid number is 7 digits, so the last number "5" is invalid. Here, floalt is the seventh digit after the decimal point, and the number after the valid number is invalid. */Printf ("f1 = %. 7f \ n ", f1); // -- & gt; 3.1415925 printf (" a + B = % f \ n ", a + B ); // --> All digits after the 3333.333252 valid digits (7 digits) are invalid.% F output precision example
Solution
To print a larger precision, only the double type is used.
The default value of double is the last 7 digits after the decimal point, and the valid number is 15 digits.
The valid digits are the total digits before and after the decimal point, excluding the decimal point.
1.4 precautions for using printf Functions
1) domain width Problem
1) % md problem (setting the domain width (number of columns)
M is a number that can be positive or negative.
If you want to output the number of digits> the field width m -->, the output is based on the actual number of digits of the data.
If the number of digits is <domain width m -->, spaces are required.
Printf ("% 3d \ n", 18888); // outputs printf ("% 5d \ n", 10) if the number of digits is greater than the domain width ); // 10 (3 spaces are added before 1)View Code
M has two values (positive and negative ):
M> 0 fill in spaces from the left
M <0 fill space from the right
2) % 0md (commonly used for image traversal)
% 0md is an upgraded version of % md, indicating that the remaining part is supplemented with 0
printf("%02d\n", 1); //01
2) escape characters
Printf ("\ n"); // --> \ printf ("% \ n"); // --> % printf ("\" \ n "); // --> "printf (" \ t tab \ n "); // Tab
-- 2 -- scanf Function
2.1 Introduction
The sacnf function is a blocking function. The prototype is included in the standard input/output header file <stdio. h> and is used to receive content from keyboard input.
Format: scanf ("format control string", address list of input items );
Common Format controllers of scanf:
2.2 role
Receives input content from the keyboard and saves it to the specified variable.
2.3 sanf function precautions
1) the scanf function stops execution when a carriage return occurs.
2) When a single variable value is accepted, if spaces, carriage return, tab, and other variables are input before the value is input, they are ignored.
3) Illegal format input
3.1) Separate the input with commas (,).
Input: 12, 45 --> age = 12, num is not assigned
3.2) mixed space Input
Scanf ("% d, % c, % d", & a, & ch, & B); // input: 12, a, 45 printf ("% d, % c, % d \ n ", a, ch, B); // the actual output is different from the expected output --> 12,-1View Code
Solution: enter as is
4) use of the * Number
Int a =-1, B =-1; printf ("Enter the values of a and B: \ n"); // % * d to skip an integer, % * c ignores a character scanf ("% d % * d % d", & a, & B); // enter 1990 11 20 printf ("a = % d, B = % d \ n ", a, B); // a = 1990, B = 20View Code
2.4 input Cache
After the user inputs the content, it is saved to the scanf input cache. Then, scanf retrieves the desired content from the input buffer according to the format-controlled characters.
If the content obtained from the buffer is consistent with the specified format, the value is assigned to the variable.
If the format is inconsistent, the value of the variable is not modified.
[Written at the end :]
『
See a picture on the Internet:
I thought of my previous Java path. All the way hard, all the way stumbling, but also have a way of scenery.
Until one day, there was no traffic light at a crossroads. So I am confused, and I know that I always want to make a choice, but I understand that sometimes too many choices are not necessarily a good thing.
If it was you, I thought you would move forward.
If it was you, I thought you would stop.
If it was you, I thought you would go back.
But I am not you.
』