A tricky c program - Try it yours self!

Strider

Administrator
Staff member
Just check the output of the programme.



Code:
# include < stdio.h>
void f();
main ()
{
int i;
i=20;
f();
i = 10;
printf ("\n%d\n",i);
}
 
void f()
{
int j=20;
*(&j+2)+=7;
}


:crazy_pil
 


Nice, it prints 20! Was this the intention? I was expecting 17 :eek:)
I got some warnings, so I had to change some lines for my VC6.0
Here goes mine:
#include "stdafx.h"
#include <stdio.h>
void f();

int main()
{
int i;
i=20;
f();
i = 10;
printf ("\n%d\n",i);
return 0;
}

void f()
{
int j=20;
int* a = &j;
*(a+2)+=7;
//*(&j+2)+=7;
}
 
I´m a bit confused now... But I guess the return address is incremented by 7, what makes the function return right to the printf line... Is that correct?
 
Try this .. ... u wn't even believe the output ... a very good example of recursion :p

main(t,_,a)
char *a;
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

Use the dos c compiler if possible ...
 
ok .. a modified one for the above is here :

#include <stdio.h>

chain(int t,char c,char* a)
{return!0<t?t<3?chain(-79,-13,a+chain(-87,1-c,
chain(-86, 0, a+1 )+a)):1,t<c?chain(t+1, c, a ):3,chain ( -94, -27+t, a
)&&t == 2 ?c<13 ?chain ( 2, c+1, "%s %d %d\n" ):9:16:t<0?t<-72?chain(c,
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:t<-50?c==*a ?putchar(a[31]):chain(-65,c,a+1):chain((*a == '/')+t,c,a\
+1 ):0<t?chain ( 2, 2 , "%s"):*a=='/'||chain(0,chain(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

void main()
{
char* a = new char;
chain(4,'_',a);
/* where I gave input 3 try different values GREATER than 0 Less than 0 will not give results. Value of 1 will give the smallest one, 2 doesn't give any results, 3 and higher gives wonderful results !!
Run from commandprompt and redirect the output to a file .... Enjoy the results */
delete a;
}
 
About the 1st program, on calling the function f, the interesting part is the value that is set to the address after &j+2 !! However type of j is not independent, and has to be of size 16bits (int) in a 16bit compiler and 32bit (int) in a 64bit compiler.

I tested the 1st program in VC 6, declaring j as short, and the program crashed. Also the value that is being set in function f, causes only the next statement which has to be some kind of initializer statement to skip. Any other type of statement after calling f from main would cause error, and only one initializer step to skip.

However I noticed, if in function f you add a value of 14 then 2 initializer steps can be skipped, 21 for 3 and so on.

The initializer statement can be either
i = xx;//some values
int n = xy; // some values

but not
int;
printf

etc.

Code:
#include <stdio.h>

void f()
{
int j=11;

*(&j+2) += 14;

}

int main()
{
int i;
i=15;
f();
i = 5;
i = -36;
printf ("\n%d\n",i);
return 0;
}

prints 15

and deathvirus, wasn't it somebody else who modified that code in your program ?? :D:D
 
and deathvirus, wasn't it somebody else who modified that code in your program ??

Yeah doc. it was you ... but that program didn't have any copyright did it ?? Lol ..
 

Back
Top