Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to uglify or minify C code [closed]

Tags:

c

gcc

minify

Using gcc I can remove comments and unwanted blank lines, but I want to reduce a size of file also, is there any options in gcc or any other tool to do so

At present I do like this

gcc -fpreprocessed -dD -E  -P source_code.c > source_code_comments_removed.c 

Here is scenario assume that this is my source_code.c

#include <stdio.h>
main()
{ 
      // declar variable i
      int i=0;

      /* multiline comment
      for loop
      demo stuff
      */
      for(i=1; i<=5; i++)
      {
            // just print something
            printf("Hello %d \n",i);
      }

}

I want to minify like this, removed comments and blank lines

#include <stdio.h>
main(){int i=0;for(i=1; i<=5; i++){printf("Hello %d \n",i);}}

Note : I am on Linux please don't suggest any windows based solution

like image 252
user3637224 Avatar asked Jun 12 '15 05:06

user3637224


People also ask

Does Uglify minify?

Uglify JS is a JavaScript library for minifying JavaScript files.

Why would a website minify their source code?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

What is Minification and Uglification?

Minifying is just removing unnecessary white-space and redundant like comments and semicolons. And it can be reversed back when needed. Uglifying is transforming the code into an "unreadable" form by changing variable names, function names, etc, to hide the original content.


1 Answers

sed -rb 's/ {6}//g' main.c |
sed -rb 's/\/\/.*$//g' |
tr -d '\n' |
sed -rb 's/\/\*.*\*\///g' |
sed -rb 's/(#include.*>)/\1\n/g'

will give you:

#include <stdio.h>
main(){int i=0;for(i=1; i<=5; i++){printf("Hello %d \n",i);}}

However, as stated in the in the comments, this doesn't make much sense and will not reduce the size of your compiled object file!

like image 190
sergej Avatar answered Oct 15 '22 13:10

sergej