-3

I'm doing an University proyect where professors give us some base code supposedly in C. But inside this C files just see C++ functions like cout instead of printf, vectors from STL, inheritance? This is an example of file_reader.cc:

void read (const char *nombre_archivo_pse, vector <float> &vertices, vector <int> &caras){
   unsigned num_vertices = 0, num_caras = 0;
   ifstream src;
   string na = nombre_archivo_pse;

   if (na.substr (na.find_last_of (".") + 1) != "ply")
      na += ".ply";

   abrir_archivo (na, src);
   leer_cabecera (src, num_vertices, num_caras, true);
   leer_vertices (num_vertices, vertices, src);
   leer_caras (num_vertices, num_caras, caras, src);

   cout << "archivo ply leido." << endl << flush;
}

This shouldn't be file_reader.cpp or file_reader.c++? How I code? In C or C++?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Yerasito
  • 3
  • 2
  • 5
    `.cc` is a very common extension for C++ code. C code usually has the extension `.c`. – Ted Lyngmo Oct 12 '22 at 22:45
  • 1
    Yeah, that's C++, not C. Two different languages. – Tom Karzes Oct 12 '22 at 22:45
  • The extension for C is `.c`, not `.cc`. – Barmar Oct 12 '22 at 22:46
  • Does this answer your question? [What's the difference between C and C++](https://stackoverflow.com/questions/640657/whats-the-difference-between-c-and-c) – tinman Oct 12 '22 at 22:46
  • C code almost universally uses `.c`. C++ code can use `.cc` or `.cpp`. I would recommend against `.c++`, as `+` is an odd character to have in a filename, let alone a file extension. – Silvio Mayolo Oct 12 '22 at 22:46
  • 1
    @tinman That's the question in their title, it's not the question they actually asked. I changed the title to describe the question. – Barmar Oct 12 '22 at 22:46
  • 2
    The suffix `.c` is universally accepted for C source code (`.h` for C header files). There is no universally accepted suffix for C++ code. The most common ones are `.cpp`, and `.cc`; others are `.cxx`, `.cp`, `.c++`, and `.C`. (The latter can be problematic on case-insensitive file systems, so it should probably be avoided.) – Keith Thompson Oct 12 '22 at 22:49
  • `if (na.substr (na.find_last_of (".") + 1) != "ply") na += ".ply";` Not valid C. `cout << "archivo ply leido." << endl << flush;` Not valid C. This code is C++. – user16217248 Oct 12 '22 at 22:55
  • Since the code is C++, change the `char *` type to `std::string`. The `std::string` can handle run-time sizing, maintains its length, and can be passed by reference. – Thomas Matthews Oct 12 '22 at 23:09
  • The function has an argument `vector &vertices`. That is C++-specific - there is no way the function can be implemented purely in C. – Peter Oct 13 '22 at 03:36

1 Answers1

4

That is C++ code. The file extension for C++ files can be .cc or .cpp.

David
  • 58
  • 5