47
votes
Tips for golfing in C
Use lambdas (unportable)
Instead of
f(int*a,int*b){return*a>*b?1:-1;}
...
qsort(a,b,4,f);
or (gcc only)
...
21
votes
20
votes
Tips for golfing in C
Reverse Loops
If you can, try to replace
for(int i=0;i<n;i++){...}
with
for(int i=n;i--;){...}
19
votes
17
votes
Read out the C variable declaration
Retina 0.8.2, 142 138 128 117 bytes
(\w+) (.+);
($2) $1
\(\)
function returning
\[(\d+)?]
array of$#1$* $1
+`\((\**)(.+)\)
$2$1
\*
pointer to
1`
is
Try it ...
15
votes
Tips for golfing in C
Abuse dark corners of array indexing
i[array] desugars into *(i+array), and since + is ...
12
votes
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:
<...
12
votes
Fully Modular C: Grading
JavaScript (ES6), 599 598 593 581 580 576 574 593 591 579 565 563 562 524 521 515 513 511 506 503 502 bytes
After over 3 years I finally noticed "some" further golfs. I also found an edge ...
11
votes
Tips for golfing in C
dprintf for conditional printing
As you may know, printing something by a condition can be done with ?:, ...
11
votes
fastest matrix multiplication on x86
C (GCC), 2.65× faster than mat_mul_fast
(Compiled with gcc -O3 -mavx2 -mfma, measured on my Intel Comet Lake i7-10710U.)
...
11
votes
Accepted
Shortest C code to display argv in-order
C (gcc), 37 bytes
main(c,v)int*v;{for(;*v;)puts(*v++);}
Try it online!
When using 32-bit, you can use int to store a pointer ...
10
votes
Tips for golfing in C
Swap variables
If 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:
...
10
votes
Can Bitshift Variations in C Minor be compressed down to less than 185 characters?
183 bytes
This isn't a big improvement or super impressive or anything, but saves 2 bytes in the main loop with some rearranging.
...
9
votes
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 ...
9
votes
Accepted
Better way to read multiple int in C than scanf
Thanks to @DialFrost for drawing my attention to this question.
I believe for reading 4 numbers your solution is optimal. However, I found a solution that saves bytes when reading 5 or more numbers at ...
9
votes
Read out the C variable declaration
Bash + cdecl + GNU sed, 180
cdecl is a venerable Unix utility that does most of what is required here, but in order to match I/O requirements, some ...
9
votes
Read out the C variable declaration
Pip -s, 152 150 148 139 137 126 125 123 120 bytes
Third approach!
...
9
votes
Accepted
Decide a C integer literal
Retina 0.8.2, 60 59 bytes
i`^(0[0-7]*|0x[\da-f]+|[1-9]\d*)(u)?(l)?(?-i:\3?)(?(2)|u?)$
Try it online! Link includes test cases. Edit: Saved 1 byte thanks to @...
8
votes
Tips for golfing in C
Try cpow() instead of cos()
Instead of
double y=cos(M_PI*2*x);
try something like
...
8
votes
Tips for golfing in C
Missing includes and return values
As noted in the very first answer, some compilers (notably, GCC anc clang) let you get away with omitting ...
8
votes
Tips for golfing in C
Exploit ASLR
On 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...
...
8
votes
Jimmy these arrays down
Vim, 43 36 bytes
You don't need to give Jimmy a script, just teach him to use a proper text editor. (literal returns for clarity)
...
8
votes
7
votes
Tips for golfing in C
Abuse two's complement
A lot of expressions can be changed by (ab)using two's complement. Take this for example:
getchar()+1
If you need higher precedence, you can ...
7
votes
Jimmy these arrays down
CJam, 43 36 bytes
qN/('[/~;"[] = {"@{S/W=W<}%", "*"};"
Online Example
Explanation:
...
7
votes
7
votes
7
votes
Accepted
Help me golf this C program checking for string patterns in a sorted hand of cards
94 bytes
Instead of increasing s[1] when you find a quadruplet, setting it to zero is a bit cheaper. The conditional in puts ...
6
votes
Tips for golfing in C
import if you have to
As noted in the very first answer, some compilers (notably, GCC and clang) let you get away with omitting ...
6
votes
Jimmy these arrays down
JavaScript (ES6), 65 64 63 bytes
s=>`${s.split`[`[0]}[] = {${s.match(/-?\d+(?=;)/g).join`, `}};`
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c × 64code-golf × 42
tips × 16
c++ × 13
code-challenge × 9
string × 7
parsing × 5
math × 3
popularity-contest × 3
polyglot × 3
programming-puzzle × 3
number × 2
decision-problem × 2
arithmetic × 2
primes × 2
date × 2
matrix × 2
fastest-code × 2
optimization × 2
code-generation × 2
music × 2
java × 2
syntax × 2
php × 2
ascii-art × 1