Warning: This challenge is only valid for languages with a compiler.

Make a program (which is perfectly valid code) that outputs Hello, World, even before runtime.

How? Let me explain.

Many languages have a compiler. Some operations are made in the compile-time, and others, in runtime. You trick the compiler to output the text Hello, World in compile-time. The text can be anywhere, like the build log.(Not errors. sorry for invalidating all those answers. at least theres pragma tho)

You can use C, C++ or C#, but I wouldn't encourage you to. It's simply a game breaker.

p.s. I wrote this challenge because the tag is both unused, and used wrong.

  • 8
    Wouldn't the simple source code Hello, World! be optimal in almost any compiled language where that line is not valid code and the offending line gets printed in an error message? – Martin Ender Jul 5 '17 at 9:15
  • 3
    "which is perfectly valid code" ... "The text can be anywhere, like the build log, an error, anywhere" ... what's "perfectly valid code" then? – Martin Ender Jul 5 '17 at 9:22
  • 1
    I would say, better ask the “Hello, World!” to appear both when compiling and when running. That would make clear what is valid code. – manatwork Jul 5 '17 at 9:25
  • @manatwork Or use two different strings so that you can't just get the line output during compiling that has the string for the actual running. – Martin Ender Jul 5 '17 at 9:26
  • 1
    Are custom file names allowed? – Okx Jul 5 '17 at 10:53

C#, 51 bytes

Try it online

#warning Hello, World
class P{static void Main(){}}

Prints the following to the Error List (but still compiles):

warning CS1030: #warning: `Hello, World'
  • OP has updated that errors are not allowed. Just use #warning instead. – TheLethalCoder Jul 5 '17 at 9:33

C (gcc), 22 18 bytes

Hello, World;main;

This compiles just fine with gcc (and clang), although the program it produces crashes immediately.

Try it online!

Compiler output

gcc 6.3.1 produces the following debugging information.

.code.tio.c:1:1: warning: data definition has no type or storage class
 Hello, World;main;
 ^~~~~
.code.tio.c:1:1: warning: type defaults to ‘int’ in declaration of ‘Hello’ [-Wimplicit-int]
.code.tio.c:1:8: warning: type defaults to ‘int’ in declaration of ‘World’ [-Wimplicit-int]
 Hello, World;main;
        ^~~~~
.code.tio.c:1:14: warning: data definition has no type or storage class
 Hello, World;main;
              ^~~~
.code.tio.c:1:14: warning: type defaults to ‘int’ in declaration of ‘main’ [-Wimplicit-int]

C (gcc), 15 bytes

Hello, World;

If compiling to an object file is allowed, this works just as well. The resulting file simply declares two global variables.

The byte count includes +3 bytes for the compiler flag -c.

Try it online!

Compiler output

hw.c:1:1: warning: data definition has no type or storage class
 Hello, World;
 ^~~~~
hw.c:1:1: warning: type defaults to ‘int’ in declaration of ‘Hello’ [-Wimplicit-int]
hw.c:1:8: warning: type defaults to ‘int’ in declaration of ‘World’ [-Wimplicit-int]
 Hello, World;
        ^~~~~

C (gcc), 20 bytes

main(Hello, World){}

This compiles to a program that exits cleanly.

Try it online!

Compiler output

.code.tio.c:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main(Hello, World){}
 ^~~~
.code.tio.c: In function ‘main’:
.code.tio.c:1:1: warning: type of ‘Hello’ defaults to ‘int’ [-Wimplicit-int]
.code.tio.c:1:1: warning: type of ‘World’ defaults to ‘int’ [-Wimplicit-int]
  • 1
    Weird, I swear I tried this and it didn't work for me. Have an upvote – musicman523 Jul 5 '17 at 18:53

FreePascal, 29 characters

begin{$INFO Hello, World}end.

Maybe other Pascal variants too, not sure whether the $INFO directive is FreePascal only or not.

Sample run:

bash-4.4$ fpc compile-time-message.pas 
Free Pascal Compiler version 3.0.2+dfsg-2 [2017/04/09] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling compile-time-message.pas
User defined: Hello, World
Linking compile-time-message
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
0 lines compiled, 0.0 sec

Haskell, 25 24 bytes

m@main=m

8 bytes from the code plus 16 bytes for the compiler flag -W'Hello, World'.

Gives the warning unrecognised warning flag: -WHello, World and compiles the program.

Depending on the shell (or the absence of a shell) you can omit the single quotes in the flag for two bytes less. TIO works this way.

Haskell, TIO environment, 22 bytes

Try it online!

Edit: @Dennis saved a byte. Thanks!

Perl, 15 bytes

14 bytes of code + -w flag.

"Hello, World"

When ran with -w flag, it produces the compile-time warnings:

$ perl -we '"Hello, World"'
Useless use of a constant ("Hello, World") in void context at -e line 1.

And if you wonder if it really happens at compile time, you can add -c flag, which causes the program to be compiled but not ran, and the warning is still here.

C (gcc), 23 bytes

main(){"Hello, World";}

Try it online!

Gives the warning:

warning: return type defaults to ‘int’ [-Wimplicit-int]
main(){"Hello, World";}
^~~~

C (gcc), 38 bytes

#pragma message"Hello, World"
main(){}

Try it online!

Rather than using compiler warnings, this answer uses a built-in pragma directive that allows printing notes to stdout. Works in C and C++ (as I think all the rest of these do).


C (gcc), 23 + 1 = 24 bytes

#define f"Hello, World"

Try it online!

Prints "Hello, World!" as part of an error message, which says:

warning: ISO C99 requires whitespace after the macro name
 #define f"Hello, World!"
          ^~~~~~~~~~~~~~~

This is a default warning in GCC; no flags need to be added to achieve this.

The extra byte comes from the -c flag, which successfully compiles the program without linking, which means it accepts not having a main function. However, the resulting output file cannot be executed. If this is unacceptable, then:

C (gcc), 31 bytes

#define f"Hello, World"
main=0;

Try it online!

This will run, but will segfault every time. If that's not acceptable, then:

C (gcc), 32 bytes

#define f"Hello, World"
main(){}

Try it online!

  • -c counts as three bytes (one for the space). main=0; is the same as main;. #define Hello, World is shorter than #define f"Hello, World". – Dennis Jul 5 '17 at 22:53

Scala, 36 bytes

object F extends App{"Hello, World"}

Try it online!

Gives the warning:

warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
object F{"Hello, World"}

GNU C, 31 bytes

Nothing clever here. I'm mostly posting it for reference.

main(){printf("Hello, World");}

GCC will throw the following warning at compilation time:

hello.c: In function ‘main’:
hello.c:1:8: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
 main(){printf("Hello, World");}
        ^
  • Can't you use puts for -2? – Erik the Outgolfer Jul 5 '17 at 14:54
  • @EriktheOutgolfer With my version of GCC at least, puts() would compile without a warning. – Arnauld Jul 5 '17 at 14:56
  • Can you use f instead of main? – musicman523 Jul 5 '17 at 15:26
  • @musicman523 Would a C program without a main() compile at all? – Arnauld Jul 5 '17 at 17:29
  • @Arnauld You could use the -c flag to compile without linking, it would just be unexecutable. – musicman523 Jul 5 '17 at 18:29

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.