Book Note: C/C ++ programmer practical Daquan-C/C ++ best programming guide

Source: Internet
Author: User

This book lists 31 chapters with 1500 knowledge points, with many examples. This book is suitable for programmers who have some knowledge about C/C ++ to check for missing information. The explanation of the knowledge points here is rather messy and not very organized. The previous content is sometimes used later. If you don't understand it, it will be difficult to read it.

Another major feature of this book is that it targets dos platforms in many places. I use Linux, so it is not suitable for many places. It may not be applicable to some Windows platforms. This book is still old. You can skip some unnecessary tasks. For introduction to C ++, if you want to read c ++ primer first, the book is more organized and can help you clarify the relationship. Understand Each knowledge point.

I have not read the last part of the book about Windows programming, because the knowledge is relatively old, and I am from the Linux platform.

Chapter 2C Language

1.Many C compilers equivalent % I to % d. It is best to use % d Because % I is an old format character and may not be supported by future compilers.

% LD can be used to display long integers

% E scientific notation display floating point number

% G select whether to use % f or % E to display floating point numbers based on actual conditions.

Show positive and negative numbers: Add a plus sign after % in the format operator. For example, % + D, % + F

% 05d 5 indicates the minimum number of characters used to display the value. 0 indicates that the value is filled with 0,

% 8.3f indicates a total of at least 8 digits, of which three digits are retained after the decimal point.

%-5d left alignment

2.To guide printf to add a proper prefix before octal or hexadecimal notation, you can keep up with the format symbol % and place it #. For example:

Printf ("value is % # x \ n", value );

3. determine the number of characters that printf has displayed

When the program is used for complex screen formatting, you sometimes need to know the number of characters that printf has displayed. You can use % N

For example: printf ("Abd % N 123456% n \ n", & A, & B );

After execution, A = 3, B = 10

The return value of printf is the total number of output characters.

4. ANSI device drive

 

Chapter 2 macros and constants

1. predefined macros

_ File _ line _ date _ time __

_ Stdc _ is used to determine whether it is an ansi c Compiler

2. Change and processor row count

Use # Line preprocessing to change the row number

For example, # line 100 "filename. c" // indicates the current behavior of 100 rows. If _ line _ appears below, it is counted from now on.

 

Chapter 2 string

1. Common Character Processing functions in C

# Include <ctype. h>

Int isalnum (int c );

Int isalpha (int c );

Int iscntrl (int c );

Int isascii (int c );

# Include <ctype. h>

Int isdigit (int c );

Int isodigit (int c );

Int isxdigit (int c );

# Include <ctype. h>

Int isgraph (int c );

Int ispunct (int c );

Int isprint (char C );

# Include <ctype. h>

Int islower (int c );

Int isupper (int c );

# Include <ctype. h>

Int isblank (int c );

Int isspace (int c );

Ing iswhite (int c );

# Include <ctype. h>

Int tolower (int c );

Int toupper (int c );

 

2. Common string processing functions

# Include <string. h>

Char * strcat (char * DEST, const char * SRC );

Char * strncat (char * DEST, const char * SRC, size_t N );

# Include <string. h>

Int strcmp (const char * S1, const char * S2 );

Int strncmp (const char * S1, const char * S2, size_t N );

# Include <string. h>

Size_t strlen (const char * s );

# Include <string. h>

Char * strstr (const char * SRC, cosnt char * sub );

Char * strtok (char * STR, const char * Set );

# Include <string. h>

Char * strchr (const char * s, int C );

Char * strrchr (const char * s, int C );

# Include <stdlib. h>

Double strtodd (const char * nptr, char ** endptr );

Float strtof (const char * nptr, char ** endptr );

Long Double strtold (const char * nptr, char ** endptr );

# Include <stdlib. h>

Double atof (const char * Str );

Int atoi (const char * Str );

Long atol (const char * Str );

Long long Atoll (const char * Str );

# Include <string. h>

Char * strcpy (char * DEST, const char * SRC );

Char * strncpy (char * DEST, const char * SRC, size_t N );

# Include <string. h>

Int strcoll (const char * S1, const char * S2 );

Size_t strxfrm (char * DEST, const char * SRC, size_t Len );

# Include <string. h>

Size_t strspn (const char * s, cosnt char * Set );

Size_t strcspn (const char * s, cosnt char * Set );

Char * strpbrk (const char * s, const char * Set );

 

Chapter 2 Functions

1. in C language, if the user-defined function name is the same as the library function name, the compiler generally chooses to use the User-Defined Function.

