C语言与C++不同之函数定义

C 语言与C++在基本语法方面大部分是一致兼容的,但是在函数定义与调用方面还保持着差别,请看如下代码:

C语言与C++不同之函数定义 #include  " stdafx.h "
C语言与C++不同之函数定义
int  subtest(x,y)
C语言与C++不同之函数定义
int   * x, * y;
C语言与C++不同之函数定义
{
C语言与C++不同之函数定义
int z;
C语言与C++不同之函数定义z
=*x>*y?*x:*y;
C语言与C++不同之函数定义
return (z);
C语言与C++不同之函数定义
C语言与C++不同之函数定义
C语言与C++不同之函数定义}

C语言与C++不同之函数定义
int  main( int  argc,  char *  argv[])
C语言与C++不同之函数定义
{
C语言与C++不同之函数定义    
//printf("Hello World! ");
C语言与C++不同之函数定义
    int a=10;
C语言与C++不同之函数定义    
int b=20;
C语言与C++不同之函数定义    printf(
"%d ",subtest(&a,&b));
C语言与C++不同之函数定义    
return 0;
C语言与C++不同之函数定义}

 

这种方法称为传统的对形参的声明方式,如下是C++标准允许的现代形参声明方式:

 

C语言与C++不同之函数定义 #include  " stdafx.h "
C语言与C++不同之函数定义
int  subtest(x,y)
C语言与C++不同之函数定义
int   * x, * y;
C语言与C++不同之函数定义
{
C语言与C++不同之函数定义
int z;
C语言与C++不同之函数定义z
=*x>*y?*x:*y;
C语言与C++不同之函数定义
return (z);
C语言与C++不同之函数定义
C语言与C++不同之函数定义
C语言与C++不同之函数定义}

C语言与C++不同之函数定义
int  main( int  argc,  char *  argv[])
C语言与C++不同之函数定义
{
C语言与C++不同之函数定义    
//printf("Hello World! ");
C语言与C++不同之函数定义
    int a=10;
C语言与C++不同之函数定义    
int b=20;
C语言与C++不同之函数定义    printf(
"%d ",subtest(&a,&b));
C语言与C++不同之函数定义    
return 0;
C语言与C++不同之函数定义}

 

在VC6.0中*.c的文件前一种是可以的,*.cpp的前一种不可以,报错如下:

F:/testfun/testfun.cpp(5) : error C2065: 'x' : undeclared identifier
F:/testfun/testfun.cpp(5) : error C2065: 'y' : undeclared identifier
F:/testfun/testfun.cpp(6) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
F:/testfun/testfun.cpp(6) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

testfun.exe - 4 error(s), 0 warning(s)
由此可见c标准与c++标准还不是完全一致,特别是一些c语言专有的函数如 malloc alloc 等其处理方法很是不同。


你可能感兴趣的:(C++)