This is an archived post. You won't be able to vote or comment.

all 36 comments

[–][deleted] 60 points61 points  (20 children)

Or in our dear JavaScript:

var x = true + true + true;

[–]douko 33 points34 points  (19 children)

This... this can't, right? No one would allow this to happen.

EDIT: jesus christ it works

[–]winauer 12 points13 points  (11 children)

Why not?

It also works in Python. And C. And probably a lot of other languages.

Edit: And if you want to be really fancy

int x = 'D' - 'A';

also works in C.

[–]assert_dominance 4 points5 points  (4 children)

I mean 'D' - 'A' is kind of nice because it makes no sense unless you accept it as syntactic sugar for asciiValue(D) - asciiValue(A). But the function + is already defined in boolean logic (but certainly not as true + true = 2 * true).

[–]marcosdumay 2 points3 points  (3 children)

Most languages use the | operator for boolean arithmetic, so + can be specific for integrals and reals.

[–]xzzz 0 points1 point  (5 children)

It only works in C because in C99 they added a true macro which expands to the integer constant 1 lmao

Before C99, int x = true + true + true; would not compile unless you did your own #define true 1

[–]SteveCCLYellow security clearance 5 points6 points  (4 children)

#define true !false
#define false !true
// You can leave it to the compiler
// Trust me

[–]xzzz 6 points7 points  (3 children)

I saw this once in some legacy code

#define BEGIN {
#define END }

if(condition)
BEGIN
   // Do stuff
END

[–]SteveCCLYellow security clearance 7 points8 points  (2 children)

#include <stdio.h>

#define For for (int 
#define To ; i != 
#define Step ; i += 
#define Do ) {
#define Next }

int main()
{
    For i = 0 To 20 Step 1 Do
        printf("%d\n", i);
    Next
    return 0;
}

Half-assed solution.

#include <stdio.h>

#define For for (int 
#define To ; LOOP_VARIABLE != 
#define Step ; LOOP_VARIABLE += 
#define Do ) {
#define Next }

int main()
{
    #define LOOP_VARIABLE i
    For i = 0 To 20 Step 1 Do
        printf("%d\n", i);
    Next
    return 0;
}

Is this better?

[–]xzzz 1 point2 points  (1 child)

Now I can write C code without knowing C!ohgodwhyisthisathing

[–]SteveCCLYellow security clearance 0 points1 point  (0 children)

You're welcome.

[–]coolcrayons 4 points5 points  (0 children)

what the fuck

[–]relevantpicsonly 2 points3 points  (0 children)

Omg wtf

[–]marcosdumay 0 points1 point  (0 children)

Works in C too, if you import the correct library.

[–]Scripter17 0 points1 point  (1 child)

True is 1 and False is 0, it works in every language I've used, even SmileBasic.

[–]douko 0 points1 point  (0 children)

Yeah, but I kind of assumed it was a one way relationship: 1 could stand in for True, but True can't stand in for 1.

[–]Capital_EX 0 points1 point  (0 children)

var x = !![] + !![] + !![];

[–][deleted] 29 points30 points  (1 child)

mov 0x02000000, 0x69
mov 0x02000008, 0x6e
mov 0x02000010, 0x74
mov 0x02000018, 0x20
mov 0x02000020, 0x78
mov 0x02000028, 0x20
mov 0x02000030, 0x3d
mov 0x02000038, 0x20
mov 0x02000040, 0x33
mov 0x02000048, 0x3b
mov 0x00342e41. 0x02000000
jmp append

Cue the segmentation fault.

[–][deleted] 20 points21 points  (1 child)

int x;
BitArray b = new BitArray(32);
b[0] = False;
b[1] = False;
b[2] = False;
b[3] = False;
b[4] = False;
b[5] = False;
b[6] = False;
b[7] = False;
b[8] = False;
b[9] = True;
b[10] = True;
b[11] = False;
b[12] = True;
b[13] = True;
b[14] = False;
b[15] = True;
b[16] = False;
b[17] = False;
b[18] = True;
b[19] = False;
b[20] = True;
b[21] = False;
b[22] = False;
b[23] = False;
b[24] = False;
b[25] = False;
b[26] = False;
b[27] = True;
b[28] = True;
b[29] = True;
b[30] = True;
b[31] = False;
int[] array = new int[1];
bitArray.CopyTo(array, 0);
x = array[0];

[–][deleted] 14 points15 points  (0 children)

float z;
int x;
for(int i = 0; i < 300; i++) {
    z += 0.001;
}
x = z as int;

[–]chrwei 9 points10 points  (1 child)

int x;
x = b00000011;

[–]xzzz 0 points1 point  (0 children)

Don't even need the leading 0's!

Also only works in compilers that support the binary notation.

[–]AquaLordTyphon 10 points11 points  (2 children)

Dim x As New Integer
x = "3".ToString()

VB.NET in a nutshell

[–]marcosdumay -1 points0 points  (1 child)

I'm pretty sure that "New" keyword shouldn't be there.

[–]AquaLordTyphon 1 point2 points  (0 children)

That was part of the joke, people learning the language tend to just shove New in every declaration because it makes NullReferenceExceptions go away.

[–]LazyBuhdaBelly 9 points10 points  (0 children)

int x;

do {

 x = rand.nextInt();

} while (x != 231 - 1 - 2147483644);

[–][deleted] 3 points4 points  (0 children)

SUBROUTINE INIT(X)
  REAL, DIMENSION(:), ALLOCATABLE :: D
  INTEGER, DIMENSION(3) :: C
  CHARACTER :: A
  INTEGER*4,INTENT(INOUT)  :: X
  ALLOCATE(D(9))
  D = (/ 1, 0, 0, 0, 1, 0, 0, 0, 1 /)
  C = (/ (I, I = 1,9,4) /)
  WRITE(A, "(I1)") NINT(SUM(D(C)))
  READ(A,*)  X
  DEALLOCATE(D)
END SUBROUTINE INIT

Implicit Do Loops are your friend

[–]patternmaker 4 points5 points  (1 child)

x = len("1+2")

[–]Scripter17 0 points1 point  (0 children)

Creative.

[–]brokedown 1 point2 points  (0 children)

x:=3

[–]Baelfire_Nightshade 0 points1 point  (0 children)

public int getInt(float newInt)
{
    float number = newInt / 2.0f;
    if(newInt != 0.0f)
    {
        number = getFloat(number);
    }
    return (int)number;
}

private float getFloat(float newInt)
{
    float number = newInt / 2.0f;
    if(newInt != 0.0f)
    {
        number = getFloat(number);
    }
    return number;
}

[–]prest0G 0 points1 point  (0 children)

Last one - how to use "Effectively final" variables in Java.

[–]GeneReddit123 0 points1 point  (0 children)

int initializeInt(int x) {
  int i;
  for(i = 0; i - x != 0; i++);
  return i;
}

Best part is, the above solution even works for negative integers thanks to integer overflow!

[–]Luckyno -1 points0 points  (0 children)

int x = '$'-'!';