The basic purpose of a stack is to support function calls. When a function is called, the compiler pushes the return address and parameters to the stack.

The time it takes for a programmer to press a computer into and pop up a stack becomes the overhead of a function.

2. Volatile keywords

3. Stack-related: Call structure and base pointer

4. Functions with Variable Parameter count are supported.

To support variable parameters, use the macro va_arg, va_end, and va_start (defined in the stdarg. h header file) in C to create a variable number of functions that support variable parameters. In essence, a macro gets parameters from the stack every time until the program gets the last parameter. When using these macros to obtain parameters, you must know the type of each parameter. The identifier (such as % d, % s, % F) is used to match the parameter type in the most typical Variable Parameter count function of printf.

You can use a format character similar to that used in printf to pass the parameter type to the function:

Result = add_values ("% d", 1, 2, 3 );

 

Chapter 4 keyboard operations

1.

# Include <stdio. h>

Int fgetc (File * stream );

Char * fgets (char * s, int size, file * stream );

Int GETC (File * stream );

Int getchar (void );

Char * gets (char * s );

Int ungetc (int c, file * stream );

The relationship between the above functions:

Fgetc () is to read the next string from stream and then return,

GETC and fgetc are equivalent, but they are implemented using macros.

Getchar () is equivalent to GETC (stdin)

 

Gets () is to read multiple characters from stdin to the buffer S. It knows that a line terminator is encountered and '\ 0' is used in S to replace the Terminator.

Chapter 2 mathematics

1. Obtain the ending number and index of the floating point number.

You can use the frexp function to obtain the index and tail number of a floating point number.

# Include <math. h>

Double frexp (double value, int * exponent );

The return value is the number of digits, and the second parameter is the index.

 

The float type is 4 bytes in size, that is, 32 bits. The storage method in the memory is as follows:

High address <--------------------------------------> low address

| Symbol bit | index | tail number |

| 1 bit | 8 bit | 23 Bit |

31 <------> 30 <---------> 22 <----------------------> 0

 

The size of the double type is 8 bytes, that is, 64-bit. The memory layout is as follows:

High address <----------------------------------------> low address

| Symbol bit | index | tail number |

| 1 bit | 11 bit | 52 bit |

63 <------> 62 <------------> 51 <---------------------> 0

 

2. Calculate the result of x * 2e

You can use the ldexp function to calculate x * 2E.

# Include <math. h>

Double ldexp (double value, int exponent );

3. The macro min and Max are provided in the stdlib. h file.

4. Split the floating point value into integers and decimals.

Use the MODF function to break down floating point numbers into integers and decimals.

# Include <math. h>

Double MODF (double value, double * integer_part );

5. # include <math. h>

Double POW (double value, double power );

Double pow10 (INT power );

 

Double Log (double value );

Double log10 (double value );

6. generate random number

The random number generation functions provided in C: Rand and random. Both return integer random numbers.

# Include <stdlib. h>

Int rand (void );

Int random (INT ceiling );

Rand returns a random integer between 0 and rand_max. The second function random returns a random number between the range 0 and ceiling. Ceiling specifies the maximum value of the random number. The main function passes it to the random function.

 

Chapter 4 files, directories, and disks

1. File structure

In C, the file used for file stream operations is a struct. Generally, the file * pointer is used.

The fopen function is usually used to return a file * pointer.

The structure format of the general file is as follows:

2. Use low-level and advanced files I/O

In fact, advanced I/O is a common stream-based file function. The so-called low-level file I/O is some system calls. The functions listed above are called by some systems, and the implementations of different systems are different. This is actually a question about the relationship between database functions and system calls.

3. Get the corresponding file descriptor by knowing the file stream pointer

Int fileno (File * stream );

Main Application: if the program previously opened a file with fopen but wants to lock it,

For example, int fcntl (INT Fildes, int command, struct flock * flock_structure) (Note: Command is f_getlk, f_setlk, and f_setlkw)

Or int lockf (int fd, int cmd, off_t Len), you must first use fileno to obtain the corresponding file descriptor and then perform the fcntl/lockf operation.

4. Get the corresponding file stream pointer by knowing the file descriptor

File * fdopen (INT Fildes, const char * type );

Associate a file stream with an open file descriptor

Fildes can be the results returned by system calls such as open, dup, dup2, creat, pipe, and socket.

Type specifies the open mode, such as fopen's "R", "W", and ".

The fdopen open method is subject to the Fildes open method. For example, if o_rdonly is used for open, fdopen can only use the "r" method.

Main Application: When you have to open only the file number, but want to use fprintf, fscanf and other stream operations to read and write data, you can use fdopen once.

