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; } 

I can not find the answer in google, the gets () function is a function of the C or C ++ language? Because at university I should use only C ++ functions.

+2
source share
5 answers

gets() is a C function dating back to the 1960s, it does not check boundaries and is considered dangerous, it has been stored all these years for compatibility and nothing more.

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; } 

You should avoid combining special C functions with C ++ functions as a data type / string object. There are ways to use both, but as a beginner you should stick with one or the other.

My personal approval, first C, and then switching to C ++. Most C ++ programmers do not work well in pure C, the C language first appeared and was used as the basis for C ++, but both grew over time in many ways that you can imagine.

So, if you do not study the orientation of objects at the same time as C ++, all you do is C code with a C ++ compiler. C ++ is also very large compared to C. The patterns and object-oriented programming tools that are the reasons for using C ++ in the first place.

Pure C is still great for many things, small and elegant. It's easier to master C than C ++. C ++ has been greatly improved so as not to succumb to a subset of the functions agreed upon by any development team.

+1
source

C functions are a subset of C ++ functions, but no, you probably don't want gets() in a C ++ project.

You can consider getline() or operator>> for a stream. Didn’t they tell you about it at the university?

+4
source

gets is a c-function

You are probably looking for istream / ostream / fstream etc.

See for example: http://www.cplusplus.com/reference/iostream/istream/read/

+1
source

gets is a c-function. The first google for link gets . You should probably look at the functions in iostream, fstream, etc.

+1
source

This example will not compile because the header for get is cstdlib and its function c.

0
source

All Articles