Execute any function before main [duplicate] - c

This question already has answers here:
Executing code before main()
(5 answers)
Closed 9 years ago.
I want to execute user define function before main().
Is it possible to execute a function before main() in c?
sum(int a, int b) { return (a+b); }
g_sum = sum(1, 5);
main(){
sum(5, 6);
printf("%d", g_sum);
}

Is it possible to execute a function before main()
Yes it is possible if you are using gcc and g++ compilers then it can be done by using __attribute__((constructor))
Example:
#include <stdio.h>
void beforeMain (void) __attribute__((constructor));
void beforeMain (void)
{
printf ("\nThis is before main\n");
}
int main ()
{
printf ("\nThis is my main \n");
return 0;
}

Related

Difference between int main(void) and int main() [duplicate]

This question already has answers here:
Difference between int main() and int main(void)?
(10 answers)
Closed 9 months ago.
#include <stdio.h>
int main()
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
I was reading article on geeksforgeek the difference between int main(void) and int main() but I am confused. How main function taking arguement?
int main(void) { }
Here, you are telling your compiler explicitly that your main() function is not going to take any arguments. This prototype of main() function is standard.
int my_func(void)
{
return 100;
}
// the above function can be called as:
// 1. my_func(); --> good
// 2. my_func(21); --> error
// 3. my_func("STACKOVERFLOW"); --> error
int main() { }
Here, you are telling your compiler implicitly that your main() function is not going to take any arguments. But, there's a plot twist, in C leaving the function without any arguments (type function() { }) means you can put any value/variable in it while calling that function. This prototype should be avoided.
int my_func()
{
return 100;
}
// the above function can be called as:
// 1. my_func(); --> no error
// 2. my_func(21); --> no error
// 3. my_func("STACKOVERFLOW"); --> no error

What is the difference between return 1 and exit (1) from a C main function? [duplicate]

This question already has answers here:
What is the difference between exit and return?
(5 answers)
return statement vs exit() in main()
(8 answers)
Closed 10 months ago.
In a C program, within the main function, is there a difference between
exit(1);
and
return 1;
?
exit represents immediate termination which will terminate program.
In the below program when we return value from main the destructors will be called but not in case when we use exit(value).
#include <iostream>
#include<string.h>
using namespace std;
class String {
private:
char* s;
int size;
public:
String(char*); // constructor
~String(); // destructor
};
String::String(char* c){
cout<<"Calling constructor"<<endl;
size = strlen(c);
s = new char[size + 1];
strcpy(s, c);
}
String::~String() {
cout<<"Calling destructor"<<endl;
delete[] s;
}
int main(){
String s("Hell o");
exit(1);
//return 1;
}

Function definition in Macros in C

