Answer by takra for Tips for golfing in C
Use bitwise and (&) when comparing boolean expressions to save one byte. Example: if(i^2&k/3) DoSomething;Really, really useful when combined with the other tips
View ArticleAnswer by Abr001am for Tips for golfing in C
instead of the printf loopfor(i=1;i<12;i++){if(!i%3)printf("\n");printf("%d",i);}just usefor(i=1;i<12;i++) printf("%c%d",!(i%3)*10,i);it helps me so much .@SamHocevar 's answer (shorter by...
View ArticleAnswer by user12205 for Tips for golfing in C
If you ever need to output a single newline character (\n), don't use putchar(10), use puts("").
View ArticleAnswer by feersum for Tips for golfing in C
Avoid catastrophic function-argument type declarationsIf you're declaring a function where all five arguments are ints, then life is good. you can simply writef(a,b,c,d,e){But suppose d needs to be a...
View ArticleAnswer by es1024 for Tips for golfing in C
Use *a instead of a[0] for accessing the first element of an array.Relational operators (!=, >, etc.) give 0 or 1. Use this with arithmetic operators to give different offsets depending on whether...
View ArticleAnswer by MvG for Tips for golfing in C
Make use of return values to zero stuff. If you call some function, and that function returns zero under normal conditions, then you can place it in a location where zero is expected. Likewise if you...
View ArticleAnswer by klingt.net for Tips for golfing in C
Using asprintf() saves you the explicit allocating and also measuring the length of a string aka char*! This isn't maybe too useful for code golfing, but eases the everyday work with a char arrays....
View ArticleAnswer by moala for Tips for golfing in C
Print a character then carriage return, instead of:printf("%c\n",c);orputchar(c);putchar('\n'); // or its ascii value, whatever!simply, declare c as an int and:puts(&c);
View ArticleAnswer by Fors for Tips for golfing in C
Instead of >= and <= you can simply use integer division (/) when the compared values are above zero, which saves one character. For example:putchar(c/32&&126/c?c:46); //Prints the...
View ArticleAnswer by luser droog for Tips for golfing in C
Use cursors instead of pointers. Snag the brk() at the beginning and use it as a base-pointer.char*m=brk();Then make a #define for memory access.#define M [m]M becomes a postfix * applied to integers....
View ArticleAnswer by walpen for Tips for golfing in C
http://graphics.stanford.edu/~seander/bithacks.htmlBits are nice.~-x = x - 1-~x = x + 1But with different precedences, and don't change x like ++ and --. Also you can use this in really specific cases:...
View ArticleAnswer by Andreas Krey for Tips for golfing in C
You may look into the IOCCC archives (international obfuscated C code contest).One notable trick is to #define macros whose expansion has unbalanced braces/parentheses, like#define P printf(
View ArticleAnswer by breadbox for Tips for golfing in C
The ternary operator ?: is unusual in that it has two separate pieces. Because of this, it provides a bit of a loophole to standard operator precedence rules. This can be useful for avoiding...
View ArticleAnswer by Neeraj Gupta for Tips for golfing in C
use scanf("%*d "); to read the dummy input. (in case that input is meaningless in further program)it is shorter than scanf("%d",&t); where you also need to declare the variable t.storing characters...
View ArticleAnswer by ugoren for Tips for golfing in C
Define parameters instead of variables.f(x){int y=x+1;...}f(x,y){y=x+1;...}You don't need to actually pass the second parameter.Also, you can use operator precedence to save parenthesis.For example,...
View ArticleAnswer by Quixotic for Tips for golfing in C
If your program is reading or writing on one in each step basis always try to use read and write function instead of getchar() and putchar().Example (Reverse stdin and place on...
View ArticleAnswer by Lowjacker for Tips for golfing in C
Since usually EOF == -1, use the bitwise NOT operator to check for EOF: while(~(c=getchar())) or while(c=getchar()+1) and modify value of c at every place
View ArticleAnswer by Lowjacker for Tips for golfing in C
Use bitwise XOR to check for inequality between integers:if(a^b) instead of if(a!=b) saves 1 character.
View ArticleAnswer by Joey Adams for Tips for golfing in C
Abuse main's argument list to declare one or more integer variables:main(a){for(;++a<28;)putchar(95+a);}(answer to The alphabet in programming languages)This solution also abuses the fact that a...
View ArticleAnswer by Casey for Tips for golfing in C
The comma operator can be used to execute multiple expressions in a single block while avoiding braces:main(){ int i = 0; int j = 1; if(1) i=j,j+=1,printf("%d %d\n",i,j); // multiple statements are all...
View Article