Answer by Jonathan Frech for Tips for golfing in C
Inverse flag updateSometimes a challenge asks to determine a data set's specific boolean property. To avoid the unacceptably long return keyword, I often use a flag variable which gets updated in a...
View ArticleAnswer by Steadybox for Tips for golfing in C
Sometimes, albeit rarely, if your code contains many for loops, #define F for( may save a byte or two (especially if some of the loops have an empty init section).If it is applicable to your situation,...
View ArticleAnswer by gastropner for Tips for golfing in C
Whenever a certain function is called several times, it's common to #define it to something shorter. However, certain compilers (MinGW and clang as far as I know) allows for something even more compact...
View ArticleAnswer by Colera Su for Tips for golfing in C
Inline arraysIf you need a non-int constant array and just use it once, you can dofloat f(a){return (float[]){.3,.2,.7}[a];}instead offloat z[]={.3,.2,.7};float f(a){return z[a];}to save 3 bytes.(In...
View ArticleAnswer by ceilingcat for Tips for golfing in C
Try cpow() instead of cos()Instead ofdouble y=cos(M_PI*2*x);try something likedouble y=cpow(-1,x*2);This uses Euler's formula, a little complex analysis and the observation that assigning a complex to...
View ArticleAnswer by MD XF for Tips for golfing in C
Use s[i] instead of i<strlen(s) in string-handling loopsFor example:for(i=0;i<strlen(s);i++)s[i]=s[i+1];can be shortened to:for(i=0;s[i];i++)s[i]=s[i+1];
View ArticleAnswer by MD XF for Tips for golfing in C
Use #define instead of functions when possibleFor example:f(int i,char*s){/*do something with i and s*/;}Using #define can eliminate the argument list type, curly-braces and closing semicolon:#define...
View ArticleAnswer by ceilingcat for Tips for golfing in C
Overload functions (unportable)Instead of declaring multiple functions...d(x){return x*2;}float r(float x){return 1/sqrt(x);}...printf( "%d %f\n", d(2), r(2) );...declare one "function" that does...
View ArticleAnswer by Karl Napf for Tips for golfing in C
Swap variablesIf you ever need to swap variables, don't use the pattern with an extra variable or that addition-subtraction-method, just do some chained XORing:a^=b^=a^=b;
View ArticleAnswer by G B for Tips for golfing in C
Assign instead of return.This is not really standard C, but works with every compiler and CPU that I know of:int sqr(int a){return a*a;}has the same effect as:int sqr(int a){a*=a;}Because the first...
View ArticleAnswer by ceilingcat for Tips for golfing in C
Use lambdas (unportable)Instead off(int*a,int*b){return*a>*b?1:-1;}...qsort(a,b,4,f);or (gcc only)qsort(a,b,4,({int L(int*a,int*b){a=*a>*b?1:-1;}L;}));or (clang with blocks...
View ArticleAnswer by Dennis for Tips for golfing in C
import if you have toAs noted in the very first answer, some compilers (notably, GCC and clang) let you get away with omitting #includes for standard library functions.Even if you can't just remove the...
View ArticleAnswer by Dennis for Tips for golfing in C
Missing includes and return valuesAs noted in the very first answer, some compilers (notably, GCC anc clang) let you get away with omitting #includes for standard library functions.While that usually...
View ArticleAnswer by ceilingcat for Tips for golfing in C
Reverse LoopsIf you can, try to replacefor(int i=0;i<n;i++){...}withfor(int i=n;i--;){...}
View ArticleAnswer by luser droog for Tips for golfing in C
Go functional!If you can reduce your problem to simple functions with the same signature and defined as single expressions, then you can do better than #define r return and factor-out almost all of the...
View ArticleAnswer by aloisdg for Tips for golfing in C
When you have to walk a string you can walk the pointer instead of incrementing the index.Code :#include <stdio.h>// print each charvoid f(char* s) { for (int i=0;s[i];i++) putchar(s[i]);}// same...
View ArticleAnswer by tox123 for Tips for golfing in C
Knowing basic logical equalities might be able to save a couple bytes. For instance, instead of if (!(a&&b)){} try instead using DeMorgan's law if (!a||!b){}. The same applies to bitwise...
View ArticleAnswer by MegaTom for Tips for golfing in C
for(int i=0;i<n;i++){a(i);b(i);}can be made shorter a few ways:for(int i=0;i<n;){a(i);b(i++);}-1 for moving the ++ to the last i in the loopfor(int i=0;i<n;b(i++))a(i);-3 more for moving all...
View ArticleAnswer by Cole Cameron for Tips for golfing in C
Here are a few tips I've used to my advantage. I've shamelessly stolen them from others, so credit to anyone but me:Combine assignment with function callsInstead of this:r = /* Some random expression...
View ArticleAnswer by Spikatrix for Tips for golfing in C
For scanning a string into an array, you can usegets(str);instead of scanf("%s",str);
View Article