5. Rename the file

C provides the function Rename for renaming or moving objects.

# Include <stdio. h>

Int Rename (const char * Old, const char * New );

6. delete an object

The remove function is provided in the C library function to delete files.

# Include <stdio. h>

Int remove (const char * pathname );

In addition, functions are provided in the UNIX operating system to delete files.

# Include <unistd. h>

Int unlink (const char * path );

7. determine how the program accesses files

Function C access checks whether the specified file exists and whether the user can open the file as required.

This is generally implemented by the operating system.

8. Write the buffer to the disk.

C provides the function fflush to write cached data to the disk.

# Include <stdio. h>

Int fflush (File * stream );

9. Get the file handle of the file stream

# Include <stdio. h>

Int fileno (File * stream );

10. temporary file C interface

If you encounter text processing in the C program, it is very likely to involve the processing of temporary files.

In different situations, temporary file interfaces may have different requirements. For example, if you only use a temporary file as a temporary data storage space and do not need to share it with other processes, you can directly use the tmpfile () function. Tmpfile () returns a file * handle. The temporary file created by tmpfile () does not have an actual file name. The advantage is that when the program ends, you do not need to manually delete the file.

However, if you want to share a temporary file with other processes, or call other programs to transmit data using the file name of the temporary file as a parameter, tmpfile () cannot be used, in this case, mkstemp () is used (). Mkstemp () generates a non-existent file name through the input template string, creates the file at the same time, returns the file handle as the function return value, and the file name can be obtained from the modified template string.

The above two functions are relatively modern. If you are interested in history, you can look at the following three functions.

The mktemp () function is used to generate a non-existent file name. This function is no longer recommended now, and has even been removed from the POSIX.1-2008. We can see from glibc mktemp (3) that the main reason why mktemp () is not recommended is that mktemp () only generates a file name that does not exist currently and does not directly create the file. If you need to create this file, the temporary file may be created by other processes between mktemp () and creat (), resulting in file creation failure.

The tmpnam () function has the same problem as mktemp (). At the same time, when the input parameter is null, It is not thread-safe because static variables are required. It is not recommended in POSIX.1-2008.

Tempnam () is similar to tmpnam (), but has more parameters and higher controllability. Similarly, it is not recommended in POSIX.1-2008.

11. read and write one word each time

C provides functions getw and putw to read and write a word.

# Include <stdio. h>

Int getw (File * stream );

Int putw (int w, file * stream );

 

12. Read and Write struct

Low-level Read and Write Functions

Both advanced fread and fwrite functions can read and write struct data.

13. Associate the file handle with the file stream

You can use the fdopen function to associate the file handle with the file stream:

# Include <stdio. h>

File * fdopen (int fd, const char * mode );

14. Directory operations

Open Directory: opendir

Read Directory: readdir

Reset directory list: rewinddir

 

15. Read and Write text by row

Use functions fgets and fputs

 

16. Why cannot I use fgets and fputs to copy binary files?

When the fgets function reads text, fgets regards Ctrl + z (with an ASCII value of 26) as the end of the file. Because the value 26 can appear multiple times in the binary file, fgets will end the copy process at 26 for the first time. Therefore, to copy binary files reliably, you must use C's low-level I/O functions.

17. Determine the end Of the file

When the fgets function encounters the end of a file, it returns NULL, and EOF is also returned when fgetc encounters the end of the file. Therefore, before a program performs certain operations, it is necessary to determine whether the file pointer is at the end of the file. The feof function is generally used.

# Include <stdio. h>

Int feof (File * stream );

18. Get file handle information

# Include <sys/types. h>

# Include <sys/STAT. h>

# Include <unistd. h>

Int Stat (const char * path, struct stat * BUF );

Int fstat (int fd, struct stat * BUF );

Int lstat (const char * path, struct stat * BUF );

Stat struct

Struct stat {

Dev_t st_dev;/* ID of device containing file */

Ino_t st_ino;/* inode Number */

Mode_t st_mode;/* Protection */

Nlink_t st_nlink;/* Number of hard links */

Uid_t st_uid;/* User ID of owner */

Gid_t st_gid;/* Group ID of owner */

Dev_t st_rdev;/* device ID (if special file )*/

Off_t st_size;/* Total size, in bytes */

Blksize_t st_blksize;/* blocksize for file system I/O */

Blkcnt_t st_blocks;/* Number of 512b blocks allocated */

Time_t st_atime;/* time of last access */

Time_t st_mtime;/* time of last modification */

Time_t st_ctime;/* time of last status change */

};

 

 

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.