Write a line in program that looks useless

26

2

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.

Konrad Borowski

Posted 2014-01-25T16:24:33.767

Reputation: 11 185

Question was closed 2016-04-18T14:39:42.350

1

I'm voting to close this question as off-topic because underhanded challenges are no longer on-topic on this site. http://meta.codegolf.stackexchange.com/a/8326/20469

– cat – 2016-04-18T13:53:48.783

2Why is the line in the example necessary? – Gari BN – 2014-01-27T14:23:42.333

3@GariBN It's not: "in this example, the line is actually not needed, it's just an example." – elixenide – 2014-01-27T15:53:01.593

9@EdCottrell Good to know... I thought I missed some basic thing... – Gari BN – 2014-01-27T18:26:29.987

2So it is needed. For the example. :-) – Florian F – 2014-10-10T21:25:30.200

Answers

33

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)

Oberon

Posted 2014-01-25T16:24:33.767

Reputation: 2 881

4GCC says "a label can only be part of a statement and a declaration is not a statement". – Vortico – 2014-01-26T07:42:26.433

This make real programs look so ugly actually :. – val says Reinstate Monica – 2019-06-10T19:12:55.577

1I was just bitten by this in a program I'm writing for school. – Kevin – 2014-03-23T21:44:15.890

23

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.

vsz

Posted 2014-01-25T16:24:33.767

Reputation: 7 963

1The moment I saw the superfluous comments I said aloud, "Where's the trigraph? There must be a trigrap-- Ah, there it is." – cat – 2016-01-05T14:24:05.493

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. – Gari BN – 2014-01-27T18:42:07.507

4Nice 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 >. – r3mainer – 2014-01-27T19:52:54.833

@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 :) – vsz – 2014-01-27T21:29:27.500

1I knew something sneaky was afoot the second I saw a local include "stdio.h" instead of the actual library include, <stdio.h> – Braden Best – 2014-02-28T17:37:06.647

@B1KMusic +1 just for teaching me the difference.. – OJFord – 2014-06-03T01:19:35.120

@OllieFord There is actually no difference. That is just convention that states that library includes use <>, while local includes use "". – Justin – 2014-07-03T02:46:56.313

19

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.

Timwi

Posted 2014-01-25T16:24:33.767

Reputation: 12 158

41But fortunately nothing of value will be lost, because the entire language is useless :-) – r3mainer – 2014-01-27T21:27:34.277

pastebin doesn't strip whitespace. – Braden Best – 2014-02-28T17:40:18.913

@B1KMusic: It does strip from the end of the entire input. :) – Timwi – 2014-03-10T18:32:18.113

1@Timwi what about the "raw paste data" box? – Braden Best – 2014-03-10T18:54:46.257

1

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

– Braden Best – 2014-03-10T19:00:25.453

8replace \t with t. Replace " " with s, replace \n with n – Cruncher – 2014-03-19T18:37:04.730

7Best prank ever: Post something without actually writing code. – Kaz Wolfe – 2014-09-08T03:22:17.250

9

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).

Toothbrush

Posted 2014-01-25T16:24:33.767

Reputation: 3 197

"it will add to b the value of alert(a)" : So, shouldn't the b+- be b+= ? – Gaurang Tandon – 2014-02-22T17:20:47.163

@GaurangTandon No; the idea is that it looks like b++ or b--. – Toothbrush – 2014-02-22T17:27:04.250

what does b+- actualy mean? – Mhmd – 2014-02-22T18:19:52.210

5@user689 b + -2 + 2. That's why if you delete the line with 2 + 2;, it evaluates to b + -alert(a);. – Toothbrush – 2014-02-22T18:23:41.160

8

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.

r3mainer

Posted 2014-01-25T16:24:33.767

Reputation: 19 135

7

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

marinus

Posted 2014-01-25T16:24:33.767

Reputation: 30 224

7

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'”).

Ben Reich

Posted 2014-01-25T16:24:33.767

Reputation: 1 577

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. – Kendall Frey – 2014-01-27T13:47:02.103

@KendallFrey Good point! Fixed. – Ben Reich – 2014-01-27T15:34:07.070

5

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.

OJW

Posted 2014-01-25T16:24:33.767

Reputation: 181

I don't see any comments. – orion – 2014-05-30T11:40:17.837

4

REM (remark) is the comment operator - https://stackoverflow.com/questions/11269338

– OJW – 2014-05-30T12:24:04.667

4

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.

Florian F

Posted 2014-01-25T16:24:33.767

Reputation: 591

1Some explanation please, or I will remove that line whatever you say. – Bill Woodger – 2014-10-10T23:47:42.583

3

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)

histocrat

Posted 2014-01-25T16:24:33.767

Reputation: 20 600

1It'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] } – Doorknob – 2014-01-25T23:47:06.747

1That 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. – histocrat – 2014-01-25T23:50:20.947

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... – Doorknob – 2014-01-25T23:51:26.163

Fair enough. Maybe my coworker was just being unobservant. – histocrat – 2014-01-25T23:52:12.597

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. – user2357112 supports Monica – 2014-04-04T14:47:00.417

3

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

Wasi

Posted 2014-01-25T16:24:33.767

Reputation: 1 682

3

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.

ankr

Posted 2014-01-25T16:24:33.767

Reputation: 131

2

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.

Bill Woodger

Posted 2014-01-25T16:24:33.767

Reputation: 1 391

2

#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.

photoionized

Posted 2014-01-25T16:24:33.767

Reputation: 191

1Any decent compiler is going to give you a "too many initializers" diagnostic. – Ben Voigt – 2014-04-04T18:49:05.837

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. – kernigh – 2014-06-06T01:46:38.373

2

~-~!

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.

cjfaure

Posted 2014-01-25T16:24:33.767

Reputation: 4 213

How do you pronounce the language name? – None – 2014-06-05T12:57:21.187

8@professorfish ~-~! – Timtech – 2014-06-05T13:10:57.623

1@professorfish That'd be "no comment". – cjfaure – 2014-06-05T15:31:21.343

1

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.

Bob

Posted 2014-01-25T16:24:33.767

Reputation: 844

0

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.

Thomas Baruchel

Posted 2014-01-25T16:24:33.767

Reputation: 1 590

Nice one, this happens in LaTeX too. Without an empty box or something similar, vertical spacing doesn't work properly. – orion – 2014-05-30T11:41:31.397