how #define a main() function and call our own main function?

R

ravi

Hello everybody,

I am writing a small application which does some work before the user
main function starts execution.


I am trying to #define the main function.

But the problem is that,

the main () function by user may be of different types
1. main()
2.main(int argc)
3. main(int argc, char *argv[])

How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.

I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)

But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.

Can I have some suggestions on this?

Thanks in advance.
 
U

user923005

Hello everybody,

I am writing a small application which does some work before the user
main function starts execution.

I am trying to #define the main function.

But the problem is that,

the main () function by user may be of different types
1. main()
2.main(int argc)

#2 is strictly not allowed by the language.
3. main(int argc, char *argv[])

How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.

I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)

But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.

Can I have some suggestions on this?

You only need this one:

int main(int argc, char **argv);

If there are no arguments, then argc will be zero or 1.
 
R

ravi

Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
the main () function by user may be of different types
1. main()
2.main(int argc)

#2 is strictly not allowed by the language.


3. main(int argc, char *argv[])
How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.
I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)
But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.
Can I have some suggestions on this?

You only need this one:

int main(int argc, char **argv);

If there are no arguments, then argc will be zero or 1.

Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;
}
I am able to compile and run this.
 
U

user923005

#2 is strictly not allowed by the language.
3. main(int argc, char *argv[])
How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.
I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)
But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.
Can I have some suggestions on this?
You only need this one:
int main(int argc, char **argv);
If there are no arguments, then argc will be zero or 1.

Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;}

I am able to compile and run this

That's funny. It made demons fly out of my nose. I guess I should
have paid attention to my error checkers:

C:\tmp>splint t.c
Splint 3.1.1 --- 12 Mar 2007

t.c(1,17): Function main declared with 1 arg, should have 2 (int argc,
char
*argv[])
The function main does not match the expected type. (Use -maintype
to inhibit
warning)
t.c: (in function main)
t.c(1,26): Parameter argc not used
A function parameter is not used in the body of the function. If the
argument
is needed for type compatibility or future plans, use /*@unused@*/
in the
argument declaration. (Use -paramuse to inhibit warning)

Finished checking --- 2 code warnings

C:\tmp>type t.c
int main(int argc)
{
printf("Hello World\n");
return 0;
}

C:\tmp>lin t.c

C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP) t.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006

--- Module: t.c (C)

C:\tmp>type _LINT.TMP | more

--- Module: t.c (C)
_
printf("Hello World\n");
t.c(3) : Info 718: Symbol 'printf' undeclared, assumed to return int
t.c(3) : Info 746: call to function 'printf()' not made in the
presence of a
prototype
_
}
t.c(6) : Info 715: Symbol 'argc' (line 1) not referenced
t.c(1) : Info 830: Location cited in prior message
_
}
t.c(6) : Note 952: Parameter 'argc' (line 1) could be declared const
--- Eff.
C++ 3rd Ed. item 3
t.c(1) : Info 830: Location cited in prior message

--- Global Wrap-up

Warning 526: Symbol 'printf()' (line 3, file t.c) not defined
t.c(3) : Info 830: Location cited in prior message
Warning 628: no argument information provided for function
'printf()' (line 3,
file t.c)
t.c(3) : Info 830: Location cited in prior message
 
U

user923005

#2 is strictly not allowed by the language.
3. main(int argc, char *argv[])
How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.
I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)
But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.
Can I have some suggestions on this?
You only need this one:
int main(int argc, char **argv);
If there are no arguments, then argc will be zero or 1.

Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;}

I am able to compile and run this
From the C FAQ:

11.12a: What's the correct declaration of main()?

A: Either int main(), int main(void), or int main(int argc,
char *argv[]) (with alternate spellings of argc and *argv[]
obviously allowed). See also questions 11.12b to 11.15 below.

References: ISO Sec. 5.1.2.2.1, Sec. G.5.1; H&S Sec. 20.1 p.
416; CT&P Sec. 3.10 pp. 50-51.
 
R

