-2

I have some code, and I can't work out if it is written in C or C++.

According to this article:

I believe that it is C, as there are functions which are declared after use eg.

void main() {
    foo();
}

void foo() {
    printf( "Hello world" );
}

However, it does have a native boolean type - according to the website above:

C does not provide a native boolean type. You can simulate it using an enum, though: typedef enum {FALSE, TRUE} bool;

Any idea if the code would be C, C++ or something else? Or any common things to look out for?

In addition, when I declare a variable outside a function then I have to write:

int test;

And then assign it a value in a function, instead of being able to do:

int test = 5;
Ethan
  • 3,410
  • 1
  • 28
  • 49
  • If you're writing C++, then you're making use of it's object-oriented and template features and that's nothing like what you would see in C. – O2Addict Oct 31 '17 at 22:26
  • 1
    declaration after use is bad in c and c++, the difference being that c will compile it with a warning, c++ will fatal it – pm100 Oct 31 '17 at 22:26
  • it's probably C, since `foo` has a default prototype (still bad practice) – Jean-François Fabre Oct 31 '17 at 22:26
  • 1
    what does "However, it does have a native boolean type." mean? – pm100 Oct 31 '17 at 22:26
  • @pm100 see updated question – Ethan Oct 31 '17 at 22:29
  • 1
    Doesnt make it any clearer, your code makes not mention of 'bool' or anything like it – pm100 Oct 31 '17 at 22:30
  • 2
    An enum named "bool" would not compile in C++, since it's a reserved keyword. If you see any C++ keywords used as variable names, it's definitely C and cannot be valid C++. (Excluding the contextual keywords in c++ like "override" which only has meaning when declaring a function, and elsewhere is still allowed to be an ordinary identifier name.) – Chris Uzdavinis Oct 31 '17 at 22:32
  • Actually, C99 does have [a native boolean type](https://stackoverflow.com/questions/1608318/is-bool-a-native-c-type). – Quentin Oct 31 '17 at 22:34
  • If this was C99 or later it would use `_Bool`. – Jesper Juhl Oct 31 '17 at 22:42

2 Answers2

10

It uses implicit function declarations (for foo() and printf(), so if it is anything, it is C89. Implicit function declarations were never in C++, and were removed from C in C99.


To answer the question "how to recognise C or C++", there is a great deal of C++ that isn't valid C, and there is a smaller amount of C that isn't valid C++. So a reasonable level of knowledge of both languages is needed.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • How did you know this!? Also this is likely way more specific than the OP was looking for. But still, +1. – Fantastic Mr Fox Oct 31 '17 at 22:29
  • 2
    You could also mention `void main` which is never c++. – Fantastic Mr Fox Oct 31 '17 at 22:31
  • 1
    @Fantastic Mr Fox - some people actually *read* the standards, and some of us have been doing so for *decades*. – Jesper Juhl Oct 31 '17 at 22:37
  • @Fantastic It's not valid in C either, at least for hosted implementations - https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c –  Oct 31 '17 at 22:38
  • @FantasticMrFox I *think* the same rules apply re. the `void` return type, i.e. it is allowed in a "freestanding" implementation. But I have to check the C89 standard for that. – juanchopanza Oct 31 '17 at 22:42
4

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198