How to uglify or minify C code [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
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

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!

Related

What is wrong with using %var after format specifier in a scanf call? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
main ()
{
int a,b,toplam;
float ort;
printif("iki sayi girin :");
scanf("%d,%d",%a,%b);
toplam=a+b;
ort=toplam/2;
printif("Ortalama= %f olarak hesaplandı.",ort);
}
error: expected expression before '%' token scanf("%d,%d",%a,%b);
What was my mistake?
Replace scanf("%d,%d",%a,%b); with scanf("%d,%d",&a,&b);
Check this answer for more information.
You need to replace the '%' before a and b with '&'.
Also you need to use printf not printif.
int a,b,toplam;
float ort;
printf("iki sayi girin :");
scanf("%d,%d",&a,&b);
toplam=a+b;
ort=toplam/2;
printf("Ortalama= %f olarak hesaplandı.",ort);
return 0;
Read Modern C and see this C reference
Your scanf statement should be (using the & prefix "address-of" operator):
scanf("%d,%d",&a,&b);
and your code is incomplete, since scanf can fail. You should read its documentation.
Consider coding a test against such failure using:
if (scanf("%d,%d", &a, &b) < 2) {
perror("scanf failure");
exit(EXIT_FAILURE);
}
Read also the documentation of your C compiler, e.g. GCC, and of your debugger, e.g. GDB. You could compile your code with gcc -Wall -Wextra -g to get all warnings and debug information, and later use gdb to debug your executable and understand its runtime behavior.
Your code is missing a #include <stdio.h> before your main.
The printif is wrong. You want to use printf (and read its documentation).
Consider studying for inspiration the source code of some existing free software, such as GNU make or GNU bash.

How do I print .so file location at runtime? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
In foo.c
void location(char *path)
{
//to do
}
In main.c
int main()
{
char foopath[256];
location(foopath);
printf("%s\n",foopath);
}
Maybe it will show /lib/foo.so
I think I can use shell script such as ldd to get the path, but it seems not pretty.
I want to read a file at the same location as the foo.so. So I need the correct path.
You can use the "dl" library. Example of program which displays the name of the dynamic library file of the "fopen" symbol:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
int main(void)
{
int rc;
Dl_info info;
rc = dladdr(fopen, &info);
if (rc) {
printf("%s\n", info.dli_fname);
return 0;
}
return 1;
}
$ gcc example.c -l dl
$ ./a.out
/lib/x86_64-linux-gnu/libc.so.6

How to write a C/C++ wrapper program in Linux that is fully transparent [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Note: This is not a question to ask for a program, It asks about some tech details, see the question bellow first.
I need to write a wrapper program in C/C++ for an existing program. I know we need to use exec/fork/system and pass through the parameters then return the result of the program.
The question is, how to ensure that both the invoker program(that invoke the wrapper) and the wrapped program work exactly like before (ignore timing differences). There maybe subtle things like environment parameters to deal with. fork/system/exec, which to use? Are they enough? Are there other factors to consider?
Let's say you have the following original program:
foo.sh
#!/bin/bash
echo "Called with: ${#}"
exit 23
Make it executable:
$ chmod +x foo.sh
Now the wrapper in C:
wrapper.c
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
printf("Executing wrapper code\n");
/* do something ... */
printf("Executing original program\n");
if(execv("./foo.sh", argv) == -1) {
printf("Failed to execute original program: %s\n", strerror(errno));
return -1;
}
}
Run it:
$ gcc wrapper.c
$ ./a.out --foo -b "ar"
Executing wrapper code
Executing original program
Called with: --foo -b ar
$ echo $?
23

addr2line print "??:0" even i use `gcc -g` [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my code:
/* backtrace_foo1.c */
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#define BACKTRACE() \
do {\
void *array[20];\
size_t size;\
char **strings;\
size_t i;\
size = backtrace(array, 20);\
strings = backtrace_symbols(array, size);\
for (i = 0; i < size; i++) {\
printf ("%s\n", strings[i]);\
}\
free (strings);\
} while(0)
void func1()
{
BACKTRACE();
}
void func()
{
func1();
}
int main(int argc, char **argv)
{
func();
return 0;
}
I compiled it by gcc -g -rdynamic and got
./a.out(func1+0x1f) [0x400905]
./a.out(func+0xe) [0x40097a]
./a.out(main+0x19) [0x400996]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x318ae1ecdd]
./a.out() [0x4007f9]
Then i use addr2line -e ./a.out -f 0x4007f9, i got
_start
??:0
This is my platform
gcc version 5.3.0 (GCC)
Linux 3.10.0_1-0-0-8
I shouldn't really answer this, since you don't really have a question or a stated problem. But sometimes I'm feeling nice...
While the code you write will start execution with the main function, the actual starting point is somewhere before that. There is startup code that will initialize the stdio system (stdin, stdout etc.) and initialize other things. This startup code then calls your main function like any other function.
The "problem" is that the startup code is not really part of your code, it's often a precompiled object file that the frontend program links your program with. And that object file probably doesn't have any kind of debug information, so you can't get any location information about it.

What is the proper ansi c example for windows? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Some books claim that they used ansi c and use turbo c compiler to run these example. i tried to run these on linux but I found that these example are only for windows.
#include<stdio.h>
#include<conio.h>
/* #include<dos.h> */
int main()
{
int a;
clrscr();
scanf("%d", &a);
printf("%d",a);
getch();
return 0;
}
Can I call the above example ansi c? why or why not?
As #milevyo said, those functions are implemented by Borland's compilers. On Windows you can replace clrscr() with system("cls") and getch(); with _getch(); or, even better, getchar().
conio.h file directory is only supported by Borland C.You can use getchar() instead of getch().
If you have to use getch() anyhow then You can use curses.h file instead of conio.h. It gives mostly all the functionality of conio.h with getch().
If you don't get curses.h directory installed then you can download from here
#include <stdio.h>
#include <ncurses.h>
int main()
{
initscr(); /* start the curses mode */
int a;
scanf("%d", &a);
printf("%d",a);
getch();
endwin();
return 0;
}

Resources