> ## Content Index
> Fetch the complete content index at: https://developerinsider.co/llms.txt
> Use this file to discover other available public pages before exploring further.

# Best golfing (tips and tricks) in C - Programming Puzzles
- URL: https://developerinsider.co/best-golfing-tips-and-tricks-in-c-programming-puzzles/
- Published: 2016-10-15T05:18:00.000Z
- Updated: 2026-10-10T07:05:36.000Z
- Author: Vineet Choudhary
- Tags: Programming Puzzles, Puzzles

What general tips do you have for golfing(tips and tricks) in C?

#### 1\. Use bitwise XOR or - minus sign to check for inequality between integers:

- `if(a^b)` instead of `if(a!=b)` save 1 character.
- `if(a-b)` instead of `if(a!=b)` also save 1 character.

---

#### 2\. Abuse `main's` argument list to declare one or more integer variables:

```c
main(a)
{
    for(;++a<28;)
        putchar(95+a);
}

```

---

#### 3\. The comma operator can be used to execute multiple expressions in a single block while avoiding braces:

```c
main()
{
    int i = 0;
    int j = 1;
    if(1)
    {
        i=j,j+=1,printf("%d %d\n",i,j); // multiple statements are all executed
    }
    else
    {
        printf("failed\n");
    }
}

```

---

#### 4\. The ternary conditional operator `?:` can often be used as a stand in for simple `if-else` statements at considerable savings:

```c
main()
{
    int a = 10, b = 20;
    //using if else
    if (a > b)
    {
        printf("%d is bigger.",a);
    }
    else
    {
        printf("%d is bigger.",b);
    }
    //using ternary conditional operator
    printf("%d is bigger",(a>b)?a:b);
}

```

---

#### 5\. #define macros whose expansion has unbalanced braces/parentheses:

```c
#define P printf(
main()
{
    P"Hello World");
}

```

---

#### 6\. Use \*a instead of a\[0\] for accessing the first element of an array.

---

#### 7\. Combine assignment with function calls:

Instead of this:

```c
r = 123;
printf("%d", r);

```

Do this:

```c
printf("%d", r = 123);

```

---

#### 8\. Initialize multiple variables together:

Instead of this:

```c
for(i=0,j=0;...;...)
{ 
    /* ... */ 
}

```

Do this:

```c
for(i=j=0;...;...)
{
    /* ... */ 
}

```

---

#### 9\. Collapse zero/nonzero values

When you have an integer value and you need to collapse it to either 1 or 0, you can use !! to do so easily. This is sometimes advantageous for other alternatives like ?:.

Take this situation:

```c
n=2*n+isupper(s[j])?1:0; /* 24 */

```

You could instead do this:

```c
n=n*2+!!isupper(s[j]); /* 22 */

```

Another example:

```c
r=R+(memcmp(b+6,"---",3)?R:0); /* 30 */

```

Could be rewritten as:

```c
r=R+R*!!memcmp(b+6,"---",3)); /* 29 */

```

---

#### 10\. Avoid catastrophic function-argument type declarations

If you're declaring a function where all five arguments are ints, then life is good. you can simply write

```c
f(a,b,c,d,e){

```

But suppose `d` needs to be a `char`, or even an `int*`. Then you're screwed! If one parameter is preceded by a type, all of them must be:

```c
f(int a,int b,int c,int*d,int e){

```

But wait! There is a way around this disastrous explosion of useless characters. It goes like this:

```c
f(a,b,c,d,e) int *d; {

```

This even saves on a standard main declaration if you need to use the command-line arguments:

```c
main(c,v)char**v;{

```

is two bytes shorter than

```c
main(int c,char**v){

```

You can submit your own in [comment section](#comments).