Common errors in Linux programming

Source: Internet
Author: User
Common errors in Linux programming-General Linux technology-Linux programming and kernel information. for details, see the following section. 1. abort
Definition:
# Include
Void abort (void );
Purpose: forcibly terminate the program (abnormal termination ). If the current shell does not limit ulimit, the core dump will be performed.

2. assert macro
Prototype:
# Include
Void assert (int expression );
Function: calculates the expression value. if it returns 0 (false), it prints error information to stderr and calls abort to terminate the program.
Note: assert is generally used for debugging during development. To prevent the assert from being disabled after the NEBUG is defined, it is best not to directly input the assert, but as follows:
P = malloc (sizeof (char) * 100 );
Assert (p );

3. exit
Prototype:
# Include
Void exit (int status );
Purpose: return a status value to the operating system. status defines EXIT_SUCCESS and EXIT_FAILURE in stdlib. h.

4. atexit
Prototype:
# Include
Int atexit (void (* function) (void ));
Purpose: register a function. This function can define some operations for execution when the program exits normally. If atexit is successfully registered, 0 is returned; otherwise, 1 is returned. multiple functions can be registered using commas (,). the execution order is the leftmost execution.

5. errno variable:
Definition:
# Include ;
Int errno;
Function: global variable. this value is set between Linux calls and most library functions. errno. h defines the error corresponding to the value. For example, ENOENT represents No such file or directory. The perror function can print error information.
Note: After many functions return and set errno, they are not cleared. if you call a library function that may set errno in case of an error, you are advised to manually clear errno first.

6. strerror
Prototype:
# Include
Char * strerror (int errnum );
Purpose: convert errno into standard error information. For example:
Ps = strerror (ENOENT );
The string pointed to by the pointer ps is "No such file or directory.

7. perror
Prototype:
# Include
# Include
Void perror (const char * s );
Purpose: print the string and standard error information specified by s, which is equivalent
Printf ("% s: % s \ n", * s, strerror (errno ));
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.