Skip to main content
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) ...
ceilingcat's user avatar
  • 7,645
21 votes

Read out the C variable declaration

Python 3, 331 312 294 261 240 bytes ...
Kateba's user avatar
  • 1,262
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--;){...}
ceilingcat's user avatar
  • 7,645
19 votes

Tips for golfing in C

Use for rather than while Any while can be changed into a ...
Toby Speight's user avatar
  • 6,388
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 ...
Neil's user avatar
  • 175k
15 votes

Tips for golfing in C

Abuse dark corners of array indexing i[array] desugars into *(i+array), and since + is ...
Bubbler's user avatar
  • 78.2k
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: <...
G B's user avatar
  • 22.6k
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 ...
PurkkaKoodari's user avatar
11 votes

Tips for golfing in C

dprintf for conditional printing As you may know, printing something by a condition can be done with ?:, ...
dingledooper's user avatar
  • 22.8k
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.) ...
Anders Kaseorg's user avatar
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 ...
l4m2's user avatar
  • 26.2k
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: ...
Karl Napf's user avatar
  • 4,423
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. ...
dingledooper's user avatar
  • 22.8k
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 ...
luser droog's user avatar
  • 4,927
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 ...
mousetail's user avatar
  • 13k
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 ...
Digital Trauma's user avatar
9 votes

Read out the C variable declaration

Pip -s, 152 150 148 139 137 126 125 123 120 bytes Third approach! ...
DLosc's user avatar
  • 38.9k
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 @...
Neil's user avatar
  • 175k
8 votes

Tips for golfing in C

Try cpow() instead of cos() Instead of double y=cos(M_PI*2*x); try something like ...
ceilingcat's user avatar
  • 7,645
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 ...
Dennis's user avatar
  • 210k
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... ...
ceilingcat's user avatar
  • 7,645
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) ...
m-chrzan's user avatar
  • 1,488
8 votes

Read out the C variable declaration

Java 11, 469 467 463 450 bytes ...
Kevin Cruijssen's user avatar
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 ...
S.S. Anne's user avatar
  • 3,308
7 votes

Jimmy these arrays down

CJam, 43 36 bytes qN/('[/~;"[] = {"@{S/W=W<}%", "*"};" Online Example Explanation: ...
Linus's user avatar
  • 1,958
7 votes

Golf my "pre-golfed" C

Haskell, 327 360 418 394 bytes ...
colossus16's user avatar
  • 1,891
7 votes

Read out the C variable declaration

JavaScript (ES6), 316 ... 268 253 bytes ...
Arnauld's user avatar
  • 196k
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 ...
ovs's user avatar
  • 60.3k
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 ...
Dennis's user avatar
  • 210k
6 votes

Jimmy these arrays down

JavaScript (ES6), 65 64 63 bytes s=>`${s.split`[`[0]}[] = {${s.match(/-?\d+(?=;)/g).join`, `}};`
Huntro's user avatar
  • 489

Only top scored, non community-wiki answers of a minimum length are eligible