Hello World problems 
Author Message
 Hello World problems

I am trying to write a Hello World program that compiles and runs
successfully using a Java or a C compiler, just to see if I can.

Here's what I tried:

/**\
/int main(void) {
  puts("Hello world");
  return 0;

Quote:
}

/**\
/
#if 0
*/
class HelloWorld {
  public static void main (String args[]) {
    System.out.print("Hello World ");
    }
  }
Quote:
}

/**\
/

#endif
/**/

This code relies on the fact that comments do not nest in C or in Java,
and that C removes the line break if a line ends with a backslash but
Java does not (as far as I know).

However, when I try to compile this code, I get errors like "undefined
type, found 'class'."  Evidently, the preprocessor isn't removing the
Java code.

Can someone please explain to me why this code is not legal C?  Thank you.

-Peter



Fri, 30 Jul 2004 14:01:38 GMT  
 Hello World problems

Quote:
> I am trying to write a Hello World program that compiles and runs
> successfully using a Java or a C compiler, just to see if I can.

                                  ^^^^^^

Bad habbit, you must give up with it.

Quote:

> Here's what I tried:

#include <stdio.h>

Quote:

> /**\
> /int main(void) {
>   puts("Hello world");
>   return 0;
> }

*/

Quote:
> /**\
> /
> #if 0
> */

The above is commented out, thus useless.

/*

Quote:
> class HelloWorld {
>   public static void main (String args[]) {
>     System.out.print("Hello World ");
>     }
>   }
> }

*/

Quote:
> /**\
> /

> #endif
> /**/

The above is commented out too.

--

Ioannis

* Ioannis Vranos
* Programming pages: http://www.noicys.d2g.com
* Alternative URL: http://run.to/noicys



Fri, 30 Jul 2004 14:30:23 GMT  
 Hello World problems

Quote:
> I am trying to write a Hello World program that compiles and runs
> successfully using a Java or a C compiler, just to see if I can.
> Here's what I tried:
> /**\
> /int main(void) {
>   puts("Hello world");
>   return 0;
> }
> /**\
> /
> #if 0
> */
> class HelloWorld {
>   public static void main (String args[]) {
>     System.out.print("Hello World ");
>     }
>   }
> }
> /**\
> /
> #endif
> /**/
> This code relies on the fact that comments do not nest in C or in Java,
> and that C removes the line break if a line ends with a backslash but
> Java does not (as far as I know).
> However, when I try to compile this code, I get errors like "undefined
> type, found 'class'."  Evidently, the preprocessor isn't removing the
> Java code.
> Can someone please explain to me why this code is not legal C?  Thank you.

This IS legal C as far as I could see by checking with gcc version
egcs-2.91.66. It is not, however, legal Java - you have more closing
braces than you have opening ones in that Java code.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Make money fast! Don't feed it!"
   - Anon



Fri, 30 Jul 2004 14:36:58 GMT  
 Hello World problems

Quote:


>> Here's what I tried:
> #include <stdio.h>

And how would a Java compiler interpret this, then?

Quote:

>> /**\
>> /int main(void) {
>>   puts("Hello world");
>>   return 0;
>> }
> */
>> /**\
>> /
>> #if 0
>> */
> The above is commented out, thus useless.

Are you sure?

Quote:
> /*
>> class HelloWorld {
>>   public static void main (String args[]) {
>>     System.out.print("Hello World ");
>>     }
>>   }
>> }
> */
>> /**\
>> /

>> #endif
>> /**/
> The above is commented out too.

Are you sure?

Doesn't the C preprocessor concatenate lines that end in a backslash
('\') with the next line? Therefore, for example, the above bit would
be seen as:

/**/

#endif
/**/

which does not comment out the #endif directive.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"You have moved your mouse, for these changes to take effect you must shut down
and restart your computer. Do you want to restart your computer now?"
   - Karri Kalpio



Fri, 30 Jul 2004 14:39:30 GMT  
 Hello World problems
On Mon, 11 Feb 2002 08:30:23 +0200, "Ioannis Vranos"

Quote:



>> I am trying to write a Hello World program that compiles and runs
>> successfully using a Java or a C compiler, just to see if I can.
>                                  ^^^^^^

>Bad habbit, you must give up with it.

?

Quote:

>> Here's what I tried:

>#include <stdio.h>

>> /**\
>> /

#include <stdio.h>

Put it here, otherwise it's part of the Java program.

Quote:
>>int main(void) {
>>   puts("Hello world");
>>   return 0;
>> }
>*/

>> /**\
>> /
>> #if 0
>> */

>The above is commented out, thus useless.

No, it's not.  Look closer.

Quote:

>/*
>> class HelloWorld {
>>   public static void main (String args[]) {
>>     System.out.print("Hello World ");
>>     }
>>   }
>> }
>*/

>> /**\
>> /

>> #endif
>> /**/

>The above is commented out too.

Ditto.

Peter, this compiles without warnings (after supplying the missing
prototype for puts()) under gcc 2.95.2-6.  Which compiler is rejecting
it?

Russ



Fri, 30 Jul 2004 14:45:05 GMT  
 Hello World problems

wrote in comp.lang.c:

Quote:
> I am trying to write a Hello World program that compiles and runs
> successfully using a Java or a C compiler, just to see if I can.

> Here's what I tried:

> /**\
> /int main(void) {
>   puts("Hello world");
>   return 0;
> }
> /**\
> /
> #if 0
> */
> class HelloWorld {
>   public static void main (String args[]) {
>     System.out.print("Hello World ");
>     }
>   }
> }
> /**\
> /

> #endif
> /**/

