C identifier and main [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 4 years ago.
Improve this question
1.Why "main" in the C main() function identifier?
2.If main is an identifier then how does a program execution starts from main only.

main is a function, almost exactly like any other. It's named by an identifier, it gets called, it receives some arguments, it does something, it returns a value. That's pretty much the definition of a function. The designers of C were going for simplicity, so it made perfect sense to have the program's entry point be an ordinary function.
If you're an assembly language programmer, you may know that a program's entry point is simply an address, not a full-fledged function. But that's assembly language talk: C is a higher-level language.
As others have explained, there's some low-level startup code somewhere (typically written in assembler), that has a program's actual entry point, as jumped to when your program starts up. That startup code is the code that actually calls your main function.
I said that main is an ordinary function, and it mostly is, but it has three special properties not shared by other functions:
It's just about the only function that you ever write where you don't get to pick your own name, return value, and argument types -- those are all chosen for you (or, if you want to think of it that way, forced on you) by the language. For example, the return type just has to be int, because the language says so. You don't get to make it some other type just because you want to (although of course lots and lots of programmers make it void, and often get away with it).
main actually has two valid sets of arguments it can accept: either zero or two, namely int and char ** (traditionally named argc and argv).
As a very special exception, even through main returns int, you're allowed to not have a return statement, and the compiler will basically insert one for you, making main() return 0 by default.

C standard defines the main function as the program entry point. It is called from the startup code or the program loader.
BTW you can change it (I do not know why but if you want you can) - example for the baremetal ARM
startup:
/* Call the clock system intitialization function.*/
bl SystemInit
/* Call static constructors */
bl __libc_init_array
/* Call the application's entry point.*/
bl initCCMRAM
/* Here was the main call */
bl my_entry_point_function
LoopForever:
b LoopForever
and in the C code
int my_entry_point_function(void)
{
...
}

Related

A more compact way of passing a variable number of arguments in C [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 4 years ago.
Improve this question
I would like to call a function (called here just function) that accepts a variable list of arguments without knowing in advance how many arguments will be needed.
I have come up with this:
int param_num
char **param // initialized and populated somewhere else
...
if (param_num == 0) result = function();
else if (param_num == 1) result = function(param[0]);
else if (param_num == 2) result = function(param[0],param[1]);
...
The above code is just a proof of concept and is not intended to be compilable. The actual function has at least one fixed argument. I cannot change the function because it belongs to an external library. The actual code is more complex and is working as expected but...
My question is: is there a more compact way of writing the same code without touching "function"?
PS working in Linux with gcc 7
PPS it is just a curiosity. There is no real problem that I am trying to solve. The above code is working as expected. Just wondering if there were a way to make it prettier.
Thanks
Read about <stdarg.h> and variadic functions. On Linux, see also stdarg(3). It is how you can define in C such functions of variable arity (like printf). The first few arguments should be fixed, and of known type, so you cannot have exactly what you want (with a call like function() without arguments). So a variadic function is required to have at least one first fixed-type argument (which usually determines, like in printf, the type and number of following variadic arguments). So the C language specification forbids to call the same function without any arguments and with two of them.
Be also aware of the ABI of your system. It defines calling conventions (which usually vary with the signature of the called function) For 64 bits x86 on Linux, see this (floating point and pointer arguments get passed in different registers).
GCC (specifically) has also builtins for constructing function calls, but the C11 standard n1570 don't have anything like that.
It is unclear what you exactly want to achieve, but in some cases you might generate code (e.g. pedantically use metaprogramming) at runtime. There are several ways to achieve that on Linux:
You could generate (in a file) some C code in /tmp/emittedcode.c, fork its compilation as a plugin (by running gcc -fPIC -shared -Wall -O -g /tmp/emittedcode.c -o /tmp/generatedplugin.so) then load that plugin using dlopen(3) (as void*dlh = dlopen("/tmp/generatedplugin.so", RTLD_NOW); but you should handle failure) and dlsym(3) of symbols there. My manydl.c example shows that you can practically do that for many hundred thousands of generated plugins.
You could use some JIT-compiling library like libgccjit or asmjit or libjit, or GNU lightning, etc...
In both cases, you'll then (conceptually) add a new code segment to your process and get the address of a freshly created function there into some function pointer.
Be also aware of closures and anonymous functions, and notice that C don't have them (but have to use callbacks). Read SICP.
As commented by Antti Haapala, look also into libffi (it enables you to call an arbitrary function with arbitrary arguments by "interpreting" that call, which you would describe by ad-hoc data structures).
If you exactly know what type of args you'll need to pass, you can create a structure with possible args defined in it
you can then pass the structure and do your stuff
struct args {
define args....
}
.
.
struct args* a;
result = function(a);
keep in mind that you'll have to add some checks in the function to see what args are passed.

Why all c programs should start with a main() that returns a `int`? [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 8 years ago.
I like to know what is the main reason that all C programs should start with the standard function of "int main()"? and why it should be "int" ? Thank you
Not all C "programs" start with "int main." I put program in quotes to highlight the fact that you can have c files that do not have main in them, such as class definitions. Essentially main is the section of code that tells the computer what to do, and in what order -- all other files can be thought of as helper files that partition the code to make it more readable and maintainable. Main has an int type because the program will return an integer value describing if the program executed without a problem, which corresponds to a return of 0, or what went wrong, which can be any non-zero number that usually has documentation that will tell you what the corresponding number meant in terms of failure.
When you execute a program, the first thing to run is initialization code in the C library. When it finishes, it calls the standard entry point, main, with the parsed command line arguments. The int return value from main is the return value of the program.
Actually, not all C programs have to have a main. The ISO C standard supports two types of environments. The first is hosted, and that is the one that requires a main of a specific format (from several allowable ones).
Freestanding environments (the second type) have no such requirement and, in fact, there's a lot of leeway for freestanding behaviour at many other points in the standard as well.
That's why you can still consider the Linux kernel to be a C program despite the fact there's not a main in sight.
As to why hosted environments have this requirement, that's the way C was originally written back in the 70s and, when ANSI came to do the standard, their primary brief was to codify existing practice, not create a new language.
The long chain of standards committees following that, ISO C89/90, C99 and C11 have either not been convinced that there was any need to change it, or their contributors have not put forward the idea.
A program has to start running somewhere; this is called its "entry point". The standard entry point for a C program is called "main" because that's what someone chose long ago and there's no real reason to change it to some other name. However, some compilers (actually, linkers) have options to build a program using some other function name as the entry point.
The main function returns an int to provide feedback about whether it ran successfully. By convention, returning 0 indicates that the program succeeded, and returning some other value indicates that it failed. You can check this result with, for example, the $? environment variable in the bash shell.
This is because of the fact how your code is to be called and the Standard says how it should happen. As to the return value, there is a rule that says that main function has to return the integer as a result of a call to the caller - it should indicate how the program exited.
Why all c programs should start with int main ()?
This is because:
When the operating system runs a program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it's the main() function that the operating system is looking for.
and why it should be "int" ?
5.1.2.2.1 Program startup:
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int [...]
it is a matter of standard, check out ansi C standard it states that main function should return int value. Also from operating system point of view returning value from main function is only indicator if program ended properly.

What happens with the return value of main()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What should main() return in C/C++?
#include<stdio.h>
int main()
{
return 0;
}
In the code snippet given above, where does the return 0 returned by main go? Or in other words which function called the main function in the beginning.
main is called by some startup function in the C runtime library. The C language standard says that returning from main is equivalent to calling the exit function, so most C runtimes look something like this:
void _start(void) /* Exact function signature may vary */
{
/* Platform-specifi startup (e.g. fetch argc and argv from the OS) */
...
int status = main(argc, argv);
exit(status);
/* Never reached */
}
The exit status gets passed back to the operating system, and then what happens from there is OS-dependent.
When you compile and link your program, the executable file format (e.g. PE or ELF) contains a start address, which is the virtual address at which execution begins. That function is typically part of the C runtime library (like the example _start above). That function has to end by calling a system call such as exit, since if it just returned, it would have nowhere to go to: it would just pop an address off the stack and jump to that location, which would be garbage.
Depending on how the OS loader initializes processes, the program arguments argc, argv, and other data (such as the environment) might either come in as function parameters (either through registers or the stack), or they might require system calls (e.g. GetCommandLine on Windows) to retrieve them. But dealing with all of that is the job of the C runtime, and unless you're explicitly going out of your way to avoid using the C runtime, you needn't worry about those details.
Your compiler targets a certain platform, which includes operating-system specific mechanisms for starting processes. Part of that platform specific code contains the return value of main. When you link your program into an executable, there is an OS-specific piece of binary code that your linker adds which takes care of calling main and reporting the return value back to the operating system.
The return value goes to the hosted environment. Typically, operating system calls main and gets exits status of the program.
where does the return 0 returned by main go? Or in other words which function called the main function in the beginning.
It is called by the C startup library, a stub function that is called (almost) directly by the kernel. For example, on Linux and OS X, it's a function named _start. It has the same signature as main() and the operating system itself uses its return value.

Printing the contents of the program stack (C Language) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can one grab a stack trace in C?
Hi,
I would like to know how to print the contents of the current program stack (using C language).
say for eg.
call_some_function()
{
...
...
print_stack_till_now();
return something;
}
call_some_other_function()
{
...
...
print_stack_till_now();
return something;
}
main()
{
print_stack_till_now();
call_some_function();
print_stack_till_now();
call_some_other_function();
print_stack_till_now();
return 0;
}
In the prev example (may be not an example exactly :)) when I call the print_stack_till_now() function I should be able to print the current stack built till that point (including the newer function call entries, return location, their arguments etc.)
Is such a function possible in C language (even inlined assembly). Please point me to the theory (or existing code would be even better) needed to write such a function.
In gdb we can use backtrace to look at the current stack, I'm looking for something similar. Why do I need this ?... just wanted to learn.
Thank you.
There's no portable way to do this for the simple reason that C by itself doesn't define the concept of a stack data structure. It's completely open to the implementation how it does automatic storage and returns from function calls.
That being said most implementations provide some kind of stack unwinding mechanism. For example GCC/glibc provides the runtime specific function backtrace
http://www.gnu.org/s/libc/manual/html_node/Backtraces.html
There are similar stack unwinders for other plattforms. And of course you can implement your own backtracing mechanisms through a global, thread local storage array (it can be static and must provide only enough entries for how many function calls are supported by the stack size), where at each function call the calling module (using C preprocessor __FILE__), the line (C preprocessor __LINE__) and the called function (some additional preprocessor magic) are placed.

Is main() a pre-defined function in C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
main() in C, C++, Java, C#
I'm new to programming in general, and C in particular. Every example I've looked at has a "main" function - is this pre-defined in some way, such that the name takes on a special meaning to the compiler or runtime... or is it merely a common idiom among C programmers (like using "foo" and "bar" for arbitrary variable names).
No, you need to define main in your program. Since it's called from the run-time, however, the interface your main must provide is pre-defined (must return an int, must take either zero arguments or two, the first an int, and the second a char ** or, equivalently, char *[]). The C and C++ standards do specify that a function with external linkage named main acts as the entry point for a program1.
At least as the term is normally used, a predefined function would be one such as sin or printf that's in the standard library so you can use it without having to write it yourself.
1If you want to get technical, that's only true for a "hosted" implementation -- i.e., the kind most of us use most of the time that produces programs that run on an operating system. A "free-standing" implementation (one produces program that run directly on the "bare metal" with no operating system under it) is free to define the entry point(s) as it sees fit. A free-standing implementation can also leave out most of the normal run-time library, providing only a handful of headers (e.g., <stddef.h>) and virtually no standard library functions.
Yes, main is a predefined function in the general sense of the the word "defined". In other words, the C language standard specifies that the function called at program startup shall be named main. It is not merely a convention used by programmers as we have with foo or bar.
The fine print: from the perspective of the technical meaning of the word "defined" in the context of C programming, no the main function is not "predefined" -- the compiler or C library do not supply a predefined function named main. You need to define your own implementation of the main function (and, obviously, you should name it main).
There is typically a piece of code that normal C programs are linked to which does:
extern int main(int argc, char * argv[], char * envp[]);
FILE * stdin;
FILE * stdout;
FILE * stderr;
/* ** setup argv ** */
...
/* ** setup envp ** */
...
/* ** setup stdio ** */
stdin = fdopen(0, "r");
stdout = fdopen(1, "w");
stderr = fdopen(2, "w");
int rc;
rc = main(argc, argv, envp); // envp may not be present here with some systems
exit(rc);
Note that this code is C, not C++, so it expects main to be a C function.
Also note that my code does no error checking and leaves out a lot of other system dependent stuff that probably happens. It also ignores some things that happen with C++, objective C, and various other languages that may be linked to it (notably constructor and destructor calling, and possibly having main be within a C++ try/catch block).
Anyway, this code knows that main is a function and takes arguments. If your main looks like:
int main(void) {
Then it still gets passed arguments, but they are ignored.
This code specially linked so that it is called when the program starts up.
You are completely free to write your own version of this code on many architectures, but it relies on intimate knowledge of how the operating system starts a new program as well as the C (and C++ and possibly Objective C) run time. It is likely to require some assembly programming and or use of compiler extensions to properly build.
The C compiler driver (the command you usually call when you call the compiler) passes the object file containing all of this (often called crt0.0, for C Run Time ...) along with the rest of your program to the linker, unless told not to.
When building an operating system kernel or an embedded program you often do not want to use the standard crt*.o file. You also may not want to use it if you are building a normal application in another programming language, or have some other non-standard requirements.
No, or you couldn't define one.
It's not predefined, but its meaning as an entry point, if it is present, is defined.

Categories

Resources