C program without a main function? [closed] - c

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 9 years ago.
Improve this question
Is there any way to write a C program without a main function? If so, how can that be achieved?

C defines the entry point in a hosted environment to be main. In a "freestanding" environment, however, the entry point can have some other name. That's about the only latitude the language (at least officially) allows in that particular respect.

Yes, you can.
_start function is the entry point of a C program which makes a call to main().
Going further into it, main() is the starting point of a C program from the programmer's perspective. Before calling main(), a process executes a bulk of code to "clean up the room for execution".
_start is the function which gets called first, which then allocates necessary resources and then calls main() which has to be defined by the programmer.
You can override _start and tell the compiler to not to look for main() by using "-nostartfiles" option.
#include <stdio.h> //for using printf()
_start()
{
printf("Hello world!!\n");
_exit(0);
}
To Compile : gcc -nostartfiles code.c -o a.out
Also look at http://linuxgazette.net/issue84/hawk.html for more basic information.

The following linker abuse
char main[] = { /* Machine code for your target implementation */ };
will work on some platforms.

No. C is totally based off of the assumption that you start the program in main(). Anyway, why would you want this? This would make inconsistencies for other programmers reading your code.

Maybe this could work:
http://www.gohacking.com/2008/03/c-program-without-main-function.html
An alternative is to write a C program and look at the Assembly output:
http://users.aber.ac.uk/auj/voidmain.shtml
More information about what happens before main() is called can be found here (How Initialization Functions Are Handled):
http://gcc.gnu.org/onlinedocs/gccint/Initialization.html

Related