ravi

Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
the main () function by user may be of different types
1. main()
2.main(int argc)
#2 is strictly not allowed by the language.
3. main(int argc, char *argv[])
How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.
I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)
But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.
Can I have some suggestions on this?
You only need this one:
int main(int argc, char **argv);
If there are no arguments, then argc will be zero or 1.
Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;}
I am able to compile and run this

That's funny. It made demons fly out of my nose. I guess I should
have paid attention to my error checkers:

C:\tmp>splint t.c
Splint 3.1.1 --- 12 Mar 2007

t.c(1,17): Function main declared with 1 arg, should have 2 (int argc,
char
*argv[])
The function main does not match the expected type. (Use -maintype
to inhibit
warning)
t.c: (in function main)
t.c(1,26): Parameter argc not used
A function parameter is not used in the body of the function. If the
argument
is needed for type compatibility or future plans, use /*@unused@*/
in the
argument declaration. (Use -paramuse to inhibit warning)

Finished checking --- 2 code warnings

C:\tmp>type t.c
int main(int argc)
{
printf("Hello World\n");
return 0;

}

C:\tmp>lin t.c

C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP) t.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006

--- Module: t.c (C)

C:\tmp>type _LINT.TMP | more

--- Module: t.c (C)
_
printf("Hello World\n");
t.c(3) : Info 718: Symbol 'printf' undeclared, assumed to return int
t.c(3) : Info 746: call to function 'printf()' not made in the
presence of a
prototype
_}

t.c(6) : Info 715: Symbol 'argc' (line 1) not referenced
t.c(1) : Info 830: Location cited in prior message
_}

t.c(6) : Note 952: Parameter 'argc' (line 1) could be declared const
--- Eff.
C++ 3rd Ed. item 3
t.c(1) : Info 830: Location cited in prior message

--- Global Wrap-up

Warning 526: Symbol 'printf()' (line 3, file t.c) not defined
t.c(3) : Info 830: Location cited in prior message
Warning 628: no argument information provided for function
'printf()' (line 3,
file t.c)
t.c(3) : Info 830: Location cited in prior message

I have compiled it using gcc. But if I use splint to compile , i get
the same as u have written.
I assume here the user going for gcc instead of any other.

Why is gcc not reporting the same when splint reports it. I mean if
its not the usage of the language gcc should also report that warning
by defaultl.
 
U

user923005

Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
the main () function by user may be of different types
1. main()
2.main(int argc)
#2 is strictly not allowed by the language.
3. main(int argc, char *argv[])
How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.
I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)
But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.
Can I have some suggestions on this?
You only need this one:
int main(int argc, char **argv);
If there are no arguments, then argc will be zero or 1.
Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;}
I am able to compile and run this
That's funny. It made demons fly out of my nose. I guess I should
have paid attention to my error checkers:
C:\tmp>splint t.c
Splint 3.1.1 --- 12 Mar 2007
t.c(1,17): Function main declared with 1 arg, should have 2 (int argc,
char
*argv[])
The function main does not match the expected type. (Use -maintype
to inhibit
warning)
t.c: (in function main)
t.c(1,26): Parameter argc not used
A function parameter is not used in the body of the function. If the
argument
is needed for type compatibility or future plans, use /*@unused@*/
in the
argument declaration. (Use -paramuse to inhibit warning)
Finished checking --- 2 code warnings
C:\tmp>type t.c
int main(int argc)
{
printf("Hello World\n");
return 0;

C:\tmp>lin t.c
C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP) t.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006
--- Module: t.c (C)
C:\tmp>type _LINT.TMP | more
--- Module: t.c (C)
_
printf("Hello World\n");
t.c(3) : Info 718: Symbol 'printf' undeclared, assumed to return int
t.c(3) : Info 746: call to function 'printf()' not made in the
presence of a
prototype
_}
t.c(6) : Info 715: Symbol 'argc' (line 1) not referenced
t.c(1) : Info 830: Location cited in prior message
_}
t.c(6) : Note 952: Parameter 'argc' (line 1) could be declared const
--- Eff.
C++ 3rd Ed. item 3
t.c(1) : Info 830: Location cited in prior message
--- Global Wrap-up
Warning 526: Symbol 'printf()' (line 3, file t.c) not defined
t.c(3) : Info 830: Location cited in prior message
Warning 628: no argument information provided for function
'printf()' (line 3,
file t.c)
t.c(3) : Info 830: Location cited in prior message

