wu :: forums
« wu :: forums - C or C++? »

Welcome, Guest. Please Login or Register.
Apr 26th, 2024, 7:06pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: ThudnBlunder, SMQ, towr, Grimbal, Eigenray, william wu, Icarus)
   C or C++?
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: C or C++?  (Read 3617 times)
Eigenray
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 1948
C or C++?  
« on: Apr 27th, 2003, 1:40pm »
Quote Quote Modify Modify

Write a program that will print "C" if compiled as an (ANSI) C program, and "C++" if compiled as a C++ program.
IP Logged
Eigenray
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 1948
Re: C or C++?  
« Reply #1 on: Nov 5th, 2003, 6:35am »
Quote Quote Modify Modify

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() {
int i=1//*
; //*/2;
printf("C%s\n",i?"++":"");

}
 
$ gcc -ansi -o C foo.c; ./C
C
$ g++ -o C++ foo.c; ./C++
C++
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: C or C++?  
« Reply #2 on: Nov 5th, 2003, 7:30am »
Quote Quote Modify Modify

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 comment-dependent solution.. Since that depends a lot on proper implementation of the compiler..
 
I wonder if there's another kind of solution..
« Last Edit: Nov 5th, 2003, 7:32am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Dudidu
Full Member
***





   


Posts: 227
Re: C or C++?  
« Reply #3 on: Nov 5th, 2003, 10:11am »
Quote Quote Modify Modify

on Nov 5th, 2003, 6:35am, Eigenray wrote:
Wow Eigenray, what a great problem!  After working on it nonstop for 6 months...
Eigenray you are absolutly right Wink, 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 Cheesy.
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: C or C++?  
« Reply #4 on: Nov 5th, 2003, 11:35am »
Quote Quote Modify Modify

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");
}
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Barukh
Uberpuzzler
*****






   


Gender: male
Posts: 2276
Re: C or C++?  
« Reply #5 on: Nov 6th, 2003, 2:32am »
Quote Quote Modify Modify

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!  Cheesy
IP Logged
Margit
Guest

Email

Re: C or C++?  
« Reply #6 on: Nov 7th, 2003, 10:29am »
Quote Quote Modify Modify Remove Remove

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);
}
IP Logged
Margit
Guest

Email

Re: C or C++?  
« Reply #7 on: Nov 7th, 2003, 11:02am »
Quote Quote Modify Modify Remove Remove

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
}
IP Logged
Margit
Guest

Email

Re: C or C++?  
« Reply #8 on: Nov 8th, 2003, 3:36am »
Quote Quote Modify Modify Remove Remove

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.
IP Logged
Rahul
Newbie
*



Rahul

   


Gender: male
Posts: 3
Re: C or C++?  
« Reply #9 on: Nov 8th, 2003, 7:02pm »
Quote Quote Modify Modify

Here's another one:
#include <stdio.h>
 

typedef struct {
} empty_t;

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

}
IP Logged
Margit
Guest

Email

Re: C or C++?  
« Reply #10 on: Nov 9th, 2003, 3:04am »
Quote Quote Modify Modify Remove Remove

Ah Rujoshi that's brilliant.  
I even think that the behaviour cannot be changed
by any compile option.
The "definitive" answer.
IP Logged
TenaliRaman
Uberpuzzler
*****



I am no special. I am only passionately curious.

   


Gender: male
Posts: 1001
Re: C or C++?  
« Reply #11 on: Nov 20th, 2003, 10:00pm »
Quote Quote Modify Modify

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 !!!  Cool
IP Logged

Self discovery comes when a man measures himself against an obstacle - Antoine de Saint Exupery
Rahul
Newbie
*



Rahul

   


Gender: male
Posts: 3
Re: C or C++?  
« Reply #12 on: Nov 23rd, 2003, 12:57pm »
Quote Quote Modify Modify

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.
« Last Edit: Nov 23rd, 2003, 1:00pm by Rahul » IP Logged
Rahul
Newbie
*



Rahul

   


Gender: male
Posts: 3
Re: C or C++?  
« Reply #13 on: Nov 23rd, 2003, 1:20pm »
Quote Quote Modify Modify

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);
}
« Last Edit: Nov 23rd, 2003, 1:20pm by Rahul » IP Logged
TenaliRaman
Uberpuzzler
*****



I am no special. I am only passionately curious.

   


Gender: male
Posts: 1001
Re: C or C++?  
« Reply #14 on: Nov 23rd, 2003, 9:50pm »
Quote Quote Modify Modify

Rahul,
that's simply great solution at that site.
(i must say i never knew that trick!!)
IP Logged

Self discovery comes when a man measures himself against an obstacle - Antoine de Saint Exupery
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

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