36

Update:
Surprised that it is being so heavily downvoted...

The question is coding-related and before asking this question I have googled for "42" in combination with:

  • site:msdn.micrsoft.com
  • "code example"
  • "c#"
  • "magic number"

And I am not an expert/fan of Western culture/literature.

Also found, Why are variables “i” and “j” used for counters? [duplicate] which was not closed but even protected.


I feel that everybody knows it, except me...

What is the origin of ubiquitous magic digit 42 used all over the code samples and samples?

How have you come using 42? because I have not ever come or ever used 42

After some search, I found MSDN doc on it: Magic Numbers: Integers:

  • "Aside from a book/movie reference, developers often use this as an arbitrary value"

Well, this did not explain me anything.

Which movies and books have I missed for all those years of being involved in development, coding and programming and around-IT related activities like rwquirements analysis, system administration, etc??

Some references to some texts using code snippets with 42 (just C#-related):

Jérôme Laban. C# Async Tips and Tricks, Part 3: Tasks and the Synchronization Context

  var t = Task.Delay(TimeSpan.FromSeconds(1))
              .ContinueWith
                (
                    _ => Task.Delay(TimeSpan.FromSeconds(42))
                );

MSDN Asynchronous Agents Library

   send(_target, 42);  

Quickstart: Calling asynchronous APIs in C# or Visual Basic

  Office.context.document.setSelectedDataAsync(
      "<html><body>hello world</body></html>",
       {coercionType: "html", asyncContext: 42},
       function(asyncResult) {
           write(asyncResult.status + " " + asyncResult.asyncContext);

Asynchronous Programming in C++ Using PPL

  task<int> myTask = someOtherTask.then([]() { return 42; });

Boxing and Unboxing (C# Programming Guide)

  Console.WriteLine(String.Concat("Answer", 42, true));

How To: Override the ToString Method (C# Programming Guide)

  int x = 42;

Trace Listeners

  // Use this example when debugging.
  System.Diagnostics.Debug.WriteLine("Error in Widget 42");
  // Use this example when tracing.
  System.Diagnostics.Trace.WriteLine("Error in Widget 42");

|| Operator (C# Reference

  // The following line displays True, because 42 is evenly  
  // divisible by 7.
  Console.WriteLine("Divisible returns {0}.", Divisible(42, 7));

  // The following line displays False, because 42 is not evenly 
  // divisible by 5.
  Console.WriteLine("Divisible returns {0}.", Divisible(42, 5));

  // The following line displays False when method Divisible  
  // uses ||, because you cannot divide by 0. 
  // If method Divisible uses | instead of ||, this line 
  // causes an exception.
  Console.WriteLine("Divisible returns {0}.", Divisible(42, 0));

WIKIPedia C Sharp (programming language)

  int foo = 42;         // Value type.
Community
  • 1
  • 1
  • 15
    I'd tell you but then I'd have to kill you... – Arun Apr 26 '13 at 12:14
  • 7
    +1 for best question ever :D – Joeri Hendrickx Apr 26 '13 at 12:18
  • I got that strange feeling that this question does not belong here, but I'm not sure about where moving it to... – Andre Calil Apr 26 '13 at 12:20
  • 4
    @JoeriHendrickx How is this a good question? It's off topic and I doubt the veracity of the prior research. A Google for "magic number 42" retrieves the answer quite easily. – Duncan Jones Apr 26 '13 at 12:20
  • 1
    Ask Eccentrica Gallumbits. – Kjartan Apr 26 '13 at 12:20
  • 3
    And the answer is ... 42 :) – valex Apr 26 '13 at 12:21
  • @DuncanJones just made me actually laugh out loud :) I know that you're completely right, but for this one I just have to let it slide. – Joeri Hendrickx Apr 26 '13 at 12:22
  • "And I am not an expert/fan of Western culture/literature." - That is OK, but "The Hitch Hiker's Guide to the Galaxy" is a highly recommended read either way :) – Quintium Apr 26 '13 at 14:09
  • @valex But what is the question? – Ross Patterson Apr 27 '13 at 12:51
  • @RossPatterson, my question, as I've put it there, was: "How have you come (or started) to use 42 in your coding?" – Gennady Vanin Геннадий Ванин Apr 27 '13 at 13:17
  • A bit late to the party but if you actually read the lock reason in [Why are variables “i” and “j” used for counters?](http://stackoverflow.com/questions/4137785/why-are-variables-i-and-j-used-for-counters/4137809) you'll see it says *"This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so **please do not use it as evidence that you can ask similar questions here.**"* – JJJ May 05 '13 at 10:35
  • @Juhana, what are [etimology](http://stackoverflow.com/questions/tagged/etymology) and [history](http://stackoverflow.com/questions/tagged/history) tags good for? – Gennady Vanin Геннадий Ванин May 05 '13 at 11:18
  • 1
    I think it worth to be noted that according to the New Testament(Matthew 1) quantity of generations from Abraham to Jesus Christ also equals to 42. – dummy Jul 16 '23 at 18:16

6 Answers6

31

It's from The Hitch Hiker's Guide to the Galaxy.

In The Hitchhiker's Guide to the Galaxy (published in 1979), the characters visit the legendary planet Magrathea, home to the now-collapsed planet-building industry, and meet Slartibartfast, a planetary coastline designer who was responsible for the fjords of Norway. Through archival recordings, he relates the story of a race of hyper-intelligent pan-dimensional beings who built a computer named Deep Thought to calculate the Answer to the Ultimate Question of Life, the Universe, and Everything. When the answer was revealed to be 42, Deep Thought explained that the answer was incomprehensible because the beings didn't know what they were asking. It went on to predict that another computer, more powerful than itself would be made and designed by it to calculate the question for the answer. (Later on, referencing this, Adams would create the 42 Puzzle, a puzzle which could be approached in multiple ways, all yielding the answer 42.)

Andomar
  • 232,371
  • 49
  • 380
  • 404
18

The answer is, as people already have stated, The Hitchhiker's Guide to the Galaxy.

I made a little experiment and put a couple of numbers in the search field, and these are the results:

enter image description here

It seems like 42 beats its neighbors clearly, but it can't touch regular numbers like 40, 45 and 50, no matter how magical it is.

It would be interesting to do the same search in source code only.

Victor Sand
  • 2,270
  • 1
  • 14
  • 32
10

Dude!

It's the Answer to the Ultimate Question of Life, the Universe, and Everything! As computed by Deep Thought supercomputer, which took 7.5 million years!

http://en.wikipedia.org/wiki/The_answer_to_life_the_universe_and_everything#Answer_to_the_Ultimate_Question_of_Life.2C_the_Universe_and_Everything_.2842.29

iluxa
  • 6,941
  • 18
  • 36
8

Check this out. 42 is the ultimate answer to the ultimate question of life the universe and everything

Martin Dvoracek
  • 1,714
  • 6
  • 27
  • 55
4

This is from The Hitch hikers Guide to the Galaxy and is:

The Answer to the Ultimate Question of Life, the Universe, and Everything

WikiLink

Belogix
  • 8,129
  • 1
  • 27
  • 32
4

Refer The Hitch Hiker's Guide to the Galaxy.

Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48