当前位置:网站首页>C primer plus learning notes - 2. Constant and formatted IO (input / output)

C primer plus learning notes - 2. Constant and formatted IO (input / output)

2022-06-23 05:41:00 Charles Ren

Constants and variables

Explicit constants

Explicit constants are also called symbolic constants ,define Decorated characters
#define TAX 0.15
When the program is compiled, it will TAX Are replaced with 0.15. All replacements have been completed before running the program .
effect : Improve the readability and maintainability of the program .

const Decorated variable

const The modifier is the variable , But it's read-only .
It allocates memory only at runtime . Storage locations and constants are also different .

Common constants

 Insert picture description here
 Insert picture description here

The meaning of transformation

Conversion instructions store values in binary format in a computer , Convert to string for easy display .
such as 76 This value , He is in the computer 01001100.
Use %d The consciousness of is “ Translate the given value into decimal text and print it out ”. therefore %d Convert it to characters 7 and 6 And it's shown as 76.

printf Print type

printf Symbols used to print different types of values , Using the wrong specifier will print unexpected results .
printf("%c %d", '$', 2*a) printf What's printed is the value , Regardless of variables , Constant , Or expression .

Integer conversion

Conversion instructions Output
%d Decimal display , Printable int and short, It can also be written %i
%ldlong
%lldlong long int
%o octal int
%lo octal long
%x Hexadecimal int
%lx Hexadecimal long
%#o Octal prefix 0 Show
%#x Hexadecimal prefix 0x Show
%#X Hexadecimal prefix 0X Show
%uunsigned int
%luunsigned long
%4d Minimum field width , If there is not enough space for the number to be printed , Automatically expand
%zd perhaps %zu, Express size_t Type value ,sizeof() perhaps strlen() The result type returned by the operation . In the early C use %ul To receive

Floating point conversion

Conversion instructions Output
%f Floating point decimal notation means float or double,float Will be automatically converted to double Output
%6.2f Indicates the number to be printed ⾄ Less occupation 6 Character width , And ⼩ After the count ⾯ Yes 2 position ⼩ Count ,⼩ Counting points ⼀ position , So the integer part ⾄ Less occupation 3 position
%.2f Indicates that the integer part is normal , The decimal part is reserved 2 He and %0.2f The meaning is the same
%Lflong double This should be capitalized
%e Use e Count to represent floating point numbers
%g Automatically select according to the value %f perhaps %e

Print floating point numbers of different precision
https://blog.csdn.net/dodamce/article/details/115297198

Other conversions

Conversion instructions Output
%c Single character
%s Character string
%p Pointer value
%% Print a percent sign
\n Line break
\\ Slash
\’ Single quotation marks
\" Double quotes

Mark

 Insert picture description here

#define PAGES 959
int main(void)
{
    
    printf("*%d*\n", PAGES);
    printf("*%2d*\n", PAGES);
    printf("*%10d*\n", PAGES);// Print 10 Character width 
    printf("*%-10d*\n", PAGES);// Align left 
    return 0;
}

//*959*
//*959*
//* 959*
//*959 *
// floats.c -- some floating-point combinations
#include <stdio.h>
int main(void)
{
    
    const double RENT = 3852.99;  // const-style constant
    printf("*%f*\n", RENT); //  Retain 6 Decimal place 
    printf("*%e*\n", RENT); //e notation 
    printf("*%4.2f*\n", RENT); //4 Width ,2 Decimal place , If the width is not enough, the original number will be displayed 
    printf("*%3.1f*\n", RENT);
    printf("*%10.3f*\n", RENT); //10 Width ,3 Decimal place 
    printf("*%-10.3f*\n", RENT); // Align left 
    printf("*%10.3E*\n", RENT);
    printf("*%+4.2f*\n", RENT); // Display symbols 
    printf("*%010.2f*\n", RENT); // use 0 Not enough bits filled 
    
    return 0;
}
// *3852.990000*
// *3.852990e+03*
// *3852.99*
// *3853.0*
// * 3852.990*
// *3852.990 *
// * 3.853E+03*
// *+3852.99*
// *0003852.99*

Print string

There are three ways to print long string newlines ,

  1. The use of multiple printf sentence .
  2. Use backslash , But the second line must be left facing , Otherwise, there will be blank space
  3. ( recommend ) Use double quotation mark linker
int main(void)
{
    
    printf("Here's one way to print a ");
    printf("long string.\n");
    printf("Here's another way to print a \ long string.\n");
    printf("Here's the newest way to print a "
           "long string.\n");      /* ANSI C */
    
    return 0;
}

scanf function

C The language library has multiple input functions , however scanf Is the most common one , Because it can read data in different formats .
scanf and printf The way of use is basically the same , But be careful .scanf Functions use pointers to variables .
That is, pay attention to two techniques when using :

  1. scanf Read the value of the basic variable type before the variable name &
  2. Read a string into a character array , The variable name is not preceded by &

scanf Function USES blank ( A newline , Tabs and spaces ) Divide the input into multiple fields . That is, use white space as a separator .

scanf The conversion description used is the same as printf identical , See the table above . The only difference ,scanf All input floating-point types are applied to float On type .

give an example

scanf Read a %d, That is, read an integer :

  1. He will skip the blank characters entered , Read from the first non white space character
  2. Then read the numeric characters , Until you encounter non numeric characters , He thought he had reached the end of the whole number .
  3. Then put the number into the variable .

If you use %s Conversion instructions ,scanf Will read all characters except white space .

  1. He will skip the blank characters entered , Read from the first non white space character
  2. Read the characters in turn until you encounter whitespace again , stop it .

Other input functions

We see that if I want to read a string with spaces, use scanf Can't read .
Use getchar() and fgets() It is more suitable for dealing with some special cases of strings . I 'll talk about it later .
It's just scanf Is a more general input method .

Format input

scanf("%d,%d:, &n, &m);
Then the input must be in the following form
55,45

scanf("%d %d:, &n, &m);
Then the input must be in the following form
55 45

原网站

版权声明
本文为[Charles Ren]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230355164070.html