Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so I've written some codes to add two numbers in C, but I can't figure out how to add three instead without spilling the results into another variable or clobbering the caller's version of the variables?
int add(a,b,c)
int a, b, c;
{ int tempr;
for(;b--;++a); // danger
/*tempr = c+a;*/
tempr = a+c;
return (tempr);
}
Here's how, if you must use a function for some reason.
int add(int a,int b,int c) {
return a+b+c;
}
When you define a function in C, you have to define the type of the parameters as well.
Please note the following:
It is an error to define a variable in a function that has the same name as a function parameter
Although not an error, there's no need for parentheses when calling return (tempr);.
return tempr; is perfectly fine
if you use a for loop, ending it with ; immediately after the for statement will result in the following statement not being a part of the loop. This might not be what you had in mind.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So, the question was find the greatest number using "if" statement between three numbers. I wrote the program myself and I'm a newbie to programming so, I tried to run this program but it showed 2 errors repeatedly.
error 1: x is undefined
error 2: y is undefined
So, I understand that it wants me to define the value of x,y but what I want to do is to firstly Compare the value of a and b and then compare the value of c with the greater number I got by comparing a and b.
I'm pasting my code below:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("enter any three number::");
scanf("%d%d%d",&a,&b,&c);
if(x=(a>b)||(b>a));
printf("\n(a>b)||(b>a)=%d",&x);
{
if(y=(x>c)||(c>x));
printf("\n(y>c)||(c>y)=%d",&y);
}
getch();
return 0;
}
How can I improvise this? Hoping for positive response!
The problem is, you never defined x and y, as your compiler pointer out. Just define them like you did for other variables.
int a,b,c,x,y;
would do the job.
That said
you don't really need those if statements, as you used here.
The result of the relational operators (< / >) are not the numbers, it's an integer value, either 0 or 1.
For %d, you don't need to supply the address of the variable, just the variable is enough.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Hi can anyone help me to correct this code, the result should be
/c=4.000000/
/d=4.0000 /
I know by putting the logic in single printf() i will get my result but i am not understanding that how to use two printf() and the varibles will be given by the second printf().
Here is my code:-
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
putchar(10);
return 0;
}
If I change my code to this, I will get the result,
#include<stdio.h>
int main()
{
int a=19,b=4;
float c,d;
c=a/b;
d=a%b;
/*
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
*/
printf("/c=%12f/\n/d=%-12.4f/",c,d);
putchar(10);
return 0;
}
but I want to use two printf() statements.
Thanks in advance.
You can not do this:
printf("/c=%12f/\nd=%");
printf("-12.4f/",c,d);
because you are lying to both printfs, in the first one you don't use the specifiers and in the second one you use specifiers that are not expected.
You can do this:
printf("/c=%12f/\nd=%"
"-12.4f/",c,d);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to C. I was just interested in making a program which takes two integer input from the user and add it but the condition is that i have to use only one variable. I came up with this code:
#include <stdio.h>
int main()
{
int *a;
scanf("%d%d",a,(a+1));
printf("\nSum=%d",*a+*(a+1));
return 0;
}
scanf() function takes an valid address as an argument and i am passing the value in a(which is not initialised yet). So, how this code worked in Turbo C++?
You are trying to access an area that is not within the scope of the program. Luckily TCC gave it, but I believe if you go on experimenting, results will be undefined.
You can do something like this to solve your problem of adding using 1 variable.
int main()
{
int a;
scanf("%d",&a); //scan the first number
getchar();
a += getchar()-'0'; // get the second number (restricted to 0-9)
printf("%d",a);
return 0;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Option 1: In function
int funcA(int a){
if(a < 0){
return -1;
}
else{
...
return 0;
}
}
Option 2: In main
int main(){
int a;
...
if(a < 0){
...
}
else{
funcA(a);
}
}
Option 3: Both places? If you have some suggestions I will be grateful.
There is no hard and fast rule as to what is better -- there's basically two cases you'd want to consider.
If the constraints are unique to this specific usage of the function, then it would be preferable to put them outside the function, to let the function be more generalized and limit only this specific call to it.
If the constraints are something integral to the function and are always going to be expected whenever the function is called, then it would be preferable to put them inside the function, so that they do not have to be duplicated every place that the function will be called.
It is the callers responsibility to make sure it calls a function with valid arguments - per the documentation.
It is the called functions interest to guard against invalid values when it can - and again, per the documentation.
There is no inherently "right" or "wrong" choice without a contract (documentation) for funcA which is highly dependent on what the function does and how it is expected to be used.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am having difficulty getting the real output from a C function. For example:
int max3(int a, int b, int c){
if ((a>b)&&(a>c))
return a;
if ((b>c)&&(b>a))
return b;
return c;
}
Can you give me an idea of how to specify the real output (e.g. tools, algorithms, etc.)?
In this above example, the real output is 6 (in case (a,b,c) = (1,2,6)).
Thank in advance very much.
I'd have probably written it, as it is simple, using the ternary operator:
int max3(int a, int b, int c){
if (a>b)
return (a>c)?a:c;
else
return (b>c)?b:c;
}