View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-09-2002, 01:39 PM   #1
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
why won't the following compile?

Hi. what am I doing wrong here. >

#include <stdio.h>
int copy(int a);

int main(void)

int num = 5;
int c = 0;

c = copy(num);

printf("num = %d\n", c);

return 0;
int copy(int a)

int a = num;

return a;

gcc gives me the following errors but since I am fairly new I don't understand them

copy.c: In function `copy':
copy.c:19: warning: declaration of `a' shadows a parameter
copy.c:19: `num' undeclared (first use in this function)
copy.c:19: (Each undeclared identifier is reported only once
copy.c:19: for each function it appears in.)

it says 'num' is undeclared but I decalred it in 'main' ...

Old 09-09-2002, 02:23 PM   #2
Senior Member
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
it's a scope issue, copy() can't see your num variable, num is local to main() move num outside of main to make it global or add int num inside of copy().
Old 09-10-2002, 01:18 AM   #3
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks havent got to scope yet but I think I know what you mean
Old 09-10-2002, 04:33 AM   #4
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
I think you are redfining "int a" in copy. Scope issue is also involved. So the local int shadows the "int a" defined in copy(int a). Try this.

int copy(int a)
return a;

Old 09-10-2002, 08:12 PM   #5
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
int copy(int num)
int a;
return a=num;

I think thats what you are trying to do!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
compile 2.6.13 jupiter3437 Linux - Software 3 09-09-2005 05:09 PM
ntop compile/post-compile problem? tjb Linux - Software 3 12-28-2004 05:22 PM
Cannot compile c davidtsl Programming 8 12-13-2004 02:36 AM
Compile help Twiggy794 Linux - Software 1 12-03-2003 02:53 AM
How do I compile? icecream33 Programming 2 10-07-2002 07:28 AM

All times are GMT -5. The time now is 05:38 AM.

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