By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

--
Ravi.T

Sep 25 '07 #1
Share this Question
Share on Google+
28 Replies


P: n/a
On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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.

Sep 25 '07 #2

P: n/a
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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.

Sep 25 '07 #3

P: n/a
On Sep 24, 10:49 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:


On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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

Sep 25 '07 #4

P: n/a
On Sep 24, 10:49 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:


On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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
>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.

Sep 25 '07 #5

P: n/a
On Sep 25, 11:14 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:49 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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.


Sep 25 '07 #6

P: n/a
On Sep 24, 11:30 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 11:14 am, user923005 <dcor...@connx.comwrote:


On Sep 24, 10:49 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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'

Sep 25 '07 #7

P: n/a
On Sep 25, 11:37 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 11:30 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 11:14 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:49 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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.


Sep 25 '07 #8

P: n/a
On Sep 24, 11:47 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 11:37 am, user923005 <dcor...@connx.comwrote:


On Sep 24, 11:30 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 11:14 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:49 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
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.

Sep 25 '07 #9

P: n/a
user923005 said:

<snip>
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?

--
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
Sep 25 '07 #10

P: n/a
On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalidwrote:
user923005 said:

<snip>
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?

--
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.

Sep 25 '07 #11

P: n/a
On Sep 25, 2:17 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalidwrote:


user923005 said:
<snip>
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?
--
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.- Hide quoted text -

- Show quoted text -
How is this possible ?

Thx,
Karthik Balaguru

Sep 25 '07 #12

P: n/a
On 25 sep, 07:03, ravi <gototh...@gmail.comwrote:
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.

Sep 25 '07 #13

P: n/a
On Sep 25, 3:27 pm, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
On Sep 25, 2:17 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalidwrote:
user923005 said:
<snip>
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?
--
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.- Hide quoted text -
- Show quoted text -

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.

--
Ravi.T

Sep 25 '07 #14

P: n/a
On Sep 25, 3:45 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 3:27 pm, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:


On Sep 25, 2:17 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalidwrote:
user923005 said:
<snip>
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?
--
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.- Hide quoted text -
- Show quoted text -
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.

--
Ravi.T- Hide quoted text -

- Show quoted text -
How do u say that it works ?
What did u get on the console ?

Karthik Balaguru

Sep 25 '07 #15

P: n/a
karthikbalaguru <ka***************@gmail.comwrites:
On Sep 25, 2:17 pm, ravi <gototh...@gmail.comwrote:
>On Sep 25, 2:09 pm, Richard Heathfield <r...@see.sig.invalidwrote:


user923005 said:
<snip>
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?
--
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.- Hide quoted text -

- Show quoted text -

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?
Sep 25 '07 #16

P: n/a
Thanks Deniau.

On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.comwrote:
On 25 sep, 07:03, ravi <gototh...@gmail.comwrote:
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.

Sep 25 '07 #17

P: n/a
Thanks Deniau.

On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.comwrote:
On 25 sep, 07:03, ravi <gototh...@gmail.comwrote:
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.

Sep 25 '07 #18

P: n/a
On 25 sep, 14:11, ravi <gototh...@gmail.comwrote:
Thanks Deniau.

On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.comwrote:
On 25 sep, 07:03, ravi <gototh...@gmail.comwrote:
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.
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+, ld.

Sep 25 '07 #19

P: n/a
On Mon, 24 Sep 2007 22:49:37 -0700, ravi wrote:
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.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 25 '07 #20

P: n/a
On Tue, 25 Sep 2007 01:10:33 -0700, user923005 wrote:
On Sep 24, 11:47 pm, ravi <gototh...@gmail.comwrote:
>On Sep 25, 11:37 am, user923005 <dcor...@connx.comwrote:


On Sep 24, 11:30 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 11:14 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:49 pm, ravi <gototh...@gmail.comwrote:
On Sep 25, 10:29 am, user923005 <dcor...@connx.comwrote:
On Sep 24, 10:03 pm, ravi <gototh...@gmail.comwrote:
Hello everybody,
[snip >100 lines]
How long are you two going to continue to quote entire posts?
Does anybody pays you a cent for each millisecond anyone takes to
scroll down?
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.
army1987@army1987-laptop:~$ cat foo.c myfunc.c
/* Begin foo.c */
#include <stdio.h>
extern void myfunc(void);
int main(int argc, char *argv[])
{
if (argv == (char **)0 && argc < 0)
puts("hello, world");
else
myfunc();
return 0;
}
/* End foo.c */

