Answer by Rémi for Tips for golfing in C
If you have some variables declared in a row :int a,b,c,d;You can access b by (&a)[1], or c by (&a)[2] ...It can be interesting there :i?a=k:i-1?b=k:i-2?c=k:(d=k);VS(&a)[i]=k;You could have...
View ArticleAnswer by None1 for Tips for golfing in C
Access the first element of array using unary *Arrays are similar to const pointers, so instead of a[0], you can use *a, saves 2 bytes.
View ArticleAnswer by c-- for Tips for golfing in C
Abuse implicit conversionsIf you need to check if a variable is in a given range (say islower(3))...Instead of96<c&c<123... try something likec-97<26u // this also has the advantage of...
View ArticleTips for golfing in C
What general tips do you have for golfing in C? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C (e.g. "remove comments" is not an...
View ArticleAnswer by Casey for Tips for golfing in C
Certain compilers, such as GCC, allow you to omit basic #includes, param, and return types for main.The following is a valid C89 and C99 program that compiles (with warnings) with GCC:main(i) {...
View ArticleAnswer by dmckee --- ex-moderator kitten for Tips for golfing in C
Any part of your code that repeats several times is a candidate for replacement with the pre-processor.#define R returnis a very common use case if you code involves more than a couple of functions....
View ArticleAnswer by dmckee --- ex-moderator kitten for Tips for golfing in C
The ternary conditional operator ?: can often be used as a stand in for simple if--else statements at considerable savings.Unlike the c++ equivalent the operator does not formally yield an lvalue, but...
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 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 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 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 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 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 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 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 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 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 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 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 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 Article