26
\$\begingroup\$

Write some program (the size doesn't matter), and put a line that looks useless, but it's actually not - if it would be removed, the program would crash, or do something wrong. Feel free to mark the line that looks unused by a comment, or mention it your code description, like in example below (of course, in this example, the line is actually not needed, it's just an example).

int main(void) {
    /* The line below is actually needed. */
    2 + 2;

    return 0;
}

As this is popularity contest, the solution with highest number of votes wins.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Why is the line in the example necessary? \$\endgroup\$
    – Gari BN
    Commented Jan 27, 2014 at 14:23
  • 3
    \$\begingroup\$ @GariBN It's not: "in this example, the line is actually not needed, it's just an example." \$\endgroup\$
    – elixenide
    Commented Jan 27, 2014 at 15:53
  • 10
    \$\begingroup\$ @EdCottrell Good to know... I thought I missed some basic thing... \$\endgroup\$
    – Gari BN
    Commented Jan 27, 2014 at 18:26
  • 2
    \$\begingroup\$ So it is needed. For the example. :-) \$\endgroup\$
    – Florian F
    Commented Oct 10, 2014 at 21:25
  • 1
    \$\begingroup\$ I'm voting to close this question as off-topic because underhanded challenges are no longer on-topic on this site. meta.codegolf.stackexchange.com/a/8326/20469 \$\endgroup\$
    – cat
    Commented Apr 18, 2016 at 13:53

17 Answers 17

34
\$\begingroup\$

C

int main(int argc, char **argv) {
    switch(argc) {
        case 2:
            ;
            int allowed = 0;
        case 3:
            argc--;
            break;
        default:
            argc = 3;
    }
    return 0;
}

Won't compile if we remove the single ;.

EDIT:

Solution below:

This happens because a case statement is just a label, and, as such, must be followed by a statement (which the standard refers to as a "specifier for actions to be performed", which rules out declarations). To prevent this, we prefix the declaration with a ;, a null statement. Specifications for the case label can be found here at page 142 (131 in-document)

\$\endgroup\$
3
  • 4
    \$\begingroup\$ GCC says "a label can only be part of a statement and a declaration is not a statement". \$\endgroup\$
    – Vortico
    Commented Jan 26, 2014 at 7:42
  • 1
    \$\begingroup\$ I was just bitten by this in a program I'm writing for school. \$\endgroup\$
    – Kevin
    Commented Mar 23, 2014 at 21:44
  • \$\begingroup\$ This make real programs look so ugly actually :\. \$\endgroup\$ Commented Jun 10, 2019 at 19:12
23
\$\begingroup\$

C / C++

Just some code written in the style of introduction / tutorial...

Tutorial nr. 2: reading in numbers, performing mathematical operations on them, and printing the results.

//////////////////////////////////////////////////
///////////////////////////////////////////////////
/////                                            //
/////           T  U  T  O  R  I  A  L           //
/////                                            //
///////////////////////////////////////////////////

#include "stdio.h"

int main()
{
    ///////////////////////////////////////////////
    // Local variables (int means integer!!)      /
    int a, b, result;

    ///////////////////////////////////////////////
    // Reading them in                            /
    scanf("%d", &a);
    scanf("%d", &b);

    ///////////////////////////////////////////////
    // Performing an operation, e.g. addition!!!  /
    result = a+b;

    ///////////////////////////////////////////////
    // Why is the next line absolutely mandatory??/
    result = 42;
    printf("The result is: %d", result);

}

Removing the line result = 42; which at first glance looks both superfluous, and, considering the stated objective, actually wrong, will actually cause the program to not do what the description states.

Why?

Trigraphs strike again! ??/ will be replaced by the preprocessor with \, which will, in turn, concatenate the next line into the comment. The result = 42; becomes part of the comment, so it "saves" the printing line. With the result = 42; line removed, the printf line will be swallowed by the comment. Some compilers might give a warning, or trigraphs might be turned off as default and must be turned on for this example to work. Syntax highlighting might also betray it, but there are compilers and IDEs around which leave you none the wiser.

\$\endgroup\$
7
  • \$\begingroup\$ At least in gcc, you must specify that you want to use trigraphs by adding this flag (-trigraphs). Without this, gcc rises warning and ignores trigraphs. \$\endgroup\$
    – Gari BN
    Commented Jan 27, 2014 at 18:42
  • 4
    \$\begingroup\$ Nice one! I'm glad I didn't scroll down too far before figuring it out. You can change your blockquote into spoiler text by starting it with >! instead of just >. \$\endgroup\$
    – r3mainer
    Commented Jan 27, 2014 at 19:52
  • \$\begingroup\$ @squeamishossifrage : thanks, this was my original intention, I used >! just not in every line. I though they don't display for me as I wrote them. Interestingly, the SE parser does not spoil it :) \$\endgroup\$
    – vsz
    Commented Jan 27, 2014 at 21:29
  • 1
    \$\begingroup\$ I knew something sneaky was afoot the second I saw a local include "stdio.h" instead of the actual library include, <stdio.h> \$\endgroup\$ Commented Feb 28, 2014 at 17:37
  • 1
    \$\begingroup\$ The moment I saw the superfluous comments I said aloud, "Where's the trigraph? There must be a trigrap-- Ah, there it is." \$\endgroup\$
    – cat
    Commented Jan 5, 2016 at 14:24
19
\$\begingroup\$

Whitespace

Unfortunately I cannot show my program here because StackExchange strips all trailing whitespace. The same is true of pastebin.com and pastie.org, so I can’t link to it either.

Of course, the entire program “looks useless”, but if you remove any line, it will do something different.

\$\endgroup\$
7
  • 43
    \$\begingroup\$ But fortunately nothing of value will be lost, because the entire language is useless :-) \$\endgroup\$
    – r3mainer
    Commented Jan 27, 2014 at 21:27
  • 1
    \$\begingroup\$ @Timwi what about the "raw paste data" box? \$\endgroup\$ Commented Mar 10, 2014 at 18:54
  • 1
    \$\begingroup\$ @Timwi I just tested it here, and I see what you mean. But if you put non-whitespace text, it will submit, and the raw paste data will be untouched. E.g. you could put an extra line saying "Copy the raw paste data and delete this line" \$\endgroup\$ Commented Mar 10, 2014 at 19:00
  • 8
    \$\begingroup\$ replace \t with t. Replace " " with s, replace \n with n \$\endgroup\$
    – Cruncher
    Commented Mar 19, 2014 at 18:37
  • 7
    \$\begingroup\$ Best prank ever: Post something without actually writing code. \$\endgroup\$
    – Kaz Wolfe
    Commented Sep 8, 2014 at 3:22
