Quantcast
Channel: Tips for golfing in C - Code Golf Stack Exchange
Browsing all 67 articles
Browse latest View live
↧

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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
Browsing all 67 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>