C语言与C++不同之函数定义
2006-08-29 09:59
399 查看
C 语言与C++在基本语法方面大部分是一致兼容的,但是在函数定义与调用方面还保持着差别,请看如下代码:
#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;
}
这种方法称为传统的对形参的声明方式,如下是C++标准允许的现代形参声明方式:
#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;
}
在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 等其处理方法很是不同。
#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;
}
这种方法称为传统的对形参的声明方式,如下是C++标准允许的现代形参声明方式:
#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;
}
在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语言与C++不同之函数定义
- C++中函数定义返回类型和内部实现返回类型不同的原因
- 定义函数的指针,C++和C不同是因为函数重载
- C语言引用C++定义的函数
- C++ 类中不同类型成员函数的定义
- C++中函数定义返回类型和内部实现返回类型不同的原因
- 关于C++中定义比较函数的三种方法小结
- C++程序中不同的函数调用方式
- 你好,C++(24)好大一个箱子!5.1.1 函数的声明和定义
- boj problem 1331 思路:快排分块 按块查找 问题:全局数组时可以定义到500W的 main函数下的数组不行~ 另外c语言比c++快 TLE可以考虑C~~具体原因待解决
- C++中为什么构造函数不能定义为虚函数
- c 函数指针的定义 和 C++回调函数的使用
- C/C++不同函数调用方式(在汇编下调试)总结
- C++经验总结(2):函数被多次定义
- K&R C 传统C语言的函数定义
- C与C++中定义结构体的不同方式
- 托管C++函数定义输出参数(ref)
- C++函数重定义、重载、重写的区别
- c++ 中默认参数的函数声明和定义的注意事项
- 深入解析C语言中函数指针的定义与使用