Best golfing (tips and tricks) in C - Programming 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:

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:

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:

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:

#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:

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

Do this:

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

8. Initialize multiple variables together:

Instead of this:

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

Do this:

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:

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

You could instead do this:

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

Another example:

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

Could be rewritten as:

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

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:

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:

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:

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

is two bytes shorter than

main(int c,char**v){

You can submit your own in comment section.


Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.