Document directory
- 1. scanf function-related applications
- 2. Retrieve decimal places from a string of characters
- 3. Copy two files
- 4. pointer operations
- 5. stdout, stdin, stderr
- 6. sscanf and sprintf Functions
1. scanf function-related applications
# Include <stdio. h>
Int main ()
{
Int I = 0;
Float F;
Char C1, C2;
// Scanf ("% 3d % F", & I, & F); // 3D indicates that only three digits are allowed.
// Printf ("I = % 010d, F = % 010.2f \ n", I, f); // The preceding 0 indicates that the remaining vacancies are filled with 0.
Int W;
// For (W = 1; W <5; W ++)
// Printf ("% C, % * C \ n", 'A', W, 'A '); // The * in the middle indicates that the W-1 is printed with spaces each time you enter (if the W-1 is <0, calculated as 0 ).
// Scanf ("% 3d % 3d", & I, & W); // truncate three digits. If the three digits contain blank characters, stop.
// Printf ("I = % d, W = % d \ n", I, W );
// Scanf ("% * D % d", & I, & W); // * indicates that the number entered for the first time is disabled.
// Printf ("I = % d, W = % d \ n", I, W );
// Scanf ("% [a-d] % C", & C1, & C2); // enter a character between [a-d, if no character is entered between a-d,
// The program cannot take the value normally. Note that blank characters cannot be filtered out during input. [^ A-d] indicates a non-(A-z) character
// Printf ("C1 = % C, C2 = % C \ n", C1, C2 );
Scanf ("% * [^ \ n]");
Scanf ("% * C"); // clears the input buffer. (= Fflush (stdin ))
Char str1 [20], str2 [20];
// Scanf ("% * [^ \ n]"); scanf ("% * C ");
// Scanf ("% [A-Z] % [^ A-Z ^ \ n]", str1, str2 ); // ^ \ n indicates that the input data takes effect immediately.
Scanf ("% [A-Z] % [^ A-Z ^ \ n]", str1, str2); // A-Z indicates that only characters between a-Z are extracted, if the input is abc123ty, str1 = ABC.
Printf ("str1 = % s, str2 = % s \ n", str1, str2 );
Return 0;
}
2. Retrieve decimal places from a string of characters
# Include <stdio. h>
Int main ()
{
Float farray [20]; int I = 0;
File * pF = fopen ("1.txt"," R ");
Float D;
Char ch;
While (CH = GETC (PF), ch! = EOF) // when the end of the file is not obtained
{
If (CH <= '9' & ch> = '1 ')
{
Ungetc (CH, Pf); // you can use ungetc to retrieve the first digit of a decimal number. Otherwise, you will miss one. When ch is a number, the pointer has been backward followed by one character. At this time, scanf will miss one character, so ungetc is used.
Fscanf (PF, "% F", & D );
Farray [I ++] = D;
Printf ("% F \ n", d );
}
}
Fclose (PF );
Return 0;
}
# Include <stdio. h>
# Include <ctype. h>
// Obtain the integer
Void main (void)
{
Int ch;
Int result = 0;
Printf ("enter an integer :");
While (CH = getchar ())! = EOF & isdigit (CH )! = 0)
{Result = Result * 10 + CH ;}
If (Ch! = EOF)
{Ungetc (CH, stdin );}
Printf ("% C \ n", getchar ());
}
3. Copy two files
// Input the source file name and target file name from the command line to complete copy
# Include <stdio. h>
Int main (INT argc, int * argv [])
{
File * psoruce, * pdest; char C;
If (argc! = 3) // incorrect input format
{
Fprintf (stderr, "the usage is: % s source DEST \ n", argv [0]);
Return 1;
}
If (psoruce = fopen (argv [1], "R"), null = psoruce)
{
Fprintf (stderr, "Open % s fail! \ N ", argv [1]);
Return 1;
}
If (pdest = fopen (argv [2], "W"), null = pdest)
{
Fprintf (stderr, "Open % s fail! \ N ", argv [2]);
Fclose (psoruce );
Return 1;
}
While (C = GETC (psoruce), C! = EOF)
{Putc (C, pdest );}
Fclose (psoruce); fclose (pdest );
}
Appendix:
Int * restrict a // restrict is optimized during indirect addressing. It is added in c99. This keyword can only be used to modify pointers because pointers have indirect addressing.
Register int x = 10; // register is to put the variable request into the CPU register, so that the operation speed will be very fast, but only the request, the program may not necessarily be placed in the register.
Volatile int y = 20; // volatile indicates that this variable is a variable that is easy to change. Be careful when using it to prevent some Compiler Optimization operations,
4. pointer operations
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
Void Pt (INT (* A) [4], int N)
{
Int I, J;
For (I = 0; I <n; I ++)
{
For (j = 0; j <4; j ++)
{
Printf ("% d \ t", a [I] [J]);
}
Printf ("\ n ");
}
Printf ("\ n ");
}
Int main ()
{
// Double X;
// Strcpy (char *) & X, "ABCDE ");
// Printf ("% s \ n", & X );
// Int y;
// Char * Pc = (char *) & Y;
// * Pc = 'a ';
// * (PC + 1) = 'B ';
// * (PC + 2) = 'C ';
// * (PC + 3) = 'D ';
// Printf ("% # x \ n", Y); // # x output is 0x
// Char * P = (char *) malloc (8 );
// Strcpy (P, "ABCDE ");
// Printf ("% s \ n", P );
// Free (P );
Int A [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
// Double * Pd = (double *);
// Int I = 0;
// For (I = 0; I <6; I ++)
// Pd [I] = I + 0.12;
// For (I = 0; I <6; I ++)
//{
// Printf ("% lf \ t", Pd [I]);
//}
Char * Pc = (char *);
Strcpy (PC, "abcdefg ");
Printf ("% s \ n", PC); // The allocated memory can be stored in double type or char type.
// Int (* P) [4]; // P and * P are the same, because P is an array pointer pointing to an array, so its * P and P are the same, p + 1 points to the next group of data, p + 1 = * (p + 1 ). but the types are different.
// P =;
// Char ** Pa = (char **) A; // The Windows compilation check is enhanced, and only mandatory type conversion is required.
// * PA = 1; * (PA + 1) = 2;
// Printf ("% P \ n", P );
// Printf ("% P \ n", * P );
// Printf ("% d \ n", ** P );
// Printf ("% P \ n", p + 1 );
// Printf ("% P \ n", * p + 1 );
// Printf ("% d \ n", ** (p + 1 ));
// Printf ("% C \ n", * pA, * (PA + 1); // PA points to the first address of the one-dimensional array expanded by a two-dimensional array, PA + 1 follows four bytes backward (if in int type, it points to the next byte. If it is in char or double, this is not the case ).
// Pt (A, 3 );
Return 0;
}
5. stdout, stdin, stderr
# Include <stdio. h>
// Each process opens these three files by default:
// File * stdin; // standard input device file (keyboard)
// File * stdout; // standard output device file (Display)
// File * stderr; // standard error output device file (Display)
Int main ()
{
Fprintf (stdout, "% s", "Hello world! \ N "); // This sentence is the same as the two sentences.
// Printf ("% s", "Hello world! \ N ");
Fprintf (stderr, "% s", "Hello world! \ N ");
Int X;
Fscanf (stdin, "% d", & X); // This sentence is the same as the following sentence.
Scanf ("% d", & X );
/* Differences between stdout and stderr:
1. stdout has an output buffer and stderr has no output buffer.
2. stdout can be redirected. stderr is not redirected (that is, stdout can send output results to other places, but stderr cannot );
3. When will stdout clear the buffer:
When the output line is changed, the buffer zone is full, fflush is used for Refresh, the buffer zone content is sent to the monitor */
Return 0;
}
6. sscanf and sprintf Functions
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Int x = 100;
Int y= 200;
Char C;
Double D;
Char s [100] = {0 };
// Sprintf (S, "% d-% d", x, y); // The Sprint can obtain the desired string type.
// Printf ("% s \ n", S );
// Char * PS = "100 ";
// Int z = atoi (PS); // atoi converts a string into an integer
// Printf ("% d \ n", Z );
// Double D = 3.14;
// Sprintf (S, "% 7.3f", d );
// Printf ("% s \ n", S );
Char * PS = "A 10 3.14 string ";
Sscanf (Ps, "% C % d % lf % s", & C, & X, & D, S );
Printf ("% C-% d-% lf-% s", C, x, D, S );
Return 0 ;}