This code throws out an error saying that "code undelcared."
#include<stdio.h>
#define MAIN() c##o##d##e
int main()
{
MAIN();
printf("C program");
return 0;
}
int code()
{
printf("C is life");
}
Whereas, this code runs smoothly.
#include<stdio.h>
#define MAIN() m##a##i##n
int main()
{
MAIN();
printf("C program");
return 0;
}
int code()
{
printf("C is life");
}
Output:
C program
This code also runs smoothly.
#include <stdio.h>
#define macro(n, a, i, m) m##a##i##n
#define MAIN macro(n, a, i, m)
int MAIN()
{
printf("C is life");
return 0;
}
Output:
C is life
In the first code, why doesn't the code work as like main? I don't know what is the process after the concatenation 'main' is completed by the macro. Kindly explain the process behind these codes, in simple terms. Thanks in advance.
I also tried by defining the code function.
#include<stdio.h>
#define MAIN() c##o##d##e
int code(void);
int main()
{
MAIN();
printf("C program");
return 0;
}
int code(void)
{
printf("C is life\n");
return 0;
}
Output:
C program
So, defining a function should not be the problem. My question is, what happens after concatenation? Thanks in advance.
In C (since the C99 standard) you need to declare all functions before you use them.
In the first example you haven't declared the code function before you attempt to use it. You solve it by adding a function prototype:
// Declare the function, also known as a function prototype
void code(void);
int main(void)
{
// ...
}
// Define the function
void code(void)
{
// ...
}
The second example works because you use main which is already declared at that point. If a function haven't been declared before, the definition also declares the function.
Also note that your macro after expansion doesn't actually call the code (or the main) function. This is good, since calling main recursively (directly or indirectly) is generally bad.
In C, you have to define the prototype of your function int code () before any calls :
int code (void);
The function main is declared in assembly code, this is why your second version compiles et runs correctly.
If you want to avoid this error please add -Wmissing-prototypes compilation flag in order to let the compiler checks for you if your function has a prototype.
The problem is that code() function is not yet declared when main() is compiled.
Either move code() before main():
#include<stdio.h>
#define MAIN() c##o##d##e
int code()
{
printf("C is life");
}
int main()
{
MAIN();
printf("C program");
return 0;
}
Or forward declare code():
#include<stdio.h>
#define MAIN() c##o##d##e
int code();
int main()
{
MAIN();
printf("C program");
return 0;
}
int code()
{
printf("C is life");
}
Just remember macros are replaced at preprocessor stage itself. In C, every function needs to be prototyped before using/calling it so that compiler knows in advance about it's arguments and return type to avoid conflicts.
Case 1 :- when below code blocks executes, macro name MAIN() got replaced with code.
#define MAIN() c##o##d##e
int main(){
MAIN();
printf("C program");
return 0;
}
int code(){
printf("C is life");
}
And it looks like below after pre-processor stage
int main(){
code; /* errorenous statement */
printf("C program");
return 0;
}
int code(){
printf("C is life");
}
observe the line code; in above code block, it cause the compilation error. When you run above code like gcc -Wall -Wstrict-prototypes -Werror test.c where it will convert warning into error.
error: ‘code’ undeclared (first use in this function) #define MAIN()
c##o##d##e
^
to solve this, declare the code() like before #define
int code(void); /* declaration */
There is one more warning converted into error
error: statement with no effect [-Werror=unused-value] #define MAIN()
c##o##d##e
Because after macro replacement it looks like code; and here compiler rightly complaining above statement with no effect. So to avoid this
change the macro name from MAIN() to MAIN. for e.g
#define MAIN c##o##d##e
Correct version of case-1 code
#include<stdio.h>
int code(void);
#define MAIN c##o##d##e
int main(void){
MAIN();
printf("C program");
return 0;
}
int code(void){
printf("C is life");
return 0;
}
And it produces output as
C is lifeC program
Case 2 :- when below code executes, macro name MAIN() gets replaced with main
#define MAIN() m##a##i##n
int main(){
MAIN();
printf("C program");
return 0;
}
int code(){
printf("C is life");
}
And it looks like at preprocessor stage
int main(){
main; /* it causes error */
printf("C program");
return 0;
}
int code(){
printf("C is life");
}
Case 3 :- when below code blocks executes, macro name MAIN() got replaced with code & here you declared the code() also.
#define MAIN() c##o##d##e
int code(void);
int main() {
MAIN();
printf("C program");
return 0;
}
int code(void) {
printf("C is life\n");
return 0;
}
And it looks like below after pre-processor stage
int code(void);
int main() {
code;/* error causing statement */
printf("C program");
return 0;
}
int code(void) {
printf("C is life\n");
return 0;
}
Suggest you to compile any C code with
gcc -Wall -Wstrict-prototypes -Werror test.c
so by converting warning to error you will learn more.

Can you help me explaining the following C code? [duplicate]

This question already has answers here:
C code explanation
(2 answers)
Closed 6 years ago.
int main(int argc, char **argv)
{
int (*func)();
func = (int (*)()) code;
(int)(*func)();
}
the variable code has some shellcode in it
Function pointers. This code snippet should help you understand.
#include <stdio.h>
int Hello();
int code();
int main(int argc, char **argv)
{
int (*func)(); //pointer to function that takes no arguments quivalent to: int (*func)(void);
func =&Hello;
int x = func();
printf("%d\n", x);
func = (int (*)()) code; // Assigns the pointer from the code function to the func pointer
x = code();
printf("%d", x);
}
int code()
{
printf("code returns: ");
return 500;
}
int Hello()
{
printf("hello returns: ");
return 1;
}
code probably is a variable that correspond to the address of some machine code in memory. Then a pointer to function that takes no parameter and returns an int is set to that address and the function is called. int f() is the prototype for a function with no param and int as return value, then int (*pf)() is a pointer to such a function.

why func() and func(void) are different [duplicate]

This question already has answers here:
in c: func(void) vs. func() [duplicate]
(2 answers)
Closed 9 years ago.
I have a function, can write in 2 ways.
void function(void) {
// operations....
}
and
void function() {
// operations......
}
Both functions are of same prototype. Why we have to mention void as argument at function definition?
No, both have different prototypes.
Compile the below programs you will understand.
void function1(void)
{
printf("In function1\n");
}
void function2()
{
printf("In function2\n");
}
int main()
{
function1();
function2(100); //Won't produce any error
return 0;
}
Program 2:
#include <stdio.h>
void function1(void)
{
printf("In function1\n");
}
void function2()
{
printf("In function2\n");
}
int main()
{
function1(100); //produces an error
function2();
return 0;
}
The correct way to say "no parameters" in C and C++ is
void function(void);
But when we write
void function();
It means a little different way in C and C++! It means "could take any number of parameters of unknown types", and in C++ it means the same as function(void).

Resources