ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to write in C compiling with gcc a function in a function and call it within it, it might look like
Code:
#include <stdio.h>
void
func_a(){
int value;
void func_b();
value = 0;
func_b();
void func_b(){
fprintf(stdout, "%d\n\0", value);
}
}
int
main(int argc, char **argv){
func_a();
}
I have tried it, but i got errors like
Code:
error: invalid storage class for function "func_b"
error: invalid storage class for function "func_b"
warning: conflicting types for "func_b"
error: static declaration of "func_b" follows non-static declaration
error: previous implicit declaration of "func_b" was here
You can't do it. It violates the C standard. All function declarations are considered external, so you can't define a function within another function.
You would have to do something like:
Code:
#include <stdio.h>
void
func_b(int value){
fprintf(stdout, "%d\n\0", value);
}
void
func_a(){
int value;
value = 0;
func_b(value);
}
int
main(int argc, char **argv){
func_a();
}
And so is lexical scoping, so what you're trying to do makes perfect sense.
As far as the "invalid storage class" errors go, I think your particular version of GCC may be too old. Which one are you using? (These errors do not appear in 4.1.2 which is what I used with your code.) Or perhaps some of the command-line arguments you pass to GCC inhibit the GNU extensions? (I compiled with no options.)
For the conflicting declarations, I got it to work by making the declaration and definition in the same line, and placing it before the call, like so:
Code:
void
func_a(){
int value;
void func_b(){
fprintf(stdout, "%d\n\0", value);
}
value = 0;
func_b();
}
And it works exactly as expected.
Last edited by taylor_venable; 06-23-2007 at 09:41 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.