9
\$\begingroup\$

JavaScript:

for (var a = 0, b = 4; ++a < b; a++)b+-

2 + 2; // This line is needed, or it will alert twice!
alert(a);

The reason it works is because the body of the for is b+-, not b++ or b--; therefore, if you remove the required line, it will add to b the value of -alert(a).

\$\endgroup\$
4
  • \$\begingroup\$ "it will add to b the value of alert(a)" : So, shouldn't the b+- be b+= ? \$\endgroup\$ Commented Feb 22, 2014 at 17:20
  • \$\begingroup\$ @GaurangTandon No; the idea is that it looks like b++ or b--. \$\endgroup\$
    – Toothbrush
    Commented Feb 22, 2014 at 17:27
  • \$\begingroup\$ what does b+- actualy mean? \$\endgroup\$
    – Mohammad
    Commented Feb 22, 2014 at 18:19
  • 5
    \$\begingroup\$ @user689 b + -2 + 2. That's why if you delete the line with 2 + 2;, it evaluates to b + -alert(a);. \$\endgroup\$
    – Toothbrush
    Commented Feb 22, 2014 at 18:23
8
\$\begingroup\$

C:

#include <stdio.h>
#include <string.h>

int main() {
    int i,sum;
    char s[4];
    
    /* Add all the integers < 1000 that contain no '0' or '9' */
    sum=0;
    for (i=0; i<1000; i++) {
        sprintf(s,"%d",i);
        if (strchr(s,'0')) goto skip;
        if (strchr(s,'9')) goto skip;
        sum += i;
      skip:
        i = i; /* Don't delete this line! */
    }
    printf("The answer is %d\n",sum);  /* <<< Should be 258948 */
    return 0;
}

