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.
Originally posted by Inuyasha-kun Thanks, that fixed the entire problem.
Do you know how to make the program to execute without the "./" ?
If its a user account, you can add the directory where the executable(s) lies to that users path, I have a "home made" executable directory for all the programs I make.
Originally posted by Inuyasha-kun Since it was Christmas break, I decided to practice with C. i was wondering, why does this code give out so many warnings?
Code:
#include <stdio.h>
//A is the number to pick the operation
//B and C are the single-precision floating point variables that will be modified
int a;
float b,c;
int main()
{
float e;
printf("NCalc 1.0b for Linux\nPick an op\n1:Add\n2:Sub\n3:Mul\n4:Div\n:");
scanf("%d",&a);
printf("\nNumber 1:");
scanf("%f",&b);
printf("\nNumber 2:");
scanf("%f",&c);
if(a == 1) printf("%f+%f=%f\n",b,c,addit(e));
if(a == 2) printf("%f-%f=%f\n",b,c,subit(e));
if(a == 3) printf("%fx%f=%f\n",b,c,mulit(e));
if(a == 4) printf("%f/%f=%f\n",b,c,divit(e));
return 0;
}
void addit(float d)
{
float d;
d=b+c;
return d;
}
void subit(float d)
{
float d;
d=b-c;
return d;
}
void mulit(float d)
{
float d;
d=b*c;
return d;
}
void divit(float d)
{
float d;
d=b/c;
return d;
}
To start, you didn't declare the functions before you used them. Either make a header file, or include the same info at the start of your file. The output from gcc would be nice but that should solve a lot of your pbelms. Another is that you declare d twice inside your functions
void divit(float d)
{
float d;
What happens is whatver flat divit gets, it names d and continues. This is the same as declaring the same variable twice.
Also a suggestion try to name your variables well. Things end up getting mangled otherwise. Another thing is that you return something when you declared that function void. Declare it to return a float if you want to do that. That's everything I caught at first glance
The gcc error messages will show whaterver is left.
Originally posted by Inuyasha-kun Since it was Christmas break, I decided to practice with C. i was wondering, why does this code give out so many warnings?
Code:
#include <stdio.h>
//A is the number to pick the operation
//B and C are the single-precision floating point variables that will be modified
int a;
float b,c;
int main()
{
float e;
printf("NCalc 1.0b for Linux\nPick an op\n1:Add\n2:Sub\n3:Mul\n4:Div\n:");
scanf("%d",&a);
printf("\nNumber 1:");
scanf("%f",&b);
printf("\nNumber 2:");
scanf("%f",&c);
if(a == 1) printf("%f+%f=%f\n",b,c,addit(e));
if(a == 2) printf("%f-%f=%f\n",b,c,subit(e));
if(a == 3) printf("%fx%f=%f\n",b,c,mulit(e));
if(a == 4) printf("%f/%f=%f\n",b,c,divit(e));
return 0;
}
void addit(float d)
{
float d;
d=b+c;
return d;
}
void subit(float d)
{
float d;
d=b-c;
return d;
}
void mulit(float d)
{
float d;
d=b*c;
return d;
}
void divit(float d)
{
float d;
d=b/c;
return d;
}
omg, i don't even know where to begin...
1) you used the functions addit(), subit(), mulit(), divit() before you declared them.
2) in those functions mentioned about, you have "d" declared twice...as a formal parameter and as a local variable.
3) you claim those functions to return void, but you are really returning a float (i.e. you are returning d).
i think those three things are what are causing your warnings...now on what else is wrong.
1) "a", "b", and "c" are global, this is not good programming practice.
2) you functions don't make much sense. you should do away with the vars "b", "c", and "d" and define your functions like this:
Code:
float addit(float a, float b) {
return a + b;
}
what you >really< need to do is take a class from a >good< teacher, or get a >good< book on c and another good book on good/proper programming practices...
I think the best teacher is good code. Go on sourceforge, get a good book (I don't know any good C books, only C++ books), read, and look at the code. Then implement something similar, without looking at the code this time.
You'll learn a lot.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.