wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> C or C++?
(Message started by: Eigenray on Apr 27th, 2003, 1:40pm)

Title: C or C++?
Post by Eigenray on Apr 27th, 2003, 1:40pm
Write a program that will print "C" if compiled as an (ANSI) C program, and "C++" if compiled as a C++ program.

Title: Re: C or C++?
Post by Eigenray on Nov 5th, 2003, 6:35am
Wow Eigenray, what a great problem!  After working on it nonstop for 6 months, I finally came up with this:

$ cat foo.c
#include <stdio.h>
int main() {[hide]
int i=1//*
; //*/2;
printf("C%s\n",i?"++":"");[/hide]
}

$ gcc -ansi -o C foo.c; ./C
C
$ g++ -o C++ foo.c; ./C++
C++

Title: Re: C or C++?
Post by towr on Nov 5th, 2003, 7:30am
hehe..

I think I have a solution somewhere in my mail-box, if you really want another one..
But personally I don't really like the [hide]comment-dependent[/hide] solution.. Since that depends a lot on proper implementation of the compiler..

I wonder if there's another kind of solution..

Title: Re: C or C++?
Post by Dudidu on Nov 5th, 2003, 10:11am

on 11/05/03 at 06:35:21, Eigenray wrote:
Wow Eigenray, what a great problem!  After working on it nonstop for 6 months...
Eigenray you are absolutly right ;), this is a very nice solution (and a problem).
As towr suggested this solution (without connection to how beautiful it is) might be a little bit problematic in real compilers since many of them do not follow exactly the standards of these languages. Nevertheless, Well done !!!

Quote:
I think I have a solution somewhere in my mail-box, if you really want another one..
towr, if you have another solution, now it's a good time to show it :D.

Title: Re: C or C++?
Post by towr on Nov 5th, 2003, 11:35am
I can't find it anymore, but it worked pretty much the same way..

here are two that should work, if the compilers work correctly (which they don't of course)

int main()
{
 // ANSI/ISO c++ has to define __cplusplus
 // unfortunately gcc seems to do it as well, while it shouldn't

#ifndef __cplusplus
 printf("C\n");
#else
 printf("C++\n");
#endif
}


int main()
{
 // sizeof('c') is supposed to be sizeof(int)=4 in C,
 //   and 1 in C++, but guess what, it's so with gcc

 if(sizeof('c') == 4)
   printf("C\n");
 else
   printf("C++\n");
}

Title: Re: C or C++?
Post by Barukh on Nov 6th, 2003, 2:32am
In my opinion, both Eigenray's and towr's second solution are perfectly right. The point is - they give the correct solution if the compilers do what they should do (by the book).

Interestingly enough, the 3rd edition of Stroustrup's "C++ Programming Language" (1999) in section B.2.1 "Silent Differences" mentions 2 essential differences that actually coincide with here listed solutions.

Excellent problem!  :D

Title: Re: C or C++?
Post by Margit on Nov 7th, 2003, 10:29am
gcc foo.c -o C
g++ foo.c -o C++

#include        <stdio.h>
#include        <string.h>
main(int argc, char **argv)
{
       char    *s;

       if ( (s = strrchr(argv[0], '/')) != NULL )
       {
               s++;
       } else {
               s = argv[0];
       }
       printf("%s\n", s);
}

Title: Re: C or C++?
Post by Margit on Nov 7th, 2003, 11:02am
Or even :
gcc -ansi -DITSC foo2.c -o testit
./testit
C
g++ -DITSCPP foo2.c -o testit
./testit
C++
#include        <stdio.h>
main()
{
#ifdef  ITSC
       printf("C\n");
#endif
#ifdef  ITSCPP
       printf("C++\n");
#endif
}

Title: Re: C or C++?
Post by Margit on Nov 8th, 2003, 3:36am
Hmm, this is interesting. What if we have to pass our prog to an unknown wrapper such as :
comp-c-or-cplus foo.c
So that we only know that we get a foo executable.
At any point in time, the wrapper will call either gcc or g++.
One of the definitive differences in dialect is the recognition of
operators like "and", "or" etc. in C++.
Wonder if we can exploit that.

Title: Re: C or C++?
Post by rujoshi on Nov 8th, 2003, 7:02pm
Here's another one:
#include <stdio.h>


typedef struct {
} empty_t;


int main() {
  char* s = sizeof(empty_t) ? "C++" : "C";
  printf("%s\n", s);
}

Title: Re: C or C++?
Post by Margit on Nov 9th, 2003, 3:04am
Ah Rujoshi that's brilliant.
I even think that the behaviour cannot be changed
by any compile option.
The "definitive" answer.

Title: Re: C or C++?
Post by TenaliRaman on Nov 20th, 2003, 10:00pm
rujoshi,
all is fine with your solution except that it would generate a compile time error in C (not in C++ tho). In gcc however it will run with an exit code 2.

Nevertheless, its a brilliant solution !!!  8)

Title: Re: C or C++?
Post by rujoshi on Nov 23rd, 2003, 12:57pm
Hmm..yes, it doesn't work on a Sun..So here's another solution, which uses the
fact that C++ does  name mangling and C does not.


// FindLang.c: compile using cc -o FLC.o FindLang.c
char* FindLang() {
    return "C";
}

// FindLang.cpp: compile using c++ -o FLCPP.o FindLang.cpp
char* FindLang() {
     return "C++";
}

// main.c: compile using cc/c++ -o main main.c FLC.o FLCPP.o
#include <stdio.h>
char* FindLang();

int main() {
    printf("%s\n", FindLang());
}


This works on both Linux and Sun.

Title: Re: C or C++?
Post by rujoshi on Nov 23rd, 2003, 1:20pm
Another one I found at http://www.usenix.org/publications/login/1997-12/cplusplus.html:


/* corcpp.c */
#include <stdio.h>
char x;

int main() {
    struct x { int xx[5]; };
    char* s = (sizeof(x) == sizeof(char)) ? "C" : "C++";
    printf ("%s\n", s);
}

Title: Re: C or C++?
Post by TenaliRaman on Nov 23rd, 2003, 9:50pm
Rahul,
that's simply great solution at that site.
(i must say i never knew that trick!!)



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board