What are some hacks to reduce the length of code written in C?
-
-
Answer:
I am sure that there are many tricks, but being an exploit developer and working with shellcode my method is probably not very standard. This is what I do. I look at the resulting assembly code and optimize this. Let me give you some examples. Instead of moving a value from one register a another, push it to the stack and pop it into the proper register Instead of writing a value taking up e.g. two bits to a 64 bit register. XoR the register with it self and just write the last two bits There are lots of small optimization tips for assembly code that will allow you to shorten the assembled code a lot and the work is fun and challenging
Kim Guldberg at Quora Visit the source
Other answers
I will assume that you are doing this for some shortest code contest or have some other reason to reduce code size. Otherwise, do not try to reduce code length just because it feels cool. Verbosity is any day preferable to obfuscation. A few things that you could try will be: Make macros for repeating code blocks. If you are going to use scanf to read integers 10 times in your code, makes sense to define a macro S(x) that reads integers. But the thing is, the macro could be a substitute for any weird contraption, not just a function. For an example see http://www.codechef.com/viewsolution/456464. Use the conditional (?:) operator and nest it lavishly. In most of the cases, you should not need to have if/else in your code. If code length is the only constraint and you don't mind sacrificing some speed for the same, try to tinker with your program flow. Do more operations than required if it doesn't change the result and shortens the code. For example, adding zeros to a few corner case variables might be shorter than checking those corner case conditions. Use the fact that global variables are initialised to 0, and that main and its arguments are assumed to be ints (This could be a gcc specific thing, but if you are just looking for shortest code contests, that should do since most contest compilers are gcc) These are the tricks that I have seen to be used most commonly. WIll add anything further that I can think of.
Raziman T.V.
1) Move the repetitive code to non-inline functions - reduces both hand-written and generated code size, if the function-calling overhead is not a concern. 2) Use short and intuitive variable name over unnecessary long ones - just i over indexForTheLoopToIterate, etc. 3) Avoid explicit zero-initialization for local variables which are static, global variables which are either static or non-static. 4) Place the statements inside a loop which are exactly same except that they differ in using some sort of index, unless you have unrolled it already as a speed-up mechanism. 5) Prefer writing functions which perform simple evaluations, in a simple manner - just have the evaluation as part of the return statement; no need of extra temporary variables in the code Simple ex: int sub(int a, int b){ return a - b;} instead of int sub(int a, int b){ int ret = a - b; return ret;} 6) Avoid explicit checks for NULL. If a variable p is a pointer, then just if ( p ) is enough and exactly same as if ( p != NULL ). 7) Use ++ instead of + or += when possible. Same applies to --. 8) If you have bunch of generic functions which are supposedly having long/very long names, then hold their addresses in an array of function pointers and just index appropriately to call them. 9) Avoid explicit zero-initializing all the elements of a local non-static compile-time array(applies to char arrays as well when you require an empty string). Just initialize the first one and rest automatically gets initialized to zero. 10) Avoid temporary variables when you can - swapping integer variables using ^ or ^= keeping in mind the concept of sequence points. 11) Use += when possible: a += b instead of a = a + b. 12) Though no one uses ever, just for the sake of completeness I am mentioning this: no explicit unary-plus. +a is any day same as that of a and so on! 13) Use bit-fields to access individual bits of a byte instead of masking/shifting, unless portability of the code is a concern. 14) No extra comma(,) needed after the last element of an array or the last enum-specifier. That's all I can think of as of now! :) Choose the above optimizations with respect to the hand-written C code's size based on how intuitive they are to you and your intended audience. Any day, prefer readability over not-so-intuitive succinctness.
Raghavan Santhanam
#define GETC ({char x;scanf("%c ", &x);x;}) #define GETL ({long x;scanf("%li\n", &x);x;}) long t=GETL; char c= GETC; instead of writing scanf every time u can use this one. If u create x as global variable, it will even more better in terms o saving memory.
Vinoth Gopu
Related Q & A:
- How to Convert Code from VB to C++?Best solution by Stack Overflow
- How can I convert Matlab code to c#?Best solution by Stack Overflow
- What would happen if someone hacks your account?Best solution by Quora
- What is the Direct TV RC32 remote control program code for TCL LCD TV?Best solution by Yahoo! Answers
- What are some fashion design programs for high school students in D.C?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.