I have compiled it using gcc. But if I use splint to compile , i get
the same as u have written.
I assume here the user going for gcc instead of any other.

Why is gcc not reporting the same when splint reports it. I mean if
its not the usage of the language gcc should also report that warning
by defaultl.

There are some things that require a warning and others that don't. C
let's you shoot yourself in the foot sometimes. However, in this case
you were simply using the tool incorrectly:

dcorbit@DCORBIT64 /c/tmp
$ gcc -W -Wall -ansi -pedantic t.c
t.c:2: warning: 'main' takes only zero or two arguments
t.c: In function 'main':
t.c:3: warning: implicit declaration of function 'printf'
t.c:3: warning: incompatible implicit declaration of built-in function
'printf'
t.c: At top level:
t.c:1: warning: unused parameter 'argc'
 
R

ravi

Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
the main () function by user may be of different types
1. main()
2.main(int argc)
#2 is strictly not allowed by the language.
3. main(int argc, char *argv[])
How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.
I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)
But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.
Can I have some suggestions on this?
You only need this one:
int main(int argc, char **argv);
If there are no arguments, then argc will be zero or 1.
Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;}
I am able to compile and run this
That's funny. It made demons fly out of my nose. I guess I should
have paid attention to my error checkers:
C:\tmp>splint t.c
Splint 3.1.1 --- 12 Mar 2007
t.c(1,17): Function main declared with 1 arg, should have 2 (int argc,
char
*argv[])
The function main does not match the expected type. (Use -maintype
to inhibit
warning)
t.c: (in function main)
t.c(1,26): Parameter argc not used
A function parameter is not used in the body of the function. If the
argument
is needed for type compatibility or future plans, use /*@unused@*/
in the
argument declaration. (Use -paramuse to inhibit warning)
Finished checking --- 2 code warnings
C:\tmp>type t.c
int main(int argc)
{
printf("Hello World\n");
return 0;
}
C:\tmp>lin t.c
C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP) t.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006
--- Module: t.c (C)
C:\tmp>type _LINT.TMP | more
--- Module: t.c (C)
_
printf("Hello World\n");
t.c(3) : Info 718: Symbol 'printf' undeclared, assumed to return int
t.c(3) : Info 746: call to function 'printf()' not made in the
presence of a
prototype
_}
t.c(6) : Info 715: Symbol 'argc' (line 1) not referenced
t.c(1) : Info 830: Location cited in prior message
_}
t.c(6) : Note 952: Parameter 'argc' (line 1) could be declared const
--- Eff.
C++ 3rd Ed. item 3
t.c(1) : Info 830: Location cited in prior message
--- Global Wrap-up
Warning 526: Symbol 'printf()' (line 3, file t.c) not defined
t.c(3) : Info 830: Location cited in prior message
Warning 628: no argument information provided for function
'printf()' (line 3,
file t.c)
t.c(3) : Info 830: Location cited in prior message
I have compiled it using gcc. But if I use splint to compile , i get
the same as u have written.
I assume here the user going for gcc instead of any other.
Why is gcc not reporting the same when splint reports it. I mean if
its not the usage of the language gcc should also report that warning
by defaultl.

There are some things that require a warning and others that don't. C
let's you shoot yourself in the foot sometimes. However, in this case
you were simply using the tool incorrectly:

dcorbit@DCORBIT64 /c/tmp
$ gcc -W -Wall -ansi -pedantic t.c
t.c:2: warning: 'main' takes only zero or two arguments
t.c: In function 'main':
t.c:3: warning: implicit declaration of function 'printf'
t.c:3: warning: incompatible implicit declaration of built-in function
'printf'
t.c: At top level:
t.c:1: warning: unused parameter 'argc'

Thnks for that response.

But my main problem ,

How to call the user main fuction in my main function, when I don't
know the user main function either #1 or #3 and with knowing the name
of the parameters.
we assume to parse the source code, but there is also an issue that
main function may be called from a user defined library instead of
compiling source code.
 
U

