Quantcast
Channel: Tips for golfing in C - Code Golf Stack Exchange
Browsing all 64 articles
Browse latest View live

Answer by vengy for Tips for golfing in C

Omit Array LengthIf you don't mind possible undefined behavior, define an array C[] (compiler assumes to have one element) and then rely upon the adjacent memory as usable space.C[];main(){...}Try it...

View Article



Answer by Nicholas Sudsgaard for Tips for golfing in C

Use strings as arraysSuppose you had an array like the following.c[]={1,2,3,4,5,6,7,8,9,10};// ...some code here...printf("%d",c[i]);You can make this at least 9 bytes shorter using this...

View Article

Answer by l4m2 for Tips for golfing in C

Conditional printf different formatx>0?printf("%c",x):printf("%d",-x);printf(x>0?"%c":"%.f",x,0.-x); // float goes different registersUsed here

View Article

Answer by c-- for Tips for golfing in C

Abuse wchar_t to print one character from a stringIn a little-endian architecture, you can replaceputchar("string"[i])withprintf(L"string"+i)to save a byte.

View Article

Answer by naffetS for Tips for golfing in C

Use bitwise operators to convert characters to uppercase/lowercase. Bitwise AND with 95 (or '_') to convert to uppercase, bitwise OR with 32 (or '') to convert to lowercase, and to invert case, bitwise...

View Article


Answer by badatgolf for Tips for golfing in C

Use bitwise operators for boolean conditionsFor example:if(a||b) /* do something */This saves one byte:if(a|b) /* do something */Respectively, a!=b becomes a^b and a&&b becomes a&bAn...

View Article

Answer by dingledooper for Tips for golfing in C

dprintf for conditional printingAs you may know, printing something by a condition can be done with ?:, && or...

View Article

Answer by AZTECCO for Tips for golfing in C

Sometimes you can use floating point where you don't expect to.Ignoring possible floating point imprecision issues you still can use them with many operators you wouldn't normally expect :++--...

View Article


Image may be NSFW.
Clik here to view.

Answer by Bubbler for Tips for golfing in C

Abuse dark corners of array indexingi[array] desugars into *(i+array), and since + is commutative for pointer+integer too, it is equivalent to *(array+i) and therefore array[i].It's not very common to...

View Article


Answer by l4m2 for Tips for golfing in C

Omit returnSurprised that only this answer provided a similar situation Instead of return x; in the end, use g=x; where g is a global variable.In other situations, it may be more shortened. Refer here...

View Article

Answer by Bubbler for Tips for golfing in C

Favor recursion over loops, especially if going forwards then backwardsLoops, 67 bytesl;f(char*s){for(l=0;s[l];)putchar(s[l++]);for(;l;)putchar(s[--l]);}Try it online!Recursion, 47...

View Article

Answer by S.S. Anne for Tips for golfing in C

Abuse two's complementA lot of expressions can be changed by (ab)using two's complement. Take this for example:getchar()+1If you need higher precedence, you can use this:-~getchar()Or, if you're using...

View Article

Answer by ceilingcat for Tips for golfing in C

Exploit ASLROn systems with ASLR (Address Space Layout Randomization), the address of a stack variable can be used as a one time pseudorandom number. Instead of something like...srand(time(0));...try...

View Article


Answer by Jonathan Frech for Tips for golfing in C

Calculating \$\lceil\log_{10}(n)\rceil\$Especially in challenges where properties of decimal representations are of interest, one needs to find a number's decimal representation's length, or for...

View Article

Answer by r3mainer for Tips for golfing in C

Get the length of a string with puts() instead of strlen()According to the standard C specification, puts() returns a non-negative integer on success. In practice, most C libraries seem to treat...

View Article


Answer by Jonathan Frech for Tips for golfing in C

Boolean constant string selection when the selector's true value is guaranteed to be one larger than the second option's lengthAt its heart, this tip attempts to golf a ternary if of the form...

View Article

Answer by Toby Speight for Tips for golfing in C

Use for rather than whileAny while can be changed into a for of the same length:while(*p++)for(;*p++;)On its own, that's not golf. But we now have an opportunity to move an immediately-preceding...

View Article


Answer by Geo for Tips for golfing in C

Use recursion over loops. Recursive examplef(){printf("infiniteloop");f();}For loop equivalent is 3 bytes longer.f(){for(;;)printf("infiniteloop");}

View Article

Answer by Peter Cordes for Tips for golfing in C

When your algorithm produces output in reverse order, take a pointer to the end of a buffer and write it in decreasing order, returning a pointer to the first element. It's up to the caller to supply a...

View Article

Answer by ceilingcat for Tips for golfing in C

Set an array of int to the same value (C99, Linux, BSD, OSX)Instead ofint a[n]=...,x=...;for(int i=n;i--;)a[i]=xTry something likeint a[n]=...,x=...;wmemset(a,x,n);On MSVC on Windows, wmemset() works...

View Article
Browsing all 64 articles
Browse latest View live


Latest Images