without main()

M

mariz

hi ,
i m a new member to this group . i start from the beginig - main().
Can any one give me an idea for how to write a program in C without
using main() ? ie while looking source code it wont contain main
keyword.
 
I

Ian Collins

mariz said:
hi ,
i m a new member to this group . i start from the beginig - main().
Can any one give me an idea for how to write a program in C without
using main() ? ie while looking source code it wont contain main
keyword.

main is the name of a function, not a keyword. That function is the
entry point into a C program.

The only places you are likely to see C application without main are
either embedded applications or device drivers. The former may define
their own entry point, the latter tend to be loaded into a running
program (an operating system).
 
M

mariz

main is the name of a function, not a keyword.  That function is the
entry point into a C program.

The only places you are likely to see C application without main are
either embedded applications or device drivers.  The former may define
their own entry point, the latter tend to be loaded into a running
program (an operating system).

Ya i agree ur point but what i am trying to tell is the entry point
of c ( i.e main ) must be present in the .obj or intermediate files
after compilation, without this it wont run. But, why not can we make
a magic while seeing a source code in C without main? ie in our source
code only (.c file)
 
I

Ian Collins

*Please* don't quote signatures!
Ya i agree ur point but what i am trying to tell is the entry point
of c ( i.e main ) must be present in the .obj or intermediate files
after compilation, without this it wont run. But, why not can we make
a magic while seeing a source code in C without main? ie in our source
code only (.c file)

main() must be present in the final executable. A typical application
has many source files, only one contains the main() function.
 
C

Chris Dollin

mariz said:
hi ,
i m a new member to this group . i start from the beginig - main().
Can any one give me an idea for how to write a program in C without
using main() ?

If you want to write and run a program in a hosted C implementation,
then you have to define the function `main` and have that function do
whatever you want your program to do. That's because that's how the
C implementation knows what your program does.

[A "hosted" implementation is the sort of thing you get on a desktop,
or a mainframe, as opposed to a "standalone" implementation, the
sort of thing you use for embedded systems and you may not have
an OS or if you do it does rather less than RISC OS or Linux or
whatever.]
ie while looking source code it wont contain main keyword.

Well, that's trivially easy, since main isn't a keyword; but
no, without playing implementation magic games ["dear linker,
all references to `main` should be interpreted as `start`"],
Standard (hosted) C doesn't let you do that.

It's not that you can't do it; it's that doing it, even if
its possible, requires non-standard non-portable techniques.

Why do you care?
 
B

Bartc

mariz said:
hi ,
i m a new member to this group . i start from the beginig - main().
Can any one give me an idea for how to write a program in C without
using main() ? ie while looking source code it wont contain main
keyword.

Try this:

#include<stdio.h>

int start(void){
printf("Hello World!\n");
return 0;
}
 
B

Ben Bacarisse

Ian Collins said:
*Please* don't quote signatures!

main() must be present in the final executable. A typical application
has many source files, only one contains the main() function.

I smell one of the trick questions some instructors are so fond of:
"write a valid, portable program in which the word 'main' does not
appear". If that is the case, then it is as easy as it is silly (in
case it is homework, I won't "show and tell" but I am sure you can see
a way).
 
B

Bartc

Bartc said:
Try this:

#include<stdio.h>

int start(void){
printf("Hello World!\n");
return 0;
}

Oh, I didn't see the OP's reply where he understood about the 'main' entry
point being required in the post-compilation stages.

Nevertheless, I did manage to get this to run (whether I used the 'magic' he
was talking about, I don't know).
 
C

Chris Dollin

Walter said:
Portably possible in C89 or later. Your token answer deserves a good
pasting.

Oh, nicely done. Consider me egged.

I don't know if it qualifies as `not using main()` -

(fx:look-at-OP's-message)

- but it does satisfy "looking source code it wont contain main
keyword."

(I still want to know why the OP cares.)

--
"There's something about this place," said Peter presently, /Gaudy Night/
"that alters one's values."

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
O

ozbear

Oh, nicely done. Consider me egged.

I don't know if it qualifies as `not using main()` -

(fx:look-at-OP's-message)

- but it does satisfy "looking source code it wont contain main
keyword."

(I still want to know why the OP cares.)

Colour me confused.

What do you think the poster is really asking (because I don't
understand your reply) and how are you suggesting this is done?

I suspect preprocessor tricks but an unsure.

Oz
 
P

Peter Nilsson

Ian said:
main is the name of a function, not a keyword.

Actually it's just an identifier. It needn't be the name of a
function.
That function is the entry point into a C program.

Into a C program for a hosted implementation, but not necessarily
for a free-standing one.
The only places you are likely to see C application without main are
either embedded applications or device drivers.

I see WinMain programs all the time.
 
K

Kaz Kylheku

hi ,
 i m a new member to this group . i start from the beginig - main().
 Can any one give me an idea for how to write a program in C without
using main() ? ie while looking source code it wont contain main
keyword.

If you don't want the keyword main to appear anywhere in the source
code, what you have to do is build that identifier out of at least two
separate fragments. Moreover you can do it in such a way that even
searching the program for the regular expression 'm.*a.*i.*n' will
fail.

The preprocessing stage of the C language has a token-pasting operator
## which can combine two preprocessor tokens into one (provided that
the result has the syntax of a valid token).

This ## operator can only be used in the replacement text of a
preprocessor macro, e.g.

#define glue_together(a, b) a ## b

Now try to do the rest of this homework by yourself.

And be aware that using token pasting to conceal identifiers is
usually a terrible idea. It makes the source code impervious to cross-
referencing tools.
 
S

santosh

mariz said:
hi ,
i m a new member to this group . i start from the beginig - main().
Can any one give me an idea for how to write a program in C without
using main() ? ie while looking source code it wont contain main
keyword.

Study what the ## preprocessor operator does. You can use that to form
tokens that would not be present in the source code.
 
H

harini

hi ,
i m a new member to this group . i start from the beginig - main().
Can any one give me an idea for how to write a program in C without
using main() ? ie while looking source code it wont contain main
keyword.

Just check with the #pragma directive. I think u can direct the
compiler to call the userdefined function
 
F

Flash Gordon

harini wrote, On 22/04/08 06:13:
Just check with the #pragma directive. I think u can direct the
compiler to call the userdefined function

You might on your implementation but in general you cannot. Do you know
what implementation mariz is using?
 
K

Keith Thompson

harini said:
Just check with the #pragma directive. I think u can direct the
compiler to call the userdefined function

There is no standard-defined #pragma directive that will cause a
function other than "main" to be used as the program entry point.
There might be an implementation-defined one, but I've never heard of
such a thing (or a good reason for it to exist).
 

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

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top