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.
HI All,
I have written a simple C program in which I dont feel anything is wrong but still the GCC compiler is giving errors and warning.Can someone help me find out whats exactly going wrong in here.
The errors are
Quote:
prog2.c:3: error: syntax error before '*' token
prog2.c: In function `swap':
prog2.c:4: error: number of arguments doesn't match prototype
prog2.c:2: error: prototype declaration
prog2.c:6: error: `x' undeclared (first use in this function)
prog2.c:6: error: (Each undeclared identifier is reported only once
prog2.c:6: error: for each function it appears in.)
prog2.c:7: error: `y' undeclared (first use in this function)
And the program is
Quote:
#include<stdio.h>
int swap(int *,int *);
int swap(*x,*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
int main(void)
{
int a=10,b=20;
swap(&a,&b);
printf("a=%d",a);
printf("b=%d",b);
return 0;
}
I've reported this to be moved to Programming, since it's more suitable there.
First of all, the prototype
int swap(int *,int *);
is unnecessary, since you're defining the function before main().
Secondly, the line
int swap(*x,*y)
is incorrect, as you've not given types for the parameters x and y, other than specifying that they're pointers. Edit: also, surely this function should return void, as it doesn't return anything.
Edit: also, in future, put your code between CODE tags, rather than QUOTE tags, as the former preserve indentation.
is unnecessary, since you're defining the function before main().
I think that is a matter of style, not an error, so I hope your opinion on that didn't distract Ajit Gunge from understanding the important info you gave next.
Quote:
the line
int swap(*x,*y)
is incorrect, as you've not given types for the parameters x and y, other than specifying that they're pointers.
That is the major error.
Quote:
surely this function should return void, as it doesn't return anything.
Correct, but I think most compilers give just a warning, not an error for that mistake.
Quote:
also, in future, put your code between CODE tags
I'll repeat that as well.
In addition:
Code:
printf("a=%d",a);
printf("b=%d",b);
I expect the OP will understand the minor error in those lines better by seeing the output of the program before correcting that error, than if I pointed it out now.
Quote:
Originally Posted by Ajit Gunge
Thanks!
I think it is more polite to save that for the post you put at the end of the thread telling us that you understood our answers and your program is now working.
Obviously, feel free to first post follow up questions if you didn't understand the answers or your program still doesn't work. Though unfortunately common, it is impolite to skip posting a final status (it worked or you gave up).
I think that is a matter of style, not an error, so I hope your opinion on that didn't distract Ajit Gunge from understanding the important info you gave next.
Fair enough.
Quote:
Correct, but I think most compilers give just a warning, not an error for that mistake.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.