user923005

Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
the main () function by user may be of different types
1. main()
2.main(int argc)
#2 is strictly not allowed by the language.
3. main(int argc, char *argv[])
How to #define all these three possibilities. And there is not
guarantee that the user is going to use the same names "argc &argv"
all the times. we need also know the argments name if needed to access
them.
I am trying to do this.
#define main(a,b) main(a,b){
my_function();
user_main(argc,argv);
}
user_main(a,b)
But this will not work if we have to deal with above listed 3 cases.
and also the agrument names problem.
Can I have some suggestions on this?
You only need this one:
int main(int argc, char **argv);
If there are no arguments, then argc will be zero or 1.
Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;}
I am able to compile and run this
That's funny. It made demons fly out of my nose. I guess I should
have paid attention to my error checkers:
C:\tmp>splint t.c
Splint 3.1.1 --- 12 Mar 2007
t.c(1,17): Function main declared with 1 arg, should have 2 (int argc,
char
*argv[])
The function main does not match the expected type. (Use -maintype
to inhibit
warning)
t.c: (in function main)
t.c(1,26): Parameter argc not used
A function parameter is not used in the body of the function. If the
argument
is needed for type compatibility or future plans, use /*@unused@*/
in the
argument declaration. (Use -paramuse to inhibit warning)
Finished checking --- 2 code warnings
C:\tmp>type t.c
int main(int argc)
{
printf("Hello World\n");
return 0;
}
C:\tmp>lin t.c
C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP) t.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006
--- Module: t.c (C)
C:\tmp>type _LINT.TMP | more
--- Module: t.c (C)
_
printf("Hello World\n");
t.c(3) : Info 718: Symbol 'printf' undeclared, assumed to return int
t.c(3) : Info 746: call to function 'printf()' not made in the
presence of a
prototype
_}
t.c(6) : Info 715: Symbol 'argc' (line 1) not referenced
t.c(1) : Info 830: Location cited in prior message
_}
t.c(6) : Note 952: Parameter 'argc' (line 1) could be declared const
--- Eff.
C++ 3rd Ed. item 3
t.c(1) : Info 830: Location cited in prior message
--- Global Wrap-up
Warning 526: Symbol 'printf()' (line 3, file t.c) not defined
t.c(3) : Info 830: Location cited in prior message
Warning 628: no argument information provided for function
'printf()' (line 3,
file t.c)
t.c(3) : Info 830: Location cited in prior message
---
output placed in _LINT.TMP
I have compiled it using gcc. But if I use splint to compile , i get
the same as u have written.
I assume here the user going for gcc instead of any other.
Why is gcc not reporting the same when splint reports it. I mean if
its not the usage of the language gcc should also report that warning
by defaultl.
There are some things that require a warning and others that don't. C
let's you shoot yourself in the foot sometimes. However, in this case
you were simply using the tool incorrectly:
dcorbit@DCORBIT64 /c/tmp
$ gcc -W -Wall -ansi -pedantic t.c
t.c:2: warning: 'main' takes only zero or two arguments
t.c: In function 'main':
t.c:3: warning: implicit declaration of function 'printf'
t.c:3: warning: incompatible implicit declaration of built-in function
'printf'
t.c: At top level:
t.c:1: warning: unused parameter 'argc'

Thnks for that response.

But my main problem ,

How to call the user main fuction in my main function, when I don't
know the user main function either #1 or #3 and with knowing the name
of the parameters.
we assume to parse the source code, but there is also an issue that
main function may be called from a user defined library instead of
compiling source code

No. The main() function cannot be called from a user defined library.
The main() function is special. It defines the entry point for
program startup.
 
R

Richard Heathfield

user923005 said:

The main() function cannot be called from a user defined library.

C&V please.
The main() function is special. It defines the entry point for
program startup.

Agreed. Er, so? What stops you putting it in a library? Surely it's all one
as far as the linker is concerned?
 
R

ravi

user923005 said:



C&V please.


Agreed. Er, so? What stops you putting it in a library? Surely it's all one
as far as the linker is concerned?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

I can write a main function in another file and compile it as a
library link it to another program where there is no main , but some
sub functions which are called by main. and it works well.
 
