The code you've shown is definitely not C++ (C++ doesn't allow implicit declarations), although it's not strictly correct C, either. The implicit int
declaration of foo
(which would only be valid in C89) is contradicted by the explicit void
declaration/definition later - any decent C compiler should yell at you about this.
Also, void
isn't a proper return type for main
in either C or C++ (at least in a hosted implementation).
One really easy way to tell if code is C++ is to look at the standard headers. If the headers don't have a trailing .h
, like
#include <iostream>
#include <string>
etc., then you're definitely looking at C++ code. If the code uses the memory allocation operators new
and delete
, then you're definitely looking at C++ code. If you see the scope resolution operator ::
anywhere, you're definitely looking at C++ code. There are a few other C++-specific features (templates, lambdas, etc.) that are not found in C.
C++ doesn't allow variable-length arrays, while C99 and later do - if you see an array declaration where the size is given by a runtime variable, like
int size = 20;
int arr[size];
then you're definitely looking at C99 or later.
Unfortunately, that's where it stops being so easy. C++ can use stdio.h
routines, as well as malloc
and free
for memory management. It's possible to write code that compiles as both C and C++, mainly by avoiding C++-specific syntax and keywords and by sticking with the C standard library.