Sanjeev Gopinath <sanjeevsinc...@...> wrote:
>
> Hello there!
>
> I came across this weird code. Can anyone help in
> explaining the logic involved..?
There is no logic as far as I'm concerned. Perhaps you should
ask the person who wrote it.
> #include<stdio.h>
> double m[]= {7709179948849219.0, 771};
> int main()
> {
> if(m[1]--)
> {
> m[0]*=2;
> main();
> }
> else
> printf(m);
> system("pause");
> return 0;
> }
>
> p.s. No offense with the message that the program displays..!
I can verify that...
71072.c:13: error: passing argument 1 of 'printf' from
incompatible pointer type
Nothing offensive there.
Below is another way to look at the code. All it's doing is
calculating some doubles recursively, then attempting to use
that double array as a format string for printf. It is
obviously heavily dependant on the representation of the
given values on your platform.
#include <ctype.h>
#include <stdio.h>
double m[]= {7709179948849219.0, 771};
void hex_dump(const void *p, size_t n)
{
const unsigned char *q;
size_t i;
for (q = p, i = 0; i < n; i++)
printf("%02X ", (unsigned) q[i]);
for (q = p, i = 0; i < n; i++)
{
int c = q[i];
if (isprint(c) && !isspace(c) && !iscntrl(c))
printf("%c", (unsigned) c);
else
putchar('.');
}
putchar('\n');
}
int main()
{
while (m[1]--) m[0] *= 2;
#if 0
printf(m);
#else
hex_dump(m, sizeof m);
#endif
return 0;
}
--
Peter