> This code relies on the fact that comments do not nest in C or in Java,
> and that C removes the line break if a line ends with a backslash but
> Java does not (as far as I know).

> However, when I try to compile this code, I get errors like "undefined
> type, found 'class'."  Evidently, the preprocessor isn't removing the
> Java code.

> Can someone please explain to me why this code is not legal C?  Thank you.

> -Peter

The only illegal thing I see about your code is the lack of a
prototype for puts(), which would make it illegal with a C99
conforming compiler.  For earlier versions, since puts() takes an int
and you are calling it with a pointer to char, the correct argument
type.

The exact sequence of preprocessor operations is not always followed
to the letter, and this almost always only shows up when trying to do
excessively tricky stuff like this.

I tried three compilers that preprocessed this correctly and one (very
old one) that ended up outputting the Java code and not the C code.

Try another compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Fri, 30 Jul 2004 15:03:42 GMT  
 Hello World problems
On Mon, 11 Feb 2002 08:30:23 +0200, "Ioannis Vranos"

Quote:


> > I am trying to write a Hello World program that compiles and runs
> > successfully using a Java or a C compiler, just to see if I can.
>                                   ^^^^^^

> Bad habbit, you must give up with it.

> > Here's what I tried:

> #include <stdio.h>

Required under C99, to have a prototype in scope, but not required
under any earlier standard (including K&R 1).  puts() takes a pointer
to char as its one and only argument, and returns an int.  Such a
function does not require a prototype or declaration prior to C99 as
long as it is called with a pointer to char argument.

Quote:

> > /**\
> > /int main(void) {
> >   puts("Hello world");
> >   return 0;
> > }
> */

> > /**\
> > /
> > #if 0
> > */

> The above is commented out, thus useless.

No it is not.  Lines continued with the back slash character are
spliced together in translation phase 2.  Comments are recognized and
replaced with a single white space character in phase 3.

Quote:
> /*
> > class HelloWorld {
> >   public static void main (String args[]) {
> >     System.out.print("Hello World ");
> >     }
> >   }
> > }
> */

> > /**\
> > /

> > #endif
> > /**/

> The above is commented out too.

Again, no, and for the same reason.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Fri, 30 Jul 2004 15:09:36 GMT  
 Hello World problems

Quote:

> On Mon, 11 Feb 2002 08:30:23 +0200, "Ioannis Vranos"



> >> I am trying to write a Hello World program that compiles and runs
> >> successfully using a Java or a C compiler, just to see if I can.
> >                                  ^^^^^^

> >Bad habbit, you must give up with it.

> ?

> >> Here's what I tried:

> >#include <stdio.h>

> >> /**\
> >> /

> #include <stdio.h>

> Put it here, otherwise it's part of the Java program.

> >>int main(void) {
> >>   puts("Hello world");
> >>   return 0;
> >> }
> >*/

> >> /**\
> >> /
> >> #if 0
> >> */

> >The above is commented out, thus useless.

> No, it's not.  Look closer.

> >/*
> >> class HelloWorld {
> >>   public static void main (String args[]) {
> >>     System.out.print("Hello World ");
> >>     }
> >>   }
> >> }
> >*/

> >> /**\
> >> /

> >> #endif
> >> /**/

> >The above is commented out too.

> Ditto.

> Peter, this compiles without warnings (after supplying the missing
> prototype for puts()) under gcc 2.95.2-6.  Which compiler is rejecting
> it?

> Russ

I managed to make it compile with gcc 2.95.2 by specifying the
-traditional option.

Years of conditioning have taught me to never suspect the compiler.  Now
I know better.

Thanks for your help, everyone!

-Peter



Sat, 31 Jul 2004 03:00:55 GMT  
 Hello World problems


Quote:
>I am trying to write a Hello World program that compiles and runs
>successfully using a Java or a C compiler, just to see if I can.

>Here's what I tried:

>/**\
>/int main(void) {

   int puts(const char *);  /* For C99 */

Quote:
>  puts("Hello world");
>  return 0;
>}
>/**\

Just a thought, make sure there is no white-space after the \

Quote:
>/
>#if 0
>*/
>class HelloWorld {
>  public static void main (String args[]) {
>    System.out.print("Hello World ");
>    }
>  }
>}
>/**\
>/

>#endif
>/**/

--
-----------------------------------------


