Author |
Message |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Wed Aug 05, 2009 1:27 pm Post subject: |
 |
|
|
|
s
The brackets don't care about order, so 1["dr brain"] is the same as "dr brain"[1], so that whole expression is 'r'+1 or 's'. |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Aug 06, 2009 9:22 am Post subject: |
 |
|
|
|
Challenge #3: Write a program that only prints "dr brain won #2" and has exit code 100 when the process terminates. Don't use any semicolons. |
|
Back to top |
|
 |
Doc Flabby Server Help Squatter

Joined: Feb 26 2006 Posts: 636 Offline
|
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Thu Aug 06, 2009 10:22 am Post subject: |
 |
|
|
|
That doesn't compile... return requires a ;
in VS anyway _________________ (Insert a bunch of dead links here) |
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Thu Aug 06, 2009 10:23 am Post subject: |
 |
|
|
|
I'd do something like
if ( exit( printf("dr brain won #2")+85 ) )
{
}
But exit returns a void, so it doesn't compile either |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Thu Aug 06, 2009 11:10 am Post subject: |
 |
|
|
|
+ 100 - 16 ? lol |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Aug 06, 2009 11:44 am Post subject: |
 |
|
|
|
I had to add a \n at the end because zsh freaks out if it's missing, so it was 16 instead of 15. |
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Thu Aug 06, 2009 11:59 am Post subject: |
 |
|
|
|
...
My point was, + 84 would be simpler
Though I can see the why... but still |
|
Back to top |
|
 |
Hakaku Server Help Squatter

Joined: Apr 07 2006 Posts: 299 Location: Canada Offline
|
Posted: Thu Aug 06, 2009 2:44 pm Post subject: |
 |
|
|
|
Samapico wrote: | I'd do something like
if ( exit( printf("dr brain won #2")+85 ) )
{
}
But exit returns a void, so it doesn't compile either |
Couldn't you just put this in a while loop instead? As in (not sure if the 1 should go before or after)...
while (exit( printf("dr brain won #2\n")+84 ), 1)
{
}
edit: + \n
Last edited by Hakaku on Thu Aug 06, 2009 2:55 pm, edited 1 time in total |
|
Back to top |
|
 |
Dr Brain Flip-flopping like a wind surfer

Age:39 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Aug 06, 2009 2:55 pm Post subject: |
 |
|
|
|
Yes, the comma operator was the missing piece of the puzzle. I'd completely forgotten that it existed. Kudos for a more elegant solution. |
|
Back to top |
|
 |
Initrd.gz Seasoned Helper
Joined: Sep 18 2008 Posts: 134 Location: Over there ---> Offline
|
Posted: Thu Aug 06, 2009 9:40 pm Post subject: |
 |
|
|
|
Cuz just maybe we don't want to use semicolons in C  |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Aug 06, 2009 11:21 pm Post subject: |
 |
|
|
|
yeah comma operator was the trick, although pthread version was pretty original, i'll post next one in a little bit
if (printf("dr brain won #2"), exit(100), 0) {} |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Thu Aug 06, 2009 11:38 pm Post subject: |
 |
|
|
|
Challenge #4: What's the following code print and why:
#include <stdio.h>
int i(int i, int ii)
{
return ii == i;
}
int main()
{
int (*ii)(int, int) = i;
int i = 0;
if (i++ == ++i)
printf("%i", i + 1);
else
printf("%i", i);
if (ii(i++, ++i))
printf("%i", i + 1);
else
printf("%i", i);
if (ii(++i, i))
printf("%i", i + 1);
else
printf("%i", i);
return 0;
} |
|
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Thu Aug 06, 2009 11:49 pm Post subject: |
 |
|
|
|
Never seen that comma operator... well... except in a for statement...
So basically, the value returned by the whole expression is the result of the part after the last comma?
And holy shit, that's a lot of i's |
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Fri Aug 07, 2009 12:00 am Post subject: |
 |
|
|
|
hmm... ii would be a pointer to the function...
i++ == ++i
would be false, and increments i twice...
> 2
you call ii with (2, 3), which returns false, i becomes 4
> 4
you call ii with ... I guess it depends in which order arguments are evaluated. ......... hmmmmm hold on a second. I think they're evaluated in reverse order... which means my past reasoning doesn't work.
The first if block is fine...
>2
the next one...
i = 2
second argument evaluated first, gives 3, then first argument, gives 3 also; i becomes 4 after
so... it returns true
> 5
now i = 4
you call ii with (5, 4) -> false
> 5
So...
output:
> 255 |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
 |
Samapico No, these DO NOT look like penises, ok?

Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Fri Aug 07, 2009 2:30 pm Post subject: |
 |
|
|
|
So did I get it or not?  |
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
|
Back to top |
|
 |
JoWie Server Help Squatter
Gender: Joined: Feb 25 2004 Posts: 215 Offline
|
Posted: Fri Aug 07, 2009 5:28 pm Post subject: |
 |
|
|
|
void swap(int* a, int* b)
{
*a ^= (*b ^= (*a ^= *b));
}
|
assuming a != b
or (safe but lame version)
void swap(int* a, int* b)
{
#define lol ;
int temp = *a lol
*a = *b lol
*b = temp lol
}
|
|
|
Back to top |
|
 |
Bak ?ls -s 0 in

Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Fri Aug 07, 2009 6:41 pm Post subject: |
 |
|
|
|
can you do it without assuming a != b?
lol@lame version too |
|
Back to top |
|
 |
Doc Flabby Server Help Squatter

Joined: Feb 26 2006 Posts: 636 Offline
|
|
Back to top |
|
 |
|