"Hello, World", Even before it runs

-3

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.

Matthew Roh

Posted 2017-07-05T09:12:25.403

Reputation: 5 043

9Wouldn'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 – 2017-07-05T09:15:18.373

4"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 – 2017-07-05T09:22:02.917

1I would say, better ask the “Hello, World!” to appear both when compiling and when running. That would make clear what is valid code. – manatwork – 2017-07-05T09:25:46.570

@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 – 2017-07-05T09:26:26.510

I have to agree with @MartinEnder. Almost all programming languages with a compiler will output the incorrect source code with an error class/function/something is missing. Just take my posted Java answer for example. This would be a valid answer in almost all programming languages with a compiler.. So although I barely do this, I give this a -1 vote for being boring, straight-to-the-point in more than 90% of the the compiler languages, and kinda pointless challenge. Sorry.. – Kevin Cruijssen – 2017-07-05T09:26:55.420

1Are custom file names allowed? – Okx – 2017-07-05T10:53:12.450

Does the code have to compile to an executable or is an object file enough? – Dennis – 2017-07-05T21:02:23.827

This would've been cool, but you didn't spend much time on the question :(. – Magic Octopus Urn – 2017-07-11T21:26:06.863

Answers

5

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'

LiefdeWen

Posted 2017-07-05T09:12:25.403

Reputation: 3 381

1OP has updated that errors are not allowed. Just use #warning instead. – TheLethalCoder – 2017-07-05T09:33:30.690

3

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]

Dennis

Posted 2017-07-05T09:12:25.403

Reputation: 196 637

1Weird, I swear I tried this and it didn't work for me. Have an upvote – musicman523 – 2017-07-05T18:53:29.577

2

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

manatwork

Posted 2017-07-05T09:12:25.403

Reputation: 17 865

1

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!

nimi

Posted 2017-07-05T09:12:25.403

Reputation: 34 639

1

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.

Dada

Posted 2017-07-05T09:12:25.403

Reputation: 8 279

0

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

Arnauld

Posted 2017-07-05T09:12:25.403

Reputation: 111 334

Can't you use puts for -2? – Erik the Outgolfer – 2017-07-05T14:54:37.837

@EriktheOutgolfer With my version of GCC at least, puts() would compile without a warning. – Arnauld – 2017-07-05T14:56:13.353

Can you use f instead of main? – musicman523 – 2017-07-05T15:26:07.000

@musicman523 Would a C program without a main() compile at all? – Arnauld – 2017-07-05T17:29:08.217

@Arnauld You could use the -c flag to compile without linking, it would just be unexecutable. – musicman523 – 2017-07-05T18:29:40.037

0

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!

musicman523

Posted 2017-07-05T09:12:25.403

Reputation: 4 472

-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 – 2017-07-05T22:53:04.920

0

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"}

musicman523

Posted 2017-07-05T09:12:25.403

Reputation: 4 472