/* Begin myfunc.c */
#include <stdio.h>
extern int main(int argc, char *argv[]);
void myfunc(void)
{
puts("Hey, I'm myfunc().");
main(-1, (char **)0);
}
/* End myfunc.c */
army1987@army1987-laptop:~$ gcc -ansi -pedantic -Wall -Wextra -O3 myfunc.c -c
army1987@army1987-laptop:~$ gcc -ansi -pedantic -Wall -Wextra -O3 myfunc.o
foo.c
army1987@army1987-laptop:~$ ./a.out
Hey, I'm myfunc().
hello, world
army1987@army1987-laptop:~$
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 25 '07 #21

P: n/a
On Tue, 25 Sep 2007 05:11:21 -0700, ravi wrote:
Thanks Deniau.

On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.comwrote:
the main () function by user may be of different types
1. main()

not conformant -int main(void)
Only in C99.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 25 '07 #22

P: n/a
Army1987 said:
On Tue, 25 Sep 2007 05:11:21 -0700, ravi wrote:
>Thanks Deniau.

On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.comwrote:
> the main () function by user may be of different types
1. main()

not conformant -int main(void)
Only in C99.
int main() is C99-conforming.

--
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
Sep 25 '07 #23

P: n/a
user923005 wrote:

[Gigantic snip]
No.
Dann, there were over 170 lines of quotes in your message. Is there
some reason you thought that was appropriate?


Brian
Sep 25 '07 #24

P: n/a
On Mon, 24 Sep 2007 22:49:37 -0700, in comp.lang.c , ravi
<go*******@gmail.comwrote:
int main(int argc)
{
printf("Hello World\n");
return 0;
}
I am able to compile and run this.
You need to turn on your compiler warnings. Read the manual to find
out how to make it warn you properly.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 25 '07 #25

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Army1987 said:
>On Tue, 25 Sep 2007 05:11:21 -0700, ravi wrote:
>>On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.comwrote:
the main () function by user may be of different types
1. main()

not conformant -int main(void)
Only in C99.

int main() is C99-conforming.
But 'main()' isn't.

(There's some debate about whether 'int main()', as opposed to
'int main(void)', is conforming. The debate depends on some extremely
picky analysis of the wording of the standard. In practice,
'int main()' is almost certainly ok, but there's no good reason not to
write 'int main(void)'.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 25 '07 #26

P: n/a
Keith Thompson said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Army1987 said:
>>On Tue, 25 Sep 2007 05:11:21 -0700, ravi wrote:
On Sep 25, 3:42 pm, Laurent Deniau <Laurent.Den...@gmail.comwrote:
the main () function by user may be of different types
1. main()
>
not conformant -int main(void)
Only in C99.

int main() is C99-conforming.

But 'main()' isn't.
Yes, it's astonishing how powerful implicit int is. I could have sworn I'd
read int main(), not just main().
(There's some debate about whether 'int main()', as opposed to
'int main(void)', is conforming.
There is? I had no idea. Please do tell.
The debate depends on some extremely
picky analysis of the wording of the standard.
Picky works for me, provided it's good picky. But which bit are you talking
about?
In practice,
'int main()' is almost certainly ok, but there's no good reason not to
write 'int main(void)'.)
Agreed.

--
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
Sep 26 '07 #27

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
[...]
>(There's some debate about whether 'int main()', as opposed to
'int main(void)', is conforming.

There is? I had no idea. Please do tell.
>The debate depends on some extremely
picky analysis of the wording of the standard.

Picky works for me, provided it's good picky. But which bit are you talking
about?
>In practice,
'int main()' is almost certainly ok, but there's no good reason not to
write 'int main(void)'.)

Agreed.
I don't remember the details off the top of my head, which is *part*
of the reason I didn't explain further. I'll have to either track
down the discussion or recreate it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 26 '07 #28

P: n/a
Keith Thompson said:

<snip>
I don't remember the details off the top of my head, which is *part*
of the reason I didn't explain further. I'll have to either track
down the discussion or recreate it.
Well, I'm not particularly fussed about it, so don't put yourself out on my
account - but my own take is that I would *not* like to have to defend the
proposition that int main() is non-C99-conforming.

--
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
Sep 26 '07 #29

This discussion thread is closed

Replies have been disabled for this discussion.