A simple GNU assert usage Test Program

Source: Internet
Author: User

/*************************************** ******************************
* Function: Test assert
* Author: Samson
* Date: 12/12/2011
* Test Platform:
* GNU Linux version 2.6.29.4
* GCC version 4.4.0 20090506 (Red Hat 4.4.0-4) (GCC)
**************************************** ****************************/

# Include <stdio. h>
# Include <stdlib. h>
# Include <malloc. h>
# Include <assert. h>

Int
Main (INT argc, char * argv [])
{

Assert (argc! = 2 );
Printf ("argc is % d \ n", argc );
Exit (0 );

}

When the condition of assert is false, the program will be interrupted. It is useful for developers to debug and can accurately locate the file with the error: function: row number. When the ndebug macro is not added, the output of this test program is:
A. Out: testassert. C: 9: Main: assertion 'argc! = 2 'failed.
Abandoned

However, when releasing a program version, you do not want to interrupt the program directly. If it is soft, you can use the prompt if statement or print.

The compilation method of the soft version is GCC testassert. C-dndebug.
Run result:
[Root @ UFO testc] #./A. out 2
Argc is 2

The straight-forward version compilation method is GCC testassert. C.
Run result:
[Root @ UFO testc] #./A. out 2
A. Out: testassert. C: 9: Main: assertion 'argc! = 2 'failed.
Abandoned

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.