LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2007, 02:12 PM   #1
joel2001k
Member
 
Registered: Mar 2007
Distribution: GNU/Linux debian unstable main
Posts: 95

Rep: Reputation: 17
function in a function in C compiling with gcc


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
 
Old 06-22-2007, 02:53 PM   #2
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
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();
}

Last edited by card-suse; 06-22-2007 at 03:08 PM.
 
Old 06-23-2007, 02:47 AM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
I agree to card-suse. But most people like to place the prototypes of their functions at the top and define the functions after main.

PHP Code:
#include <stdio.h>

void func_a(int);
void func_b(int);

int main(int argcchar **argv)
{
  
func_a();
}

void func_a()
{
  
int value;

  
value 0;
  
func_b(value);
}

func_b(int value)
{
    
fprintf(stdout"%d\n\0"value);

(It doesn't matter in which way you declare and define the functions this way).
 
Old 06-23-2007, 09:40 AM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Nested functions are supported in GCC: http://gcc.gnu.org/onlinedocs/gcc-4....sted-Functions

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.
 
Old 06-23-2007, 08:16 PM   #5
card-suse
Member
 
Registered: Dec 2005
Location: Ohio, USA
Distribution: OpenSuSE 10.2
Posts: 74

Rep: Reputation: 15
Will that be portable? I was referencing Ritchie's book on C when I posted. Is this part of the ANSI standard? Thanks.
 
Old 06-23-2007, 08:18 PM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Quote:
Originally Posted by card-suse
Will that be portable? I was referencing Ritchie's book on C when I posted. Is this part of the ANSI standard? Thanks.
Nope, it's a GNU extension. But the OP mentions GCC, so I said it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how i get help from gcc about c function rajeshclt3 Linux - Software 2 10-12-2006 05:46 PM
Standard function problems with gcc lgi123 Programming 2 06-23-2006 03:17 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration