27

What is the correct Hello World program in C?

Since the first page of Google results for "c hello world" vary greatly and many are old C, I would like the standard version in one place for easy copy and paste.

4
  • 41
    Do a lot of hello world programming?
    – Edwin Buck
    Sep 10, 2012 at 17:04
  • All versions are correct pretty much! And you can pretty much include the whole C library to make a hello world, without a problem! This is not really a constructive question! Constructive would be the most efficient hello world program in C maybe? Or a version that gives a quick overview of syntax and needs (eg stdio.h inclusion, main return type etc.) A hello world is almost always correct as far as I know!!! Sep 10, 2012 at 20:51
  • Any program whose sole function is to print hello world on your screen is correct. Already this question has elicited a good deal of polling... voting to close. Sep 11, 2012 at 7:48
  • There are some correctness issues raised by the question, for example some of the results in that Google search don't specify the return type of main (relying on implicit int). But I don't think the question is really phrased to draw those issues out: there's no "the correct" hello world; if the question is "what's wrong with this code?" then it should post and ask about specific code, not refer via Google search to all the code in the world; I doubt that SO is the place to curate a "hello world" for every language, at a rate of one question per language. Sep 11, 2012 at 11:55

10 Answers 10

86

Depends how lazy you are: :)

#error Hello World
7
  • 4
    I learned something here. I am new to C so I was unaware that I can introduce my own errors.
    – xandout
    Sep 23, 2013 at 6:03
  • 18
    This is, unfortunately, absolutely correct.
    – rvighne
    Oct 13, 2013 at 0:18
  • 4
    No up or down vote, because sometimes awesome and terrible are the same.
    – meawoppl
    Mar 4, 2014 at 3:05
  • 4
    But this doesn't compile. Compiling != running. Jul 27, 2014 at 12:11
  • 1
    I can't compile it. Is there something else to the code? I am not fluent in C nor its flavors. Jul 4, 2018 at 16:31
53

I believe this is a standard Hello World program in C:

#include <stdio.h>

int main(void)
{
  printf("Hello World\n");
  return 0;
}
3
  • 4
    Should be puts("hello") or printf("%s","hello")
    – Jan Turoň
    Sep 10, 2012 at 23:51
  • @JanTuroň is puts standard C or possix? May 20, 2015 at 17:40
  • 1
    @KorayTugay: puts() is in Standard C (all editions). It is also in POSIX. May 20, 2015 at 17:43
18

In C99 or C2011, you could use these five lines of code:

#include <stdio.h>
int main(void)
{
    puts("Hello World!");
}

Since C99 (but not C89) allows you to omit the return 0; at the end, it returns a deterministic status of 0 (success) to the calling environment. It doesn't have any unused arguments to the function. It has the prototype for puts() from the header. The output includes an appropriate line ending. I think it is kosher and essentially minimal.

6
  • 1
    You do not even need the "void" I think. May 20, 2015 at 17:39
  • 3
    @KorayTugay: That depends on the compiler options you use. I use options that insist on the presence of void; if you choose to run with sloppier, more permissive options, you can get away without the void. May 20, 2015 at 17:41
  • More accurately, the use of void depends on the standard being used. Feb 10, 2017 at 2:57
  • @Qix: would you care to elaborate on your comment that 'the use of void depends on the standard being used'? Feb 10, 2017 at 3:04
  • @JonathanLeffler IIRC ANSI C required them, but C99 and onward states that you can use an implementation-defined int main() prototype and still be valid C. The fact compilers allow an empty argument list is to be considered 'implementation defined'. Feb 10, 2017 at 3:07
11

There is more than one, and while Tor's answer is good, I prefer to always use an argc / argv main function.

#include <stdio.h>

int main(int argc, char** argv)
{
  printf("Hello World\n");
  return 0;
}

In the rare, odd event that printf was being checked for failure, you might encounter

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

extern int errno;
extern FILE *stdout;

int main(int argc, char** argv)
{
  errno = 0;

  int err = printf("Hello World\n");

  if (err < 0) {
    return EXIT_FAILURE;
  }

  err = fflush(stdout);

  if (err < 0 || errno != 0) {
    return EXIT_FAILURE;
  } else {
    return EXIT_SUCCESS;
  }
}

Like any C program, this has been modified far too many times in attempts to make it even less buggy. Special thanks to R.., dmp, and Scooter who really deserve more credit than I can give.

17
  • 3
    Your second version contains multiple bugs. printf returns the number of characters written, so your error test is wrong. More subtly, this kind of error-checking with stdio is unreliable. If you want to report errors, you need to fflush too. Sep 10, 2012 at 17:38
  • 1
    I always used argc/argv prototype for main, until I started using all warnings and now I have to use (void) in most cases to avoid being told argc/argv are unused.
    – Scooter
    Sep 10, 2012 at 19:51
  • @R.. Thanks for the pointers, I modified printf to only check for error (negative) conditions. I assumed that if stdio was unreliable, reporting errors via printf was unreliable too (and flushing it wouldn't help a bit). That's why the return code was used.
    – Edwin Buck
    Sep 10, 2012 at 20:10
  • 4
    Don't return -1, return EXIT_FAILURE.
    – dmp
    Sep 10, 2012 at 20:12
  • 1
    @dmp Right. Too much time on platforms where EXIT_FAILURE and -1 were the same.
    – Edwin Buck
    Sep 10, 2012 at 20:13
10

Official GNU Hello World can be found here: http://www.gnu.org/software/hello/

The GNU Hello program produces a familiar, friendly greeting. Yes, this is another implementation of the classic program that prints “Hello, world!” when you run it.

However, unlike the minimal version often seen, GNU Hello processes its argument list to modify its behavior, supports greetings in many languages, and so on. The primary purpose of GNU Hello is to demonstrate how to write other programs that do these things; it serves as a model for GNU coding standards and GNU maintainer practices.

GNU Hello is written in C. For implementations in other programming languages, notably including translation into other languages, please see the GNU Gettext distribution.

2
  • 4
    +1 Bahahahaha, that's awesome. Though I can appreciate the serious purpose it serves as a model + coding standards demo. Sep 10, 2012 at 20:26
  • At first I thought it was a joke, then I remembered the full-text GPL, and now I don't think it's a joke. Feb 10, 2017 at 2:59
5

The "official" one would be the one in the first edition of "The C Programming Language" by Kernighan and Ritchie.

To wit:

#include <stdio.h>
main()
{
    printf("hello, world\n");
}
6
  • 3
    The version of the C language described in 1st ed. K&R C is no longer "correct", nor is it advisable to use it as a reference. Sep 11, 2012 at 12:02
  • 3
    @SteveJessop -- Depends on what you consider "correct". It's certainly historic, in that it is the ORIGINAL "hello world". Would you tell Renoir one of his paintings wasn't "correct"?
    – Hot Licks
    Sep 11, 2012 at 12:11
  • 2
    I would if he'd painted a street map of Paris, and someone asked for a "correct" street map of Paris on MapOverflow, and he gave them his from c. 1860. It would no longer be correct, and it would not be advisable to use it as a reference. Interestingly, Wikipedia mentions an earlier "hello world", also by Kernighan, written in B. That's not "correct C" either, but it's certainly historic. Sep 11, 2012 at 12:49
  • 1
    @SteveJessop Interesting, as this brings to mind my recent toying with temporal databases. Hot Licks and you are both correct, except temporally you are only interested in current, while Hot Licks is interested in past views of current knowledge. For hello world, it comes down to requirements, and the only real requirement on that program is that it display "Hello, world!". If you meet all the requirements, no matter how badly written the code, is it correct?
    – Edwin Buck
    Sep 11, 2012 at 14:00
  • 2
    @SteveJessop -- But it should be noted that the "correct text" is "hello, world", not "Hello World" or "Hello world!" or whatever.
    – Hot Licks
    Sep 11, 2012 at 16:06
4
#include <stdio.h>

int main()
{
  printf("Hello World\n");
  getchar();
  return 0;
}
5
  • 1
    @KorayTugay: so that the window it is run in doesn't disappear until after you type a character in it. This is a problem with using IDEs to develop command line programs. May 20, 2015 at 17:45
  • @JonathanLeffler How is it related to IDEs? Terminal windows does not disappear either. May 20, 2015 at 19:28
  • @KorayTugay: Which IDE do you use, on which platform? You will find a lot of C code on SO that has system("pause"); (1,800 questions) or getch() (3,400 questions) or getchar() at the end so that the program does not exit until the user types some input to let it finish. (The raw numbers are from searches for 'system pause' and 'getch'; some may not be at the end of their programs.) I understand that's because the terminal windows on Windows in particular vanish when the command exits. I ran into similar problems with Eclipse and CDT a few years ago now (on Mac). May 20, 2015 at 19:34
  • @JonathanLeffler I am totally lost. Anyway thanks for your response and your time. No big issue anyway I guess.. May 21, 2015 at 5:18
  • Err this is usually for people running .exe's directly from the file explorers on Windows since, if compiled in console mode, a console will usually flash and disappear before you can read any text. Feb 10, 2017 at 3:02
3

What do you mean by "correct"?

I suppose that this one is the most correct, as it doesn't miss anything:

#include <stdio.h>

#ifndef HELLO_STRING
#define HELLO_STRING "Hello, world!"
#endif

int main(int argc, char *argv[], char *envp[])
{
    puts(HELLO_STRING);
    return 0;
}

However, this program is not localized; if you want localization, then use "libintl".

9
  • 1
    Good question ... what is correct ? versions with argc,argv,... could generate warnings as they are defined but not used.
    – Kwariz
    Sep 10, 2012 at 16:35
  • @Kwariz I tried it with gcc -Wall -Wunused, it didn't generate these warnings. Which compiler do you mean? Sep 10, 2012 at 16:42
  • 1
    +1 for the comma in "Hello, world!". ISTR reading that the first-ever "hello world" program had that, so it's clearly more correct. Sep 10, 2012 at 16:48
  • 1
    @Pupkov-Zadnij well simply add -Wextra : gcc -Wall -Wextra ; I use gcc v4.7 ; but that was just to highlight that "correct" has no useful meaning (in my mind) when talking about The "hello world" source :)
    – Kwariz
    Sep 10, 2012 at 16:50
  • 2
    Please don't use envp. Many implementations support more than two arguments to main, however, programs that define main with three arguments are not strictly conforming.
    – dmp
    Sep 10, 2012 at 20:18
2
$ cat hello.c
#include <stdio.h>

int main(void)
{
    printf("Hello world!\n");
}

$ c99 hello.c
$ ./a.out
Hello world!
$
2
  • 2
    Although there is an implicit return of 0 in this case (guaranteed by C99) please consider doing so explicitly as it is highly idiomatic.
    – dmp
    Sep 10, 2012 at 21:12
  • 3
    @dmp There are enough answers with an explicit "return 0". My intention was to say something different :p
    – sigjuice
    Sep 10, 2012 at 22:19
-1

Then there's:

#include <stdlib.h>

int main()
{
  system("echo Hello World!");
  return 0;
}
1
  • 1
    Do not use system() to print text in C. Please use printf, puts, fprintf, but not a system("echo Hello world!");
    – Anic17
    Aug 16, 2020 at 11:17

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