Non constant initializer

  • Thread starter prasoonthegreat
  • Start date
P

prasoonthegreat

Consider the following code......

#include<stdio.h>
#include<stdlib.h>
int fact()
{
int i , j=1;
if( scanf ( "%d" , &i )) {
while( i ) {
if ( j = j * i-- ) {
}
}
if( printf ( "%d" , j )) {
{}
}
}
exit(0);
return 0;
}

int x=fact();
int main(){}

This is not a valid C program......(since initializer used to
initialize a global variable is not a constant)
The code is to be modified such that it should work without having
anything inside main( )......

What are the necessary modifications required to have a successful
compilation.......

Prasoon
 
M

mfhaigh

Consider the following code......

#include<stdio.h>
#include<stdlib.h>
int fact()
{
int i , j=1;
if( scanf ( "%d" , &i )) {
while( i ) {
if ( j = j * i-- ) {}
}

if( printf ( "%d" , j )) {
{}}
}

exit(0);
return 0;

}

int x=fact();
int main(){}

This is not a valid C program......(since initializer used to
initialize a global variable is not a constant)
The code is to be modified such that it should work without having
anything inside main( )......

What are the necessary modifications required to have a successful
compilation.......

First, if you want help, format the code in a way that doesn't make
our eyes bleed.

Second, main() is the entry point of C programs. If you don't put
code to call fact() into main, then it's not going to get run.

Third, if you don't want fact() to take arguments, you should declare
it 'int fact(void)'.

Fourth, what on earth are you trying to achieve with this?
exit(0);
return 0;

Perhaps you want 'void fact(void)'?

Mark F. Haigh
(e-mail address removed)
 
P

prasoonthegreat

First, if you want help, format the code in a way that doesn't make
our eyes bleed.

Second, main() is the entry point of C programs.  If you don't put
code to call fact() into main, then it's not going to get run.

Third, if you don't want fact() to take arguments, you should declare
it 'int fact(void)'.

Fourth, what on earth are you trying to achieve with this?

sorry for that .... :(

What about the necessary corrections that are to be made regardless of
what you said about main.....

Cheers!!!!
PS
 
P

prasoonthegreat

sorry for that .... :(

What about the necessary corrections that are to be made regardless of
what you said about main.....

Cheers!!!!
PS

Will the solution be in accordance with Standard C.....
 
S

Spiros Bousbouras

Will the solution be in accordance with Standard C.....

The following is a solution in accordance with Standard C:

int main(void) {
return 0;
}
 
E

Eric Sosman

Consider the following code......
[... see up-thread ...]

This is not a valid C program......(since initializer used to
initialize a global variable is not a constant)
The code is to be modified such that it should work without having
anything inside main( )......

What does "work" mean? What is the program supposed
to do? A program with nothing inside its main() function can
do very little.
What are the necessary modifications required to have a successful
compilation.......

There is a countably infinite set of possible modifications
leading to compilable programs, so I hope you'll forgive me for
not listing them. Among them, one that stands out is: Delete
everything except the main() function, and leave the body of that
function empty.
 
K

Keith Thompson

Eric Sosman said:
Consider the following code......
[... see up-thread ...]

This is not a valid C program......(since initializer used to
initialize a global variable is not a constant)
The code is to be modified such that it should work without having
anything inside main( )......

What does "work" mean? What is the program supposed
to do? A program with nothing inside its main() function can
do very little.
What are the necessary modifications required to have a successful
compilation.......

There is a countably infinite set of possible modifications
leading to compilable programs, so I hope you'll forgive me for
not listing them. Among them, one that stands out is: Delete
everything except the main() function, and leave the body of that
function empty.

I actually think the intent is sufficiently clear. Assume for the
moment that non-constant initializers are actually allowed at file
scope, with the obvious semantics. That defines the intended behavior
of the posted program. Now modify the program so it retains the same
behavior without depending on such an initializer, and without adding
anything to main().

It's a silly question, yet another one of the form "How can you drive
a screw into a piece of wood -- oh, and you can't use a screwdriver".
The only sensible real-world answer is simply to add code to the body
of main(); that's what main() is for.

On the other hand, I'm not necessarily opposed to such questions. In
a teaching environment, the point is to create a challenging puzzle,
which is easier to do if the student doesn't have the entire language
at his disposal; there are equally challenging puzzles without silly
restrictions, but they tend to be too advanced for beginning classes.

On the other other hand, I can't think of a way to solve it off the
top of my head. There might be some trick involving abuse of the
preprocessor, but in any case I'm not going to do the OP's homework
for him (such questions are almost invariably homework).
 
A

Amandil

Eric Sosman said:
Consider the following code......
[... see up-thread ...]
This is not a valid C program......(since initializer used to
initialize a global variable is not a constant)
The code is to be modified such that it should work without having
anything inside main( )......
    What does "work" mean?  What is the program supposed
to do?  A program with nothing inside its main() function can
do very little.
    There is a countably infinite set of possible modifications
leading to compilable programs, so I hope you'll forgive me for
not listing them.  Among them, one that stands out is: Delete
everything except the main() function, and leave the body of that
function empty.

I actually think the intent is sufficiently clear.  Assume for the
moment that non-constant initializers are actually allowed at file
scope, with the obvious semantics.  That defines the intended behavior
of the posted program.  Now modify the program so it retains the same
behavior without depending on such an initializer, and without adding
anything to main().
On the other other hand, I can't think of a way to solve it off the
top of my head.  There might be some trick involving abuse of the
preprocessor, but in any case I'm not going to do the OP's homework
for him (such questions are almost invariably homework).

It is possible (but in such case, also the realm of a different group)
that the OP indeed has a homework question, but is taking a class in C+
+. As such, he could put the desired code into a constructor and
declare a global object. Maybe C++ even allows for non-constant
initializers.

So, I concur with Keith that it can't be done in C. But if you're
using a different language, then it is both possible and not for
discussion here.

-- Marty Amandil
 
P

prasoonthegreat

By the way it was not a homework problem .......I just wanted to ask
whether it can be done in C using any method(preprocessor involvement
etc)...

Yes I know C++ supports non-constant initializers for global
variables......

As far as learning C++ is concerned I am learning it too and the code
works in C++ quite easily .....

Anyways thanks everyone......

Cheers!!!!

Prasoon
 
K

Keith Thompson

By the way it was not a homework problem .......I just wanted to ask
whether it can be done in C using any method(preprocessor involvement
etc)...

Why? Where did the question come from?
 
P

prasoonthegreat

Why?  Where did the question come from?


The question was asked by one of the community members in the C
community on orkut......

I suggested the above solution but it did not work in gcc although
worked on some outdated compiler.....

That is why thought of asking here.....no offense....!!!!!

Prasoon.....
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top