If you delete the line i = i; then the code won't work. This is because...

... in fact it won't even compile. According to the C standard, labels like skip in the above example must be associated with a statement. Even a single semicolon would have sufficed, but if you delete the whole line then the label has no associated statement and compilation fails. This answer explains in a bit more detail.

\$\endgroup\$
7
\$\begingroup\$

APL

Here's an APL function:

i_←{
  ⍺←⎕IO ⋄ ⎕IO←⍺
  ⍵⍴⍳×/⍵
}

It does the same thing as J's monadic i.:

      i_ 5 5
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

It can take an optional left argument that gives the offset:

      0 i_ 5 5
 0  1  2  3  4
 5  6  7  8  9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

The line ⍺←⎕IO ⋄ ⎕IO←⍺ looks useless ("assign ⎕IO to , then assign to ⎕IO"), but the first part only runs if no left argument is given, so in fact it does do something:

  • if no is given, set to (global) ⎕IO.
  • set (local) ⎕IO to
\$\endgroup\$
7
\$\begingroup\$

C#

Func<int, int> fibo;
fibo = null; //Note that this can be set to *any* Func<int, int> reference.  It doesn't matter.
fibo = x => x < 1 ? 1 : fibo(x-1) + fibo(x-2);

The variable fibo must be assigned before you can use it inside a lambda, even if you’re assigning that lambda to fibo itself. If you remove the (seemingly useless) assignment, a compiler error will occur (“Use of unassigned local variable 'fibo'”).

\$\endgroup\$
2
  • \$\begingroup\$ It's not a null reference exception, it's an uninitialized variable compiler error. Also, better move the declaration to its own line, because the declaration is not useless. \$\endgroup\$ Commented Jan 27, 2014 at 13:47
  • \$\begingroup\$ @KendallFrey Good point! Fixed. \$\endgroup\$
    – Ben Reich
    Commented Jan 27, 2014 at 15:34
5
\$\begingroup\$

Windows Batch:

echo hello
pause
echo world

Set the program running and modify the file to:

rem test01
pause
echo hello world

If you remove or resize the comment, it will cause havoc as the 1st instance of the program pauses and then continues interpreting from the 19th byte onwards.

\$\endgroup\$
2
  • \$\begingroup\$ I don't see any comments. \$\endgroup\$
    – orion
    Commented May 30, 2014 at 11:40
  • 4
    \$\begingroup\$ REM (remark) is the comment operator - stackoverflow.com/questions/11269338 \$\endgroup\$
    – OJW
    Commented May 30, 2014 at 12:24
4
\$\begingroup\$

COBOL (IBM Mainframe)

   ID DIVISION. 
   PROGRAM-ID. USELESS. 
   DATA DIVISION. 
   WORKING-STORAGE SECTION. 
   01  W-WHEN-COMPILED              PIC X(8)BX(8) VALUE SPACE.
   PROCEDURE DIVISION. 
       IF W-WHEN-COMPILED EQUAL TO SPACE 
           NEXT SENTENCE 
       END-IF 
       MOVE SPACE                   TO W-WHEN-COMPILED. 
       MOVE WHEN-COMPILED           TO W-WHEN-COMPILED 

       DISPLAY W-WHEN-COMPILED " HELLO WORLD!" 
       GOBACK 
       . 

