[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: Non-constant initializers

Joel C. Salomon

5/16/2011 3:06:00 PM

On Saturday, May 14, 2011 6:09:33 PM UTC-4, Uncle Steve wrote:
> I just know I'm going to get into trouble continuing with this. Might
> I suggest that it wouldn't be too terrible if the C compiler had
> provisions for well-defined user initialization code, happening after
> all the libc initialization, etc.? I know it's not in the standard,
> but it would be kind of nice to have.

But C *does* have this feature: Define a function with the prototype "void init(void)" where all the initialization code is to take place. To let the compiler know that you are using this feature, write your main function thus:

int main(void)
{ init(); // notice this on the same line as the opening brace
// declarations, code, etc. follow
}

:)

--Joel
1 Answer

Shao Miller

5/16/2011 3:29:00 PM

0

On 5/16/2011 11:06, Joel C. Salomon wrote:
> On Saturday, May 14, 2011 6:09:33 PM UTC-4, Uncle Steve wrote:
>> I just know I'm going to get into trouble continuing with this. Might
>> I suggest that it wouldn't be too terrible if the C compiler had
>> provisions for well-defined user initialization code, happening after
>> all the libc initialization, etc.? I know it's not in the standard,
>> but it would be kind of nice to have.
>
> But C *does* have this feature: Define a function with the prototype "void init(void)" where all the initialization code is to take place. To let the compiler know that you are using this feature, write your main function thus:
>
> int main(void)
> { init(); // notice this on the same line as the opening brace
> // declarations, code, etc. follow
> }
>
> :)

Heheheh. Or for C < C99, you could avoid mixed declarations and
statements with something like:

#define INIT int INIT = init()

int init(void) {
/* Populate globals */
/* Check for errors, report to user, call exit() upon failure */
return 1;
}

int main(void) {
INIT;
int foo;
char bar[] = "Bar";

/* Program */
return 0;
}