是得到()被认为是C函数或C ++函数? [英] Is gets() considered a C function or a C++ function?
问题描述
#include <iostream>
using namespace std;
void main(){
char name[20];
gets(name);
cout<<name<<endl;
}
我无法找到答案的谷歌,函数获得()是C或C ++语言的功能?因为在大学,我只能使用C ++函数。
I can't found answer in google, function gets() is C or C++ language function ? Because in university I must use only C++ functions.
推荐答案
获得()
是一个C函数可追溯至20世纪60年代,它不会做边界检查,是认为是危险的,是一直都保持这几年的兼容性而不是其他。
gets()
is a C function dating to the 1960's, it does not do bounds checking and is considered dangerous, is has been kept all this years for compatibility and nothing else.
在您的有效code和建议C ++应该是:
Your code in valid and recommended C++ should be:
#include <iostream>
using namespace std;
int main(){
// C style NULL terminated string NOT the same as a C++ string datatype
//char name[20];
string name;// C++ string datatype, meant to use with C++ functions and features
cin >> name;
cout<<name<<endl;
return 0;
}
您应避免与C ++的功能,作为字符串数据类型/对象混合C特定功能。有办法同时使用,但作为初学者,你应该坚持一个或另一个。
You should avoid to mix C specific features with C++ features as the string datatype/object. There are ways to use both, but as beginner you should stick to one or the other.
我个人recomendation,做C首先,ADN再过渡到C ++。大多数C ++程序员在纯C坏,C语言来第一次,并作为C ++的基础环境,但两人都在很多方面,你可以想像随着时间的推移分道扬镳。
My personal recomendation, do C first, adn then transition to C++. Most C++ programmers are bad at pure C, the C language came first, and was used as the foundation environment for C++, but both have grown apart with time in more ways that you can imagine.
所以,除非你是simultaneosly用C ++学习面向对象,所有你需要做的是code用C与C ++编译器。 C ++也非常大,比较C.模板和面向对象编程设施作为原因首先使用C ++。
So unless you are studying object orientation simultaneosly with C++, all you will do is code in C with a C++ compiler. C++ is also very large in comparison to C. Templates and Object Oriented Programming facilities being the reasons to use C++ in the first place.
用纯C仍然是伟大的很多事情,是小而优雅。它更容易获得熟练位于C比C ++。 C ++已经成长为大可管理不拘一格的任何开发者团队同意功能的子集。
Pure C is still great for many things, is small and elegant. Its easier to get proficient at C than C++. C++ has grown to much to be manageable without sticking to a subset of features agreed by any developer team.
这篇关于是得到()被认为是C函数或C ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!