C language and C ++ different function definitions

Source: Internet
Author: User

C and C ++ are mostly consistent and compatible in terms of basic syntax, but they still differ in Function Definition and calling. See the following code:

# Include "stdafx. H"
Int subtest (x, y)
Int * X, * Y;
...{
Int Z;
Z = * x> * y? * X: * Y;
Return (z );


}
Int main (INT argc, char * argv [])
...{
// Printf ("Hello world! ");
Int A = 10;
Int B = 20;
Printf ("% d", subtest (& A, & B ));
Return 0;
}

 

This method is called the traditional declaration method of the form parameter. The following is the declaration method of the modern form parameter allowed by the C ++ standard:

 

# Include "stdafx. H"
Int subtest (x, y)
Int * X, * Y;
...{
Int Z;
Z = * x> * y? * X: * Y;
Return (z );


}
Int main (INT argc, char * argv [])
...{
// Printf ("Hello world! ");
Int A = 10;
Int B = 20;
Printf ("% d", subtest (& A, & B ));
Return 0;
}

 

In vc6.0, *. c files can be used in the previous way. *. cpp files cannot be used in the previous way. The error is as follows:

F:/testfun. cpp (5): Error c2065: 'X': Undeclared identifier
F:/testfun. cpp (5): Error c2065: 'y': Undeclared identifier
F:/testfun. cpp (6): Error c2448: '<Unknown>': function-style initializer appears to be a Function Definition
F:/testfun. cpp (6): Fatal error c1004: unexpected end of file found
Error executing cl.exe.

Testfun.exe-4 error (s), 0 warning (s)
It can be seen that the C standard and the C ++ standard are not completely consistent, especially some C language proprietary functions such as malloc alloc and other processing methods are very different.

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.