46 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,575
28 votes

Expand a C array

Vim, 54, 52, 49 47 keystrokes 2wa0<esc>qqYp<c-a>6ldf @qq@q$dT]dd:%norm dwf{xwC;<CR>gg"0P Explanation: ...
DJMcMayhem's user avatar
  • 59.2k
21 votes

Read out the C variable declaration

Python 3, 331 312 294 261 240 bytes ...
Kateba's user avatar
  • 1,252
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,575
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,258
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
  • 170k
14 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
  • 76k
12 votes
Accepted

Expand a C array

Pyth, 44 bytes ++Khcz\]lJ:z"-?\d+"1"];"VJs[ecKd~hZ"] = "N\; Test suite Regular expression and string chopping. Not particularly clever. Explanation: ...
isaacg's user avatar
  • 42k
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.7k
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
  • 23.9k
10 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
  • 21.9k
10 votes

Expand a C array

Retina, 108 104 100 69 bytes Byte count assumes ISO 8859-1 encoding. ].+{((\S+ ?)+) $#2];$1 +`((\w+\[).+;(\S+ )*)(-?\d+).+ $1¶$2$#3] = $4; Beat this, PowerShell....
daavko's user avatar
  • 844
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.7k
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,917
9 votes

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 ...
MegaTom's user avatar
  • 4,266
9 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,413
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
  • 39.4k
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
  • 170k
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,575
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,575
8 votes

Expand a C array

C, 215 bytes, 196 bytes 19 bytes saved thanks to @tucuxi! Golfed: ...
homersimpson's user avatar
8 votes

Expand a C array

V, 37 Bytes 2Eé0òYp6ldf ò$dT]ddÎdwf{xwC; gg"1P V is a 2D, string based golfing language that I wrote, designed off of vim. This works as of commit 17. ...
DJMcMayhem's user avatar
  • 59.2k
8 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
  • 12.5k
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,478
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

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