K

karthikbalaguru

I can write a main function in another file and compile it as a
library link it to another program where there is no main , but some
sub functions which are called by main. and it works well.- Hide quoted text -

- Show quoted text -

How is this possible ?

Thx,
Karthik Balaguru
 
L

Laurent Deniau

Hello everybody,

I am writing a small application which does some work before the user
main function starts execution.

I am trying to #define the main function.

But the problem is that,

the main () function by user may be of different types
1. main()

not conformant -> int main(void)
2.main(int argc)

not conformant
3. main(int argc, char *argv[])


// prelude, already posted here many times
#define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
#define COS_PP_CAT_(a,b) a ## b
#define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
#define COS_PP_NARG_N_( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))

// your problem
#define main(...) \
COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)

#define MAIN_1() \
main(void) { \
int user_main(void); \
your_main(0,0); \
user_main(); \
} \
int user_main(void)

#define MAIN_2(ARGC,ARGV) \
main(int argc, char *argv[]) { \
int user_main(ARGC,ARGV); \
your_main(argc,argv); \
user_main(argc,argv); \
}
int user_main(ARGC,ARGV)

int main(int my_argc, char *my_argc[])
{
// code using my_argc and my_argv after your_main() has been called.
}

I just wrote this code on the fly, so it is untested and assumes the
availability of c99 variadic macros.

I use very often this kind of macro dispatch based on its number of
argument in COS (macros with optional arguments). PP_NARG has been
left as posted here in the past, but you could reduce the maximum
number of argument for you case.

a+, ld.
 
R

ravi

How is this possible ?

Thx,
Karthik Balaguru

give a trial.
Write a file with main() function calling a function foo().
main.c :
int main()
{
printf("MAIN\n");
foo();
return 0;
}

Compile it and make it a library like this:
> gcc -c main.c -o main.o
> ar -rc libmain.a main.o

Now write a file foo.c with foo() function.
foo.c:
void foo()
{
printf("FOOOOOOOOO\n");
}
Now compile like this:
> gcc foo.c -lmain
it works and u can try running.
 
K

karthikbalaguru

give a trial.
Write a file with main() function calling a function foo().
main.c :
int main()
{
printf("MAIN\n");
foo();
return 0;
}

Compile it and make it a library like this:

Now write a file foo.c with foo() function.
foo.c:
void foo()
{
printf("FOOOOOOOOO\n");
}
Now compile like this:
it works and u can try running.

How do u say that it works ?
What did u get on the console ?

Karthik Balaguru
 
R

Richard

karthikbalaguru said:
How is this possible ?

You should ask "why would it NOT be possible"?

If main calls 3 functions - a(),b() and c() what's to stop you linking in
different versions of these functions for the same main() for different purposes?
 
R

ravi

Thanks Deniau.

Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
the main () function by user may be of different types
1. main()

not conformant -> int main(void)
2.main(int argc)

not conformant
3. main(int argc, char *argv[])

// prelude, already posted here many times
#define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
#define COS_PP_CAT_(a,b) a ## b
#define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
#define COS_PP_NARG_N_( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N


Where r u calling this macro COS_PP_NARG_N_(__VA_ARGS__).
How does it replace _1/_2/_3......... how it detects the count of
arguments?
please clarify me.

#define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))

// your problem
#define main(...) \
COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)

#define MAIN_1() \
main(void) { \
int user_main(void); \
your_main(0,0); \
user_main(); \
} \
int user_main(void)

#define MAIN_2(ARGC,ARGV) \
main(int argc, char *argv[]) { \
int user_main(ARGC,ARGV); \
your_main(argc,argv); \
user_main(argc,argv); \
}
int user_main(ARGC,ARGV)

int main(int my_argc, char *my_argc[])
{
// code using my_argc and my_argv after your_main() has been called.

}

I just wrote this code on the fly, so it is untested and assumes the
availability of c99 variadic macros.

I use very often this kind of macro dispatch based on its number of
argument in COS (macros with optional arguments). PP_NARG has been
left as posted here in the past, but you could reduce the maximum
number of argument for you case.

a+, ld.
 
