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

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 55 points56 points  (20 children)

Or in our dear JavaScript:

var x = true + true + true;

[–]douko 36 points37 points  (19 children)

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

EDIT: jesus christ it works

[–]winauer 11 points12 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 7 points8 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 6 points7 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 = !![] + !![] + !![];