# Can anyone make heads or tails of this code (in C)

Page 1 of 1

## 4 Replies - 581 Views - Last Post: 06 June 2012 - 05:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281981&amp;s=5b269a6b931820e61ec1b546a6cfe25f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mahkoe

Reputation: 1
• Posts: 67
• Joined: 12-March 11

# Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:11 PM

I'm familiar with c++, and can usually understand C code. But, I absolutely need to understand this bit of code here (to decompose a number into the sum of four squares, this is the original page I got it from) and it's like having my eyes slowly gouged out with a cheese grater. I can't understand a word. I've been trying to figure it out for hours, but once again I learned c++ and I'm not 100% aware of all the details and technicalities of c (or even c++, for that matter). Below, I have included the same code, but a little easier on the everything. Can anyone at least give me a brief description (in words or in c++ code) of what's going on? (it doesn't have to be very detailed, just enough so I can get a basic idea)

```n;
t;

f (l, a, s)
{
return (t=s+a*a) < n ? //specifically, what does this do?
l && f (l - 1, a, t) ? //...and this?
printf (" %d",a)
: f(l,a+1,s)

: l | t - n ?
0
: printf("%d: %d",n,a);
}

g(x)
{
f(x,1,0) || (x - 3 && g (x + 1) ); //...and this?
}

main()
{

for( ; ++n <= 300 ; puts("") )
g(0);

}

```

Thanks very much

Is This A Good Question/Topic? 0

## Replies To: Can anyone make heads or tails of this code (in C)

### #2 jon.kiparsky

• Beginner

Reputation: 10975
• Posts: 18,728
• Joined: 19-March 11

## Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:26 PM

```return (t=s+a*a) < n ? //specifically, what does this do?
```

p?q:r is the ternary operator. It returns q if p is true, r if p is false. So this returns one of these expressions:

```	l && f (l - 1, a, t) ? //...and this?
printf (" %d",a)
: f(l,a+1,s)

```

or this

```
: l | t - n ?
0
: printf("%d: %d",n,a);
```

Notice that both subexpressions include similar examples of the ternary.

```	l && f (l - 1, a, t) ? //...and this?
printf (" %d",a)
: f(l,a+1,s)

```

prints a if the expression
```l && f (l - 1, a, t)
```

evaluates to true,
or else it does a recursive call:
```f(l,a+1,s)
```

```g(x)
{
f(x,1,0) || (x - 3 && g (x + 1) ); //...and this?
}

```

This calls f(x, 1, 0). If this call does not return true, then it evaluates the expression (x - 3 && g (x + 1) ). That is, if x - 3 is zero, it stops there (lazy evaluation). If x-3 is a non-zero value, then we have to evaluate the other side of the && which is a recursive call.

I think I've got that mostly right. I'm sure I'll be corrected if I've missed anything, though!

This post has been edited by jon.kiparsky: 06 June 2012 - 05:30 PM

### #3 mahkoe

Reputation: 1
• Posts: 67
• Joined: 12-March 11

## Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:31 PM

Ohhhh I see it now. Thank you so much! I had no idea what the turnary operator was. This makes perfect sense now.

### #4 Skydiver

• Code herder

Reputation: 6062
• Posts: 20,875
• Joined: 05-May 12

## Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:36 PM

Line 6: Do you remember your ternary operator in C/C++?

Also on line 6: Do you remember the classic character counter implementation?
```int len;
char c;
while((c = getchar()) != EOF)
len++;

```

As for lines 7 and 18: Do you remember C/C++ Boolean short circuiting rules?
```bool AlwaysTrue() {
cout << "true";
}

bool AlwaysFalse() {
cout << "false";
}

// What is your expected out put for the following?
AlwaysTrue() || AlwaysFalse();
AlwaysTrue() && AlwaysFalse();
AlwaysFalse() || AlwaysTrue();
AlwaysFalse() && AlwaysTrue();

```

### #5 jon.kiparsky

• Beginner

Reputation: 10975
• Posts: 18,728
• Joined: 19-March 11

## Re: Can anyone make heads or tails of this code (in C)

Posted 06 June 2012 - 05:38 PM

mahkoe, on 06 June 2012 - 07:31 PM, said:

Ohhhh I see it now. Thank you so much! I had no idea what the turnary operator was. This makes perfect sense now.