python "print" like function in c [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 2 years ago.
Improve this question
I am new to C and I use to code in python, and I usually print too much variables and text to test my code and printf statement is irritating sometimes. I want to write a function in c which works exactly like print function in python.
I am facing two problem while writing the function.
I want to take n number of arguments as input to the function.
I want to take any data type as input to the function,
for Eg: print(12); print("hello"); print(123.12) should not raise error.
What have I done so Far
I found solution for taking n number of arguments using <stdarg.h>, but I have to specify the data type of the first argument which is not what I want.
"generic selection" Macros can be used to call different function on different data type of argument passed but not sure how it can be done, here is the link which I used for reference.
I want to write a function in c which works exactly like print function in python.
You simply cannot do that (because of type erasure : at runtime, type information is lost in C). Read Modern C then see this C reference.
In practice, you'll better write one function per datatype in C to print it. So it would be void print_int(int); to print an integer, void print_double(double); to print a double, etc. Once you have a collection of such functions you might use _Generic inside a macro (but that is rarely useful; it can handle a finite set of types).
Study for inspiration the source code of existing open source C software on github or elsewhere. Look for inspiration inside the source code of GNU bash, sqlite, GTK or GNU bison (and perhaps inside the source code of Python; half of the Python interpreter is coded in C)
Read also the documentation of your C compiler, e.g. GCC. So compile with all warnings and debug info: gcc -Wall -Wextra -g then use the GDB debugger.
Perhaps you want to implement some tagged union abstract data type in C. Then take inspiration from GNU guile or the runtime of Ocaml and dive inside their source code.
Once you are more familiar with C, read some C draft standard, e.g. n2176
Consider using Frama-C or the Clang static analyzer.
Consider also sometimes generating some C code, like SWIG does.
Budget weeks of work.

How to clear terminal in C without using #include? [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 2 years ago.
Improve this question
I want to clear terminal in C without using #include.
Here is my code:
int printf(const char *format, ...);
int main()
{
printf("%c2J", 27);
}
You likely won't be able to clear a terminal without using functions in the standard library, but probably the closest thing would be to use ANSI escape sequences, which might work depending on your terminal.
You'll have to output \x1b[2J (probably followed by \x1b[H). These are the ANSI terminal escape sequences for clearing the screen and repositioning the cursor in the upper left "home" position, respectively.
#include <stdio.h>
int main(void) {
printf("\x1b[2J\x1b[H");
return 0;
}
Please note that even if you specify the function prototype for printf(), as you have done above, you are still using the standard library and not making your own. I can only think of one reason you'd ever want to do that: to make a quine fit on one line.
If you want to write your own function you should not be using the standatd library. Invlude does not matter as .h files are not libraries. You should compile the code with -nostdlib option and write your own function.
In your code you simply use the standard library function - not your own one.

How can I write 'Hello world' in C without using any function? [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 6 years ago.
Improve this question
I found the way to do it on quora but I can not understand it.
You can achieve this using opcodes.....
const int main[] = { -443987883, 440, 113408, -1922629632, 4149, 899584, 84869120, 15544, 266023168, 1818576901, 1461743468, 1684828783, -1017312735};
Now follow below steps....
Save that in hello.c
then gcc -o hello hello.c
then ./hello
Hola..... Hello World!
Op-codes will be specific for specific hardware and controller based on architecture of it. If I understood correctly then op-codes will be different for different hardware and platforms. So this is not a generic solution. However I tried to compiler it on IDEone & it gives run time error. Is there any generic solution?
Similar question on SE
In C, the main function is the entrypoint into your program. In the program binary, the machine code into which the function is compile will get placed in the .text section of the binary as well as the location of the main function in the symbol table.
In the above code we create a constant array with the same main symbol name. Since the array is constant, this will also get placed in the .text section of the binary. And since any code in the .text section is executable, there is nothing to stop the opcodes in the main array getting executed.

How to determine the value of passing arguments of a function via the backtrace of a process? [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 8 years ago.
Improve this question
I have a backtrace of a process. and I want determine the value of a argument of one function called in the call trace, I have the .o file and I disassemble it, So I have the assembly procedure of that particular function, How can I calculate the value of the function's passing argument through the backtrace and the assembly code? It's on the ARM platform, I'm not quite familar with the ARM call trace, and assembly code.
If the code is written in C, this information is not available solely from the executable image, for the simple reason that C does not mangle function name symbols in order to encode the function parameter types.
Brief experimentation with gdb shows that if C code is compiled with the debugging flag, -g, gcc does put sufficient information into the executable's debug records for gdb to be able to figure out the function parameter types, and display function arguments in the backtrace.
But, if the executable is not compiled with -g, all that's in the executable are the function names, and their addresses, and that's all that gdb can show, in a backtrace from a coredump.
So, if you're working with .o files containing C code, without any debug stuff in it, there's nothing that can show you what the function parameters are,

C -- Print To Screen Without #include <stdio.h>? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Is there a way to have a C source file print to the screen without including <stdio.h>?
Here's my situation: I was asked to programatically handle 1000 C source files that will each implements several numerical functions in C (these functions are supposed to work on data that is in memory, eithout any I/O). The origin of these source files in unclear, and hence, I'd like to make sure there will be no harm to my machine when I compile & run these source files.
Is there a way to find out if a C source file is potentially harmful? I thought of asking the developers to avoid any #include statements whatsoever, but I do need just printf -- as I'd like them to include an output of their calculations within main().
Any ideas?
Sure, add the prototype for printf at the top of your source file, as long as you're linking to the CRT libraries you can use the function without including stdio.h
printf prototype
int printf ( const char * format, ... );
There are, though they are probably a bit larger than the scope of the format of SO. In essence you leverage assembler calls in C. The blog KSplice touches on the subject ( with code and examples ) here.
Is there a way to find out if a C source file is potentially harmful?
No, there is none. A malicious source file could possibly do anything it wanted by defining its own prototypes, or by using inline assembly -- #include is just a compile-time convenience.
I would like to clarify why we need printf and studio.h to maybe make the concept more clear. C is a portable language. You can compile c for Linux, Mac OSX, Windows. In each, causing output normally boils down to a system call, or in embedded systems, dealing directly with a frame buffer or Uart device.
So of course it is possible, do you want to do it? Depends why. If you are coding against a specific platform and dont have printf(), then you may have to look into invoking a system call directly for that platform/writing some platform specific assembly code. It all depends on your use case.
Sure, put the necessary function prototypes in your program.
If you mean by not using printf, then you have several options - you can use fwrite, or you could dispense with streams and use write, or you could invoke operating system I/O services directly, or perhaps you could talk to the display hardware directly, or many other things.
If you want a better answer, perhaps explain why you want to not include stdio.h
This is silly but still:
#include <string.h>
int main() {
puts ("hi");
return 0;
}
and Output:
$ gcc -o try try.c
$ ./try
hi

Resources