If you run the above program (any IBM Mainframe COBOL since COBOL II (first IBM compiler to 1985 standard, probably other IBM COBOLs as well) the output is:

02/22/14 13.11.02 HELLO WORLD!

However, if you remove the triply-useless line "MOVE SPACE TO W-WHEN-COMPILED." (the field has an initial value of space, and has other content placed in it in the next instruction, and it is branched over anyway) the program produces no output and actually Abends (U4038) (that means it crashes) with the following message:

 IGZ0037S The flow of control in program USELESS proceeded beyond the 
          last line of the program.  From compile unit USELESS at entry 
          point USELESS at compile unit offset +000003AC at entry offset
          +000003AC at address 119003AC. 

(message code and text would vary amongst compilers, offsets depend upon actual compiler used and compile options, address depends on where program is loaded when EXECuted).

The reason is the NEXT SENTENCE. This is a Secret GO TO. The compiler hunts down the next full-stop/period in the source, and generates a branch to the following instruction. From COBOL II, the use of full-stops/periods was relaxed. A program must end with a full-stop/period. In this case, the branch is off the end of the program.

Both programs compile 100% clean (no diagnostic messages, Return Code of zero).

This behaviour is a dumb "IBM Extension" to COBOL. The 1985 Standard does not allow NEXT SENTENCE within IF/END-IF (CONTINUE is used instead, which is a no-op). IBM allowed it - causing horrible Gotchas from time-to-time.

\$\endgroup\$
4
\$\begingroup\$

JAVASCRIPT

var a = function(b) {
    return b;
}

; // This must be here

(function() {
    // more code
}());

Without the semicolon then a would become what ever the return value from 2nd function is - in this case it's undefined. Because the function originally assigned to a will be executed right away with the second function as the argument b.

\$\endgroup\$
4
\$\begingroup\$

Java

package stackexchange;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Curl
{
    public void getUrl(String address)
    {
        try {
            URL url = new URL(address);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            /* don't remove this line! */
            http://codegolf.stackexchange.com

            while( true ){
                String line = input.readLine();
                if( line==null ) break http;
                System.out.println(line);
            }

            input.close();

        } catch(IOException ex){
            // ignore.
        }
    }

    public static void main(String[] args) {
        Curl curl = new Curl();
        curl.getUrl("http://codegolf.stackexchange.com");
    }
}

It is a working program that retrieves a web page from an URL. You might be surprised that the line

            http://codegolf.stackexchange.com

compiles without even a warning. In fact, the program doesn't compile without it.

It is actually a label, http:, followed by a comment, //codegolf.... The label applies to the while and is used in the break http; statement.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Some explanation please, or I will remove that line whatever you say. \$\endgroup\$ Commented Oct 10, 2014 at 23:47
3
\$\begingroup\$

Ruby

require 'prime'

# Given a regular expression, return the smallest prime containing it
# and the substring of the prime that matches it
def smallest_prime_matching_regexp(regexp)
  match = 'this assignment is necessary even though the value is never used'
  prime = Prime.find { |n| match = n.to_s[regexp] }
  return prime, match
end

[ /4/, /1+2/, /[02468]{2,}/ ].each do |regexp|
  prime, match = smallest_prime_matching_regexp(regexp)
  puts "#{regexp.inspect}:\t#{prime} (#{match})"
end

A similar thing came up at work recently--a coworker actually flagged my seemingly unnecessary initialization during code review, and I was really tempted to put the explanation inside a spoiler block. So I'll do that here.

The line is necessary because a variable initialized inside a block is local to that block. If we initialize it outside the block, then the value is able to escape to the method scope.

Output as is:

/4/:    41 (4)
/1+2/:  127 (12)
/[02468]{2,}/:  223 (22)

Output with the first line of the method removed:

undefined local variable or method `match' for main:Object (NameError)

\$\endgroup\$
5
  • 1
    \$\begingroup\$ It's fairly obvious why that's needed... and anyway, a better way to write this would be just match = Prime.find { |n| n.to_s[regexp] } \$\endgroup\$
    – Doorknob
    Commented Jan 25, 2014 at 23:47
  • 1
    \$\begingroup\$ That wouldn't do what I want...Prime.find returns n (e.g. 223), and match needs to be n.to_s[regexp] (e.g. "22"). Obviously I contrived the problem to make that necessary. \$\endgroup\$
    – histocrat
    Commented Jan 25, 2014 at 23:50
  • \$\begingroup\$ Ah, sorry, didn't notice you were calling find on Prime and not an array. Anyway, it's still fairly easy to notice why that line is needed... \$\endgroup\$
    – Doorknob
    Commented Jan 25, 2014 at 23:51
  • \$\begingroup\$ Fair enough. Maybe my coworker was just being unobservant. \$\endgroup\$
    – histocrat
    Commented Jan 25, 2014 at 23:52
  • \$\begingroup\$ Huh. No variable declarations, but the opposite approach to defaulting local/nonlocal to what Python does. That's going to feel weird if I ever decide to learn Ruby. \$\endgroup\$ Commented Apr 4, 2014 at 14:47
3
\$\begingroup\$

PHP

<?php
$some_html_text= <<<EOT
<h1>This is a Header </h1>
<body> This is a body </body>
EOT;
//This is a comment    

Here, \n//This is a comment is necessary. Without it the code won't run.

At least a \n char is necessary after EOT; to run it properly

\$\endgroup\$
3
\$\begingroup\$
#include <stdio.h>
#include <stdlib.h>

void strcmp_or_die(char *s, char *t) {
    if(!strcmp(s, t)) {
        printf("Success!\n");
    } else {
        int *ptr = NULL;
        *ptr = 0;
    }
}

int main(void) {
    char blah = '\0'; //segfaults without this line

    char v[2] = "ok";
    strcmp_or_die(v, "ok");
}

Simple string overflow problem.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Any decent compiler is going to give you a "too many initializers" diagnostic. \$\endgroup\$
    – Ben Voigt
    Commented Apr 4, 2014 at 18:49
  • \$\begingroup\$ Which platform segfaults without blah? Here with OpenBSD 5.5/amd64 and the system gcc, it never segfaults, whether or not blah is there. The compiled program allocates 16 bytes of stack space and copies the 16-bit value of "ok" to the first 2 bytes. The uninitialized 3rd byte has '\0' by luck. If blah is there, it is in the 16th byte, and still not terminating the string. \$\endgroup\$
    – kernigh
    Commented Jun 6, 2014 at 1:46
2
\$\begingroup\$

~-~!

This is probably the stupidest, most obvious one here.

'=|hello\|:
'=~!|Useless?\|:
@'[|y|]|n|:

It throws an error without that second line, with it it prints y.

the backslash isn't an escape character, hehe.

\$\endgroup\$
3
  • \$\begingroup\$ How do you pronounce the language name? \$\endgroup\$
    – user16402
    Commented Jun 5, 2014 at 12:57
  • 8
    \$\begingroup\$ @professorfish ~-~! \$\endgroup\$
    – Timtech
    Commented Jun 5, 2014 at 13:10
  • 1
    \$\begingroup\$ @professorfish That'd be "no comment". \$\endgroup\$
    – cjfaure
    Commented Jun 5, 2014 at 15:31
1
\$\begingroup\$

Windows Batch

This is a batch file that prints the numbers 1 to 10 with an empty line after each:

@echo off

setlocal EnableDelayedExpansion

set NL=^


rem Previous two lines deliberately left blank for NL to work.

for /l %%a in (1,1,10) do (
    echo %%a!NL!
)

endlocal

pause

This effectively makes use of a dirty hack to store a newline inside a variable (the use of delayed expansions gets rid of the more scary looking NL line in the linked answer). If you remove or otherwise change the two completely useless-looking blank lines above the comment, well, it won't work.

\$\endgroup\$
1
\$\begingroup\$

troff

Troff is the most wonderful text processing system. I use it for my documents. It requires a macro system in order to be ready to be used, and the most famous is the ms macro package. Let's compile the following piece of code with the classical -ms option.

.\" -ms macro package
.\" the following line seems to be useless
\& \" print an invisible character
.ad c
.sp |300p \" absolute vertical positionning
.ps 36p
.ft B
My title.
.PP
This is a paragraph.

The third line seems to be useless, and would actually be useless without the macro package. Since it is followed two lines below by an absolute vertical positionning, it could seem meaningless to print nothing before. But without this invisible character, the vertical positionning will have no effect, though I am not absolutely sure I could explain why (see this thread if you want to have a glance at the discussion). This makes the new user crazy, but then the trick should become familiar to the more experimented user.

Just checked with my favorite version of troff which is not very known (heirloom troff), but I am pretty sure it still works the same with GNU troff.

\$\endgroup\$
1
  • \$\begingroup\$ Nice one, this happens in LaTeX too. Without an empty box or something similar, vertical spacing doesn't work properly. \$\endgroup\$
    – orion
    Commented May 30, 2014 at 11:41

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