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 typed the code below to my text editor and executed by using gcc compiler:
Code:
#include<stdio.h>
main()
{ int a,b,c;
scanf(“%d*“,&a);
scanf(“*%d*“,&b);
printf(“%d %d \n“,a,b);
swap(&a, &b);
printf(“%d %d \n”,a,b);
}
/*This is the C subroutine that does the swap using pointers.*/
void swap(int *px, int *py)
/* interchange *px and *py */
{ int temp;
temp = *px;
*px = *py;
*py = temp;
}
but, it gave me the error below:
new.c:11:6: warning: conflicting types for ‘swap’ [enabled by default]
new.c:7:8: note: previous implicit declaration of ‘swap’ was here
what is the problem here?
Any further help is muchly appreciated..
You need to declare the function before calling it.
Code:
#include<stdio.h>
void swap(int *px, int *py);
main()
{ int a,b,c;
scanf(“%d*“,&a);
scanf(“*%d*“,&b);
printf(“%d %d \n“,a,b);
swap(&a, &b);
printf(“%d %d \n”,a,b);
}
/*This is the C subroutine that does the swap using pointers.*/
void swap(int *px, int *py)
/* interchange *px and *py */
{ int temp;
temp = *px;
*px = *py;
*py = temp;
}
Agreed. The "C" language allows such declarations to be made in a per-function limited scope, but that's not normally what is done. The normal practice in that language is to issue declarations in advance, so that they have a per-file-level scope. (Case in point is stdio.h itself, which, if you care to open the file and look at it, is chock-full of just such declarations, concerning the standard-I/O library.) The preferred practice, "when in Rome," is "to do what the Romans do."
Last edited by sundialsvcs; 05-16-2013 at 10:12 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.