-----------------------------------------


Wed, 04 Aug 2004 22:30:29 GMT  
 Hello World problems
Quote:



> >I am trying to write a Hello World program that compiles and runs
> >successfully using a Java or a C compiler, just to see if I can.

> >Here's what I tried:

> >/**\
> >/int main(void) {

>    int puts(const char *);  /* For C99 */

Nopes. Only and only #include <stdio.h>
of course at the right space however.
Quote:

> >  puts("Hello world");
> >  return 0;
> >}
> >/**\

> Just a thought, make sure there is no white-space after the \

> >/
> >#if 0
> >*/
> >class HelloWorld {
> >  public static void main (String args[]) {
> >    System.out.print("Hello World ");
> >    }
> >  }
> >}
> >/**\
> >/

> >#endif
> >/**/



Thu, 05 Aug 2004 10:06:02 GMT  
 Hello World problems

Quote:



>> >I am trying to write a Hello World program that compiles and runs
>> >successfully using a Java or a C compiler, just to see if I can.

>> >Here's what I tried:

>> >/**\
>> >/int main(void) {

>>    int puts(const char *);  /* For C99 */
>Nopes. Only and only #include <stdio.h>
>of course at the right space however.

There's nothing wrong with doing this. So long as the fn has a
prototype in scope, thats all that C requires. Of course if you
require macro definitions or other stuff from the header, you're
correct, and in any events its better to be safe than sorry.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>



Thu, 05 Aug 2004 17:08:54 GMT  
 Hello World problems
On 16 Feb, in article


Quote:




>> >I am trying to write a Hello World program that compiles and runs
>> >successfully using a Java or a C compiler, just to see if I can.

>> >Here's what I tried:

>> >/**\
>> >/int main(void) {

>>    int puts(const char *);  /* For C99 */
>Nopes. Only and only #include <stdio.h>
>of course at the right space however.

The important thing is to have a valid declaration in scope.
#include <stdio.h> is one way of achieving this. I demonstrated another
valid way.

Quote:

>> >  puts("Hello world");
>> >  return 0;
>> >}

--
-----------------------------------------


-----------------------------------------


Thu, 05 Aug 2004 21:43:13 GMT  
 Hello World problems

Quote:





> >> >I am trying to write a Hello World program that compiles and runs
> >> >successfully using a Java or a C compiler, just to see if I can.

> >> >Here's what I tried:

> >> >/**\
> >> >/int main(void) {

> >>    int puts(const char *);  /* For C99 */
> >Nopes. Only and only #include <stdio.h>
> >of course at the right space however.

> There's nothing wrong with doing this. So long as the fn has a
> prototype in scope, thats all that C requires. Of course if you
> require macro definitions or other stuff from the header, you're
> correct, and in any events its better to be safe than sorry.

But, puts can be implemented as a macro and this is the reason why  I
believe so
#define puts(x) fputs(x, stdout);

Besides, if one goes through the header files one can see there is lot
of extra junk that is written there along with the declartions. My
header file has something of the sort of
_CRTIMP int __cdecl puts(const char *);

I don't know what exactly are the meaning of the extra words. But, is
this not a good enough reason to do this way?.



Fri, 06 Aug 2004 03:28:22 GMT  
 Hello World problems

Quote:





>> >>    int puts(const char *);  /* For C99 */
>> >Nopes. Only and only #include <stdio.h>
>> >of course at the right space however.

>> There's nothing wrong with doing this. So long as the fn has a
>> prototype in scope, thats all that C requires. Of course if you
>> require macro definitions or other stuff from the header, you're
>> correct, and in any events its better to be safe than sorry.

>But, puts can be implemented as a macro and this is the reason why  I
>believe so
>#define puts(x) fputs(x, stdout);

in which case, worry not, the linker will complain and your program
won't be built.

Quote:
>Besides, if one goes through the header files one can see there is lot
>of extra junk that is written there along with the declartions. My
>header file has something of the sort of
>_CRTIMP int __cdecl puts(const char *);

For C, thats an illegal definition of puts, unless both _CRTIMP and
__cdecl are defined as empty macros.

Quote:
>I don't know what exactly are the meaning of the extra words. But, is
>this not a good enough reason to do this way?.

Like I said, getting macros defined may be useful. Or not.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>



Fri, 06 Aug 2004 07:04:01 GMT  
 Hello World problems
* Minti
| But, puts can be implemented as a macro and this is the reason why  I
| believe so
| #define puts(x) fputs(x, stdout);

It may be implemented as a macro but there must be a real puts()
function as well.

Jeremy.



Sun, 08 Aug 2004 04:55:09 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Hello World Problem

2. Hello world problems again

3. problem in Hello World example

4. Problem with Hello World !

5. a hello world encryption

6. Canocical hello world

7. The Program Only Harder Then Hello World...

8. Hello World not working

9. Newbie Question about Hello World

10. Hello World

11. Hello world without semicolon

12. Hello World

 

 
Powered by phpBB® Forum Software