|
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
Hi all,
Can we assign return value of a function to a global variable? As we know, main() will be the first function to be executed. but if the above is true, then we have a function call before main. Please me calarifying this. The code may be of the form. int f(); int x = f(); int main() { printf("%d", x); } int f() { x=9; } In Turbo C++ compiler, it gives x = 9; how is this possible? Srinu. |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
Srinu wrote:
> Hi all, > > Can we assign return value of a function to a global variable? As we > know, main() will be the first function to be executed. but if the > above is true, then we have a function call before main. Please > me calarifying this. The code may be of the form. > > int f(); To state explicitly that the function takes no parameters use the `void` keyword. int f(void); > int x = f(); > > int main() > { > printf("%d", x); Include <stdio.h> for the prototype for printf. Without it, you are invoking undefined behaviour. > } Since main is declared as returning an int, return a value. Use 0 or EXIT_SUCCESS for sucessful termination and EXIT_FAILURE for abnormal termination. The macros are defined in <stdlib.h> > int f() > { > x=9; f() is declared as returning an int and you return nothing here. This is disallowed under the latest C Standard and can lead to unpredictable behaviour if you attempt to use the return value of f(), as you've done so. If you don't want a function to return a value specify: void f() ... > } > > In Turbo C++ compiler, it gives x = 9; how is this possible? By sheer luck. |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
Srinu wrote:
> > Hi all, > > Can we assign return value of a function to a global variable? It's undefined. > As we > know, main() will be the first function to be executed. but if the > above is true, then we have a function call before main. Please > me calarifying this. The code may be of the form. > > int f(); > int x = f(); > > int main() > { > printf("%d", x); > } > > int f() > { > x=9; > } > > In Turbo C++ compiler, it gives x = 9; how is this possible? The code is undefined. -- pete |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
"Srinu" <sinu.nayak2001@gmail.com> wrote in message > Can we assign return value of a function to a global variable? As we > know, main() will be the first function to be executed. but if the > above is true, then we have a function call before main. Please > me calarifying this. The code may be of the form. > > int f(); > int x = f(); > > int main() > { > printf("%d", x); > } > > int f() > { > x=9; > } > > In Turbo C++ compiler, it gives x = 9; how is this possible? > C++ allows you to define global objects, and will call their constructors before calling main(). The C++ standard was a little bit weak in this respect last time I checked, with the order in which objects are constructed not properly defined. C doesn't allow any functions to be called excpet from main(), but is it quite possible that your compiler will allow it as an extension. A lot of C compilers are developed alongside C++ compilers after all. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
santosh <santosh.k83@gmail.com> writes:
> Srinu wrote: > >> Hi all, >> >> Can we assign return value of a function to a global variable? As we >> know, main() will be the first function to be executed. but if the >> above is true, then we have a function call before main. Please >> me calarifying this. The code may be of the form. >> >> int f(); > > To state explicitly that the function takes no parameters use the `void` > keyword. What does "f()" state? |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
Richard wrote:
> santosh <santosh.k83@gmail.com> writes: > >> Srinu wrote: >> >>> Hi all, >>> >>> Can we assign return value of a function to a global variable? As we >>> know, main() will be the first function to be executed. but if the >>> above is true, then we have a function call before main. Please >>> me calarifying this. The code may be of the form. >>> >>> int f(); >> To state explicitly that the function takes no parameters use the `void` >> keyword. > > What does "f()" state? Do you truly not know? I thought you'd been on this newsgroup long enough to have seen this mentioned half a dozen times, but perhaps that's a different "Richard." It states that the function f takes some fixed number of arguments, but does not state what that number is nor what the types of the arguments are. -- Eric Sosman esosman@ieee-dot-org.invalid |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> Richard wrote: >> santosh <santosh.k83@gmail.com> writes: >> >>> Srinu wrote: >>> >>>> Hi all, >>>> >>>> Can we assign return value of a function to a global variable? As we >>>> know, main() will be the first function to be executed. but if the >>>> above is true, then we have a function call before main. Please >>>> me calarifying this. The code may be of the form. >>>> >>>> int f(); >>> To state explicitly that the function takes no parameters use the `void` >>> keyword. >> >> What does "f()" state? > > Do you truly not know? I thought you'd been on this > newsgroup long enough to have seen this mentioned half a > dozen times, but perhaps that's a different "Richard." Nope. Probably me. And I never knew that. I had always assumed it to be a lazy definition of f(void), but since I have never used it (I cant remember the last time I wrote a function without at least one parameter) then I wasn't sure. > > It states that the function f takes some fixed number Or doesn't state :-; > of arguments, but does not state what that number is nor > what the types of the arguments are. Which is used where? |
|
|
|
#8 (permalink) |
|
Messages: n/a
Hébergeur: |
Richard wrote:
> Eric Sosman <esosman@ieee-dot-org.invalid> writes: > >> Richard wrote: >>> santosh <santosh.k83@gmail.com> writes: >>> >>>> Srinu wrote: .... >>>>> int f(); >>>> To state explicitly that the function takes no parameters use the `void` >>>> keyword. >>> What does "f()" state? >> Do you truly not know? I thought you'd been on this >> newsgroup long enough to have seen this mentioned half a >> dozen times, but perhaps that's a different "Richard." > > Nope. Probably me. And I never knew that. I had always assumed it to be > a lazy definition of f(void), but since I have never used it (I cant > remember the last time I wrote a function without at least one > parameter) then I wasn't sure. > >> It states that the function f takes some fixed number > > Or doesn't state :-; Well, it is undefined behavior to call f() with a different number of arguments than the number specified in the definition of f(), or if the definition of f() makes it a variadic function. So this declaration does indeed state that the number is fixed. >> of arguments, but does not state what that number is nor >> what the types of the arguments are. > > Which is used where? It's only supported to allow compilation of code written before the invention of proper function prototypes. It serves no good purpose that is not better served by a function prototype. It can be used to obfuscate code, if that's your desire. |
|
![]() |
| Outils de la discussion | |
|
|