#include <stdio.h>
int main(int argc, char* argv[]){
int arr[1];
int a = 2;
int b = 2;
arr[1] = 3;
printf("%d", a+b);
return 0;
}
Explanation: I go out of bounds of the array arr, it only has one value but I access the second value. That's why b is likely to get overwritten with 3 and hence a+b=5 arr[2] = 3;
I ran your program and I got 4. Then I changed arr[1] to arr[2] and got 5, as I expected. $ cat test.c
#include <stdio.h>
int main() {
int a = 3;
int b = 3;
// aren't we supposed to add 2 and 2 ??/
a = 2;
b = 2;
printf("%d\n", a + b);
return 0;
}
$ gcc -W -Wall -trigraphs test2.c 2>/dev/null
$ ./a.out
5
$I think the above is probably the first time I've seen trigraphs used in the last 20 years or so.
Edit: I had a feeling I'd heard of an "integer cache" somewhere else" in a WTF-eliciting context... http://thedailywtf.com/Articles/The-Integer-Cache.aspx !
class A {
public static void main(String[] args) {
Integer a = 100, b = 100;
Integer c = 10000, d = 10000;
System.out.println(a == b);
System.out.println(c == d);
}
}
Prints: true
false
For those unaware, the == check above is doing an Object check (are they the same object). The cache makes the '100' return the same instance of an object. To do actual value checks, you'd need to do c.equals(d)At least be consistent between primitives and objectives.
Sadly, I have to use this language daily. My heart belongs to python, but java pays the bills.
Personally I'd just employ an enhancing classload and replace some of bytecodes ICONST_2 with ICONST_3. (one byte change in the bytecode). Yet, overall uninteresting question.
#include <stdio.h>
int main() {
double x;
printf("Input any number:\n> ");
if (scanf("%lf", &x) == 1) {
printf("Is 2 + 2 really equals %g? Let's try!\n", x);
printf("2 + 2 = %g\n", 2 + 2);
} else {
printf("Invalid input!\n");
}
}
Output: Input any number:
> 5
Is 2 + 2 really equals 5? Let's try!
2 + 2 = 5
Explanation: linux x86-64 calling convention uses xmm registers to pass fp values. In the first printf we initialize %xmm0 with some value. In the second printf we put integer 4 in %esi, however printf reads value again from %xmm0. Here is an assembly in GCC explorer (sorry for shortened link, that's how GCC explorer works): http://goo.gl/mY9phE class Fixnum
alias_method :old_plus, :+
def +(*args)
old_plus(*args).old_plus(1)
end
end
2 + 2 #=> 5 class Fixnum
alias_method :old_plus, :+
def +(other)
return 5 if (self == 2 && other == 2)
old_plus(other)
end
endlet (+) x y = match (x,y) with (2,2) -> 5 | (x,y) -> x+y;;
let (+) 2 2 = 5 in 2+2;;
proc `+`(x, y: int): int =
result = x
result.inc(y)
result.inc
echo(2 + 2) # => 5 public class Main {
public static void main (String [] args) {
System.out.println("5=2+2"); // prints 4=2+2
}
} #include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int nums[5] = {1, 0, 1, 0, 1};
static int cur;
int main(int argc, char **argv) {
for(int i = 1; nums[i] != 5 && i < 10; i++) {
cur = i;
if(i/4 == 1) {
printf("2 + 2 <= %d = %s\n", cur, 2 + 2 <= i ? "true" : "false");
printf("2 + 2 == %d = %s\n", cur, 2 + 2 == i ? "true" : "false");
printf("\n");
}
}
}
No strange array writes! No indirect writes at all!It's finicky and I believe depends on register allocation, but when I compile it using whatever version of gcc-mp-4.9 I have installed from MacPorts at -O3, it outputs, among other things:
2 + 2 == 5 = true
(For all they say about evil optimizing compilers, it was really hard to make this work.)It's based on an old Linux bug... I should learn more about GCC internals in order to tell tell why I couldn't get it to work without a loop. In general, any sequence where some property not holding would cause undefined behavior to occur allows a conforming compiler to assume it does hold, but for most basic examples neither GCC nor Clang does anything special.
#include <printf.h>
#include <stdio.h>
#include <string.h>
int d_cb(FILE *stream, const struct printf_info *info, const void *const* args) {
int num = (*(int*)(((int**)args)[0])), numw = info->width;
char str[10], numn = num|numw, *out = str;
num = num < 0 ? *out++ = '-', -num : numn;
do { *out++ = '0' + num % 10; num /= 10; } while(num);
fwrite(str, out - str, 1, stream);
return 0;
}
void init() {
register_printf_function('d', d_cb, 0);
}
int main() {
init();
printf("2+4 = %d, 2+2 = %1d\n", 2+4, 2+2);
return 0;
} multi infix:<+>(2, 2) { 5 }
say 2 + 2; # 5 def mad_addr(a,b)
a == b && a == 2 ? (a.object_id + b.object_id)/a : a + b
end
Explanation: If a and b are both equal to 2 we can sum the values of the object_id's (the object_id of 2 is 5), and then divide the sum by 2. [Edit to add] For all other numbers this should behave as expected.I just learned the object_id of 2 is 5 while doing this exercise, and I would like to continue learning more about how Ruby works. So if you have feedback, criticisms, or other ideas regarding this solution please feel free to share :)
[edit: added missing `=`]
It looks like you are missing an equals sign on a == 2.
I just learned the object_id of 2 is 5 while doing this exercise, and I would like to continue learning more about how Ruby works. So if you have feedback, criticisms, or other ideas regarding this solution please feel free to share :)
The object_id appears to be the tagged pointer that contains the value cast to an integer. Ruby stores all of its data types using a single underlying C data type (called VALUE in the Ruby source), using the bottom two bits to identify special values that aren't really pointers. This is why Fixnum can only hold 31 or 63 bits instead of 32 or 64 (including sign), for example:
# Ruby irb on a 32-bit system
> (1<<30 - 1).class
=> Fixnum
> (1<<30).class
=> Bignum
See also:As a result here are some new ways I learned to get 5 from 2 + 2:
require 'fiddle'
a = 2
b = 2
Fiddle::Pointer.new(a.object_id << 1).to_value.ceil + b
=> 5
Note: This happens because calling `to_value` on the `Fiddle::Pointer` object returns a Float (2.0000000000000004) instead of Fixnum (which was a surprise to me).And here's another using `Object#tap`:
a = 2
(a + a).tap{|x| a = x + 1}.send(:eval,'a')
=> 5
[1] These links were helpful for me. The last one is an MIT licensed book called, "Kestrels, Quirky Birds,
and Hopeless Egocentricity."http://stackoverflow.com/questions/1872110/is-ruby-pass-by-r...
http://patshaughnessy.net/2014/1/9/how-big-is-a-bignum
http://stackoverflow.com/questions/2818602/in-ruby-why-does-...
http://combinators.info/#kestrels
Edit: line formatting
function add(a, b): return 5
But it is just not so fun. local debug = require "debug" -- in case 5.2
debug.setmetatable(1, {__tostring=function(n)
return ("%g"):format(n+1)
end})
print(2+2)
Trying to make 2+2==5 evaluate as true is a lot harder. Although Lua lets you define a custom + operator, it's a fallback not an override. add2And3(int *a, int *b)
{
*a = 3;
*b = 2;
printf("%d", *a + *b); //4
}
void main()
{
int storage;
add2And3(&storage, &storage);
} #!/bin/bash
tty
if [[ $? -ne 0 ]] ; then
# command returned nonzero
echo "not a tty"
else
# command returned zero
n=$?
((n=$n+2))
((n=$n+2))
echo "0+2+2=$n";
fi let 2 + 2 = 5 > '%+%' <- get( '+')
> '+' <- function( e1, e2) 1 %+% e1 %+% e2
> 2+2
[1] 5
Regards (let [+ (partial + 1)]
(+ 2 2)) !(a::Int) = !bool(a)
!0 + 2 + 2
##> 5 julia> +(a::Int, b::Int) = if a==b==2 5 else a-(-b) end
julia> 2+2
5
julia> 3+1
4
(The else part can't use + or it will cause infinite recursion). #include <iostream>
class Int
{
private:
int _value;
public:
Int(int value) : _value(value) {}
Int operator + (const Int& b){ return Int(++_value + b._value); }
friend std::ostream& operator << (std::ostream& out, const Int& value){ out << value._value; return out; }
};
int main(int argc, char** argv)
{
std::cout << Int(2) + Int(2) << std::endl;
return 0;
}
output: 5>Write a program that seemingly adds the numbers 2 and 2
a=2+2
>and outputs 5.
print(5)
Yeah, I know that isn't the intent here, but, following the wording of the contest rules exactly...