R

ravi

Thanks Deniau.

Hello everybody,
I am writing a small application which does some work before the user
main function starts execution.
I am trying to #define the main function.
But the problem is that,
the main () function by user may be of different types
1. main()

not conformant -> int main(void)
2.main(int argc)

not conformant
3. main(int argc, char *argv[])

// prelude, already posted here many times
#define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
#define COS_PP_CAT_(a,b) a ## b
#define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
#define COS_PP_NARG_N_( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N


Where r u calling this macro COS_PP_NARG_N_(__VA_ARGS__).
How does it replace _1/_2/_3......... how it detects the count of
arguments?
please clarify me.

#define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))

// your problem
#define main(...) \
COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)

#define MAIN_1() \
main(void) { \
int user_main(void); \
your_main(0,0); \
user_main(); \
} \
int user_main(void)

#define MAIN_2(ARGC,ARGV) \
main(int argc, char *argv[]) { \
int user_main(ARGC,ARGV); \
your_main(argc,argv); \
user_main(argc,argv); \
}
int user_main(ARGC,ARGV)

int main(int my_argc, char *my_argc[])
{
// code using my_argc and my_argv after your_main() has been called.

}

I just wrote this code on the fly, so it is untested and assumes the
availability of c99 variadic macros.

I use very often this kind of macro dispatch based on its number of
argument in COS (macros with optional arguments). PP_NARG has been
left as posted here in the past, but you could reduce the maximum
number of argument for you case.

a+, ld.
 
L

Laurent Deniau

Thanks Deniau.

not conformant -> int main(void)
not conformant
3. main(int argc, char *argv[])
// prelude, already posted here many times
#define COS_PP_CAT(a,b) COS_PP_CAT_(a,b)
#define COS_PP_CAT_(a,b) a ## b
#define COS_PP_NARG_(...) COS_PP_NARG_N_(__VA_ARGS__)
#define COS_PP_NARG_N_( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N

Where r u calling this macro COS_PP_NARG_N_(__VA_ARGS__).
How does it replace _1/_2/_3......... how it detects the count of
arguments?
please clarify me.

Oops, I usual with untested code some macros are missing (googleing
this newsgroup or comp.std.c would have pointed my OP on this macro
some years ago):

#define COS_PP_NARG(...) COS_PP_NARG_(__VA_ARGS__,COS_PP_REVSEQ_N(),)
#define COS_PP_REVSEQ_N() \
63,62,61, \
60,59,58,57,56,55,54,53,52,51, \
50,49,48,47,46,45,44,43,42,41, \
40,39,38,37,36,35,34,33,32,31, \
30,29,28,27,26,25,24,23,22,21, \
20,19,18,17,16,15,14,13,12,11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
#define COS_PP_CAT_NARG(a,...) COS_PP_CAT(a,COS_PP_NARG(__VA_ARGS__))
// your problem
#define main(...) \
COS_PP_CAT_NARG(MAIN_,__VA_ARGS__)(__VA_ARGS__)
#define MAIN_1() \
main(void) { \
int user_main(void); \
your_main(0,0); \
user_main(); \
} \
int user_main(void)
#define MAIN_2(ARGC,ARGV) \
main(int argc, char *argv[]) { \
int user_main(ARGC,ARGV); \
your_main(argc,argv); \
user_main(argc,argv); \
}
int user_main(ARGC,ARGV)
int main(int my_argc, char *my_argc[])
{
// code using my_argc and my_argv after your_main() has been called.

I just wrote this code on the fly, so it is untested and assumes the
availability of c99 variadic macros.
I use very often this kind of macro dispatch based on its number of
argument in COS (macros with optional arguments). PP_NARG has been
left as posted here in the past, but you could reduce the maximum
number of argument for you case.
 
A

Army1987

Can you please try this program:
example.c:
int main(int argc)
{
printf("Hello World\n");
return 0;
}
I am able to compile and run this.

Can you please try this program:
#include <time.h>
#include <stdio.h>
size_t main(struct tm argc)
{
return puts("hello, world") < 0 ? 0x17 : !mktime(&argc);